ws2812_flexio/
pixel.rs

1/// A pixel that can be rendered with this library.
2pub trait Pixel {
3    /// The return type of the [into_ws2812_bytes()](Pixel::into_ws2812_bytes) function.
4    type BytesIter: Iterator<Item = u8>;
5
6    /// Return the raw bytes that should be sent to the LED strip.
7    ///
8    /// IMPORTANT: Be aware that WS2812 strips are GRB encoded.
9    fn into_ws2812_bytes(self) -> Self::BytesIter;
10}
11
12/// Raw RGB data.
13impl Pixel for [u8; 3] {
14    type BytesIter = core::array::IntoIter<u8, 3>;
15
16    fn into_ws2812_bytes(self) -> Self::BytesIter {
17        // Neopixel strips want GRB data
18        [self[1], self[0], self[2]].into_iter()
19    }
20}
21
22/// Raw RGBW data.
23impl Pixel for [u8; 4] {
24    type BytesIter = core::array::IntoIter<u8, 4>;
25
26    fn into_ws2812_bytes(self) -> Self::BytesIter {
27        self.into_iter()
28    }
29}
30
31/// 8-bit Linear sRGB, which is the color space
32/// most NeoPixel strips are in.
33///
34/// Be aware that this differs from normal,
35/// gamma-corrected sRGB. A conversion has to take place.
36///
37/// More info can be found in the documentation of the
38/// [palette] crate.
39impl Pixel for palette::LinSrgb<u8> {
40    type BytesIter = core::array::IntoIter<u8, 3>;
41
42    fn into_ws2812_bytes(self) -> Self::BytesIter {
43        [self.green, self.red, self.blue].into_iter()
44    }
45}
46
47impl<'a, P> Pixel for &'a P
48where
49    P: Pixel + Clone,
50{
51    type BytesIter = <P as Pixel>::BytesIter;
52    fn into_ws2812_bytes(self) -> Self::BytesIter {
53        self.clone().into_ws2812_bytes()
54    }
55}