-
Notifications
You must be signed in to change notification settings - Fork 0
/
strip.go
55 lines (47 loc) · 1.27 KB
/
strip.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
package adalight
import (
"fmt"
"image/color"
)
// Strip represents an LED strip, and contains methods for interacting with the LEDs on the strip
type Strip struct {
length int
pixels []uint8
}
// Length returns the length of the strip (number of LEDs)
func (s *Strip) Length() int {
return s.length
}
// SetRGB sets a specific pixel `i` to a given color.RGBA color value
func (s *Strip) SetRGB(i int, rgb color.RGBA) {
if i < 0 || i > s.length {
return
}
s.pixels[(3*i)+0] = rgb.R
s.pixels[(3*i)+1] = rgb.G
s.pixels[(3*i)+2] = rgb.B
}
// Set sets a specific pixel `i` to a given color value
func (s *Strip) Set(i int, c color.Color) {
rgb := color.RGBAModel.Convert(c).(color.RGBA)
s.SetRGB(i, rgb)
}
// SetAllRGB sets all of the pixels on the strip to the given color.RGBA value
func (s *Strip) SetAllRGB(rgb color.RGBA) {
for i := 0; i < s.length; i++ {
s.SetRGB(i, rgb)
}
}
// SetAll sets all of the pixels on the strip to the given color.Color value
func (s *Strip) SetAll(c color.Color) {
rgb := color.RGBAModel.Convert(c).(color.RGBA)
s.SetAllRGB(rgb)
}
func newStrip(length int) *Strip {
if length < 1 {
fmt.Println("Invalid Strip length:", length)
panic(fmt.Sprintf("%v", length))
}
s := &Strip{length: length, pixels: make([]uint8, length*3)}
return s
}