LEDController/internal/patterns/gradient.go

31 lines
1.3 KiB
Go
Raw Permalink Normal View History

package patterns
// gradient returns from fromX to toX fading from (fromR,fromG,fromB) to (toR,toG,toB)
// at least that's the idea.
func Gradient(fromR byte, fromG byte, fromB byte, toR byte, toG byte, toB byte, fromX int, toX int) []byte {
ret := make([]byte, toX*3)
var stepR float32 = (float32(toR) - float32(fromR)) / (float32(toX) - float32(fromX))
var stepG float32 = (float32(toG) - float32(fromG)) / (float32(toX) - float32(fromX))
var stepB float32 = (float32(toB) - float32(fromB)) / (float32(toX) - float32(fromX))
for i := fromX; i < toX*3; i += 3 {
ret[i] = fromR + byte(float32(i/3)*stepR)
ret[i+1] = fromG + byte(float32(i/3)*stepG)
ret[i+2] = fromB + byte(float32(i/3)*stepB)
}
return ret
}
func GradientPanel(w int, h int, fromColour RGBcolor, toColour RGBcolor) [][]RGBcolor {
out := FillPanel(w, h, 255, 0, 0)
var stepR float32 = (float32(toColour[0]) - float32(fromColour[0])) / float32(w)
var stepG float32 = (float32(toColour[1]) - float32(fromColour[1])) / float32(w)
var stepB float32 = (float32(toColour[2]) - float32(fromColour[2])) / float32(w)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
out[x][y][0] = fromColour[0] + byte(float32(x)*stepR)
out[x][y][1] = fromColour[1] + byte(float32(x)*stepG)
out[x][y][2] = fromColour[2] + byte(float32(x)*stepB)
}
}
return out
}