-
Notifications
You must be signed in to change notification settings - Fork 0
/
colormap.go
61 lines (46 loc) · 1007 Bytes
/
colormap.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
61
package mplgo
import (
"image"
"image/color"
"math"
)
var BAD_COLOR = color.RGBA{1.0, 1.0, 1.0, 0.0}
type ColorMap struct {
data []color.RGBA
name string
steps int
fsteps float64
}
func (m ColorMap) Map(in float64) color.RGBA {
in = math.Max(in, 0.0)
in = math.Min(in, 1.0)
rounded := math.Round(in * (m.fsteps - 1.0))
if math.IsNaN(rounded) {
return BAD_COLOR
}
idx := int(math.Round(in * (m.fsteps - 1.0)))
if idx < 0 {
idx = 0
}
return m.data[idx]
}
func (m ColorMap) MapArray(in [][]float64) [][]color.RGBA {
output := make([][]color.RGBA, len(in))
for i, line := range in {
outputLine := make([]color.RGBA, len(line))
for j, val := range line {
outputLine[j] = m.Map(val)
}
output[i] = outputLine
}
return output
}
func (m ColorMap) MapArrayToImage(in [][]float64) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, len(in), len(in[0])))
for i, line := range in {
for j, val := range line {
img.Set(j, i, m.Map(val))
}
}
return img
}