LEDController/internal/patterns/plasma.go

68 lines
1.3 KiB
Go
Raw Normal View History

package patterns
import "math"
type RGBcolor = [3]byte
func plasmaRGBFromVal(val byte) (byte, byte, byte) {
var r byte
var g byte
var b byte
if val < 85 {
r = val * 3
g = 255 - r
b = 0
} else if val < 170 {
b = (val - 85) * 3
r = 255 - b
g = 0
} else {
g = (val - 170) * 3
b = 255 - g
r = 0
}
return r, g, b
}
func LinearPlasma(l int) []byte {
line := make([]byte, l*3)
for i := 0; i < l*3; i += 3 {
val := byte(i % 256)
r, g, b := plasmaRGBFromVal(val)
line[i] = r
line[i+1] = g
line[i+2] = b
}
return line
}
var offset float64 = 0.5
func PlasmaPanel(w int, h int, speed uint16) [][]RGBcolor {
grid := make([][]RGBcolor, w)
for i := 0; i < w; i++ {
grid[i] = make([]RGBcolor, h)
}
scale := math.Pi * 2.0 / float64(w)
step := float64(5 * speed / 40)
offset -= step
if offset < 0 {
offset += 1.0
}
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
u := math.Cos((float64(x) + offset) * scale)
v := math.Cos((float64(y) + offset) * scale)
w := math.Cos((float64(w) + offset) * scale)
e := (u + v + w + 3.0) / 6.0
r, g, b := plasmaRGBFromVal(byte((offset + e) * 255))
var rgb [3]byte
rgb[0] = r
rgb[1] = g
rgb[2] = b
grid[x][y] = rgb
}
}
return grid
}