/
grad.go
60 lines (46 loc) · 1.06 KB
/
grad.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"image/color"
)
type GradStop struct {
Position float64
Color color.RGBA
}
type ColourGrad []GradStop
func (cg ColourGrad) makePal(min, max float64, numCol int) color.Palette {
pal := make([]color.Color, numCol, numCol)
p := min
pStep := (max - min) / float64(numCol)
for i := range pal {
pal[i] = cg.getColour(p)
p += pStep
}
return color.Palette(pal)
}
func (cg ColourGrad) getColour(pos float64) color.RGBA {
if pos <= 0.0 || len(cg) == 1 {
return cg[0].Color
}
last := cg[len(cg)-1]
if pos >= last.Position {
return last.Color
}
for i, stop := range cg[1:] {
if pos < stop.Position {
pos = (pos - cg[i].Position) / (stop.Position - cg[i].Position)
return colorLerp(cg[i].Color, stop.Color, pos)
}
}
return last.Color
}
func colorLerp(c0, c1 color.RGBA, t float64) color.RGBA {
return color.RGBA{
uint8Lerp(c0.R, c1.R, t),
uint8Lerp(c0.G, c1.G, t),
uint8Lerp(c0.B, c1.B, t),
uint8Lerp(c0.A, c1.A, t),
}
}
func uint8Lerp(a, b uint8, t float64) uint8 {
return uint8(float64(a)*(1.0-t) + float64(b)*t)
}