Skip to content

Commit

Permalink
fix colormap
Browse files Browse the repository at this point in the history
  • Loading branch information
barnex committed Oct 31, 2014
1 parent 1b130b1 commit 7ebd4ff
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions draw/colorscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,55 @@ import "image/color"
import "fmt"

func ColorMap(min, max, value float32, colormap ...color.RGBA) color.RGBA {
if len(colormap) == 0 {
// default colormap: black-white
if len(colormap) < 1 {
colormap = []color.RGBA{{0, 0, 0, 255}, {255, 255, 255, 255}}
}

col := (value - min) / (max - min)
if col > 1. {
col = 1.
// map value to interval [O,1]
val := float64((value - min) / (max - min))
if val > 1 {
val = 1
}
if col < 0. {
col = 0.
if val < 0 {
val = 0
}

N := float64(len(colormap)) - 1
lower := int(float64(col) * N)
upper := lower + 1

if lower < 0 {
panic(fmt.Sprint("lower=", lower))
}

if upper >= len(colormap) {
return colormap[lower]
// find index of color below our value
maxIndex := float64(len(colormap) - 1)
index := val * maxIndex
// corner case val==max:
if index == maxIndex {
index--
}

c1 := colormap[lower]
c2 := colormap[upper]

x := (float64(col) - float64(lower)/N)

fmt.Println(x)
// get two neighboring colors
i := int(index)
c1 := colormap[i]
c2 := colormap[i+1]

// location between two neighboring colors [0..1]
x := (val - float64(i)/maxIndex) * maxIndex
if x < 0 || x > 1 {
panic(fmt.Sprint("x=", x))
}

// interpolate between colors
r := (1-x)*float64(c1.R) + x*float64(c2.R)
g := (1-x)*float64(c1.G) + x*float64(c2.G)
b := (1-x)*float64(c1.B) + x*float64(c2.B)
a := (1-x)*float64(c1.A) + x*float64(c2.A)

return color.RGBA{bte(r), bte(g), bte(b), bte(a)}

}

func bte(x float64) uint8 {
if x < 0 {
return 0
}
if x > 1 {
if x > 255 {
return 255
}
return uint8(255 * x)
return uint8(x)
}

0 comments on commit 7ebd4ff

Please sign in to comment.