rpi_led_matrix/
led_color.rs

1#[cfg(feature = "embeddedgraphics")]
2use embedded_graphics_core::pixelcolor::{
3    raw::RawU24, Bgr555, Bgr565, Bgr888, BinaryColor, Gray2, Gray4, Gray8, GrayColor, PixelColor,
4    Rgb555, Rgb565, Rgb888, RgbColor,
5};
6
7/// The Rust handle representing a color you'd like to display.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct LedColor {
10    /// the red channel value
11    pub red: u8,
12    /// the green channel value
13    pub green: u8,
14    /// the blue channel value
15    pub blue: u8,
16}
17
18#[cfg(feature = "embeddedgraphics")]
19impl PixelColor for LedColor {
20    type Raw = RawU24;
21}
22
23#[cfg(feature = "embeddedgraphics")]
24impl From<Bgr555> for LedColor {
25    fn from(p: Bgr555) -> Self {
26        Self {
27            red: p.r() << 3,
28            green: p.g() << 3,
29            blue: p.b() << 3,
30        }
31    }
32}
33
34#[cfg(feature = "embeddedgraphics")]
35impl From<Bgr565> for LedColor {
36    fn from(p: Bgr565) -> Self {
37        Self {
38            red: p.r() << 3,
39            green: p.g() << 2,
40            blue: p.b() << 3,
41        }
42    }
43}
44
45#[cfg(feature = "embeddedgraphics")]
46impl From<Bgr888> for LedColor {
47    fn from(p: Bgr888) -> Self {
48        Self {
49            red: p.r(),
50            green: p.g(),
51            blue: p.b(),
52        }
53    }
54}
55
56#[cfg(feature = "embeddedgraphics")]
57impl From<Gray2> for LedColor {
58    fn from(p: Gray2) -> Self {
59        Self {
60            red: p.luma() << 6,
61            green: p.luma() << 6,
62            blue: p.luma() << 6,
63        }
64    }
65}
66
67#[cfg(feature = "embeddedgraphics")]
68impl From<Gray4> for LedColor {
69    fn from(p: Gray4) -> Self {
70        Self {
71            red: p.luma() << 4,
72            green: p.luma() << 4,
73            blue: p.luma() << 4,
74        }
75    }
76}
77
78#[cfg(feature = "embeddedgraphics")]
79impl From<Gray8> for LedColor {
80    fn from(p: Gray8) -> Self {
81        Self {
82            red: p.luma(),
83            green: p.luma(),
84            blue: p.luma(),
85        }
86    }
87}
88
89#[cfg(feature = "embeddedgraphics")]
90impl From<Rgb555> for LedColor {
91    fn from(p: Rgb555) -> Self {
92        Self {
93            red: p.r() << 3,
94            green: p.g() << 3,
95            blue: p.b() << 3,
96        }
97    }
98}
99
100#[cfg(feature = "embeddedgraphics")]
101impl From<Rgb565> for LedColor {
102    fn from(p: Rgb565) -> Self {
103        Self {
104            red: p.r() << 3,
105            green: p.g() << 2,
106            blue: p.b() << 3,
107        }
108    }
109}
110
111#[cfg(feature = "embeddedgraphics")]
112impl From<Rgb888> for LedColor {
113    fn from(p: Rgb888) -> Self {
114        Self {
115            red: p.r(),
116            green: p.g(),
117            blue: p.b(),
118        }
119    }
120}
121
122#[cfg(feature = "embeddedgraphics")]
123impl From<BinaryColor> for LedColor {
124    fn from(p: BinaryColor) -> Self {
125        let value = if p == BinaryColor::On {
126            std::u8::MAX
127        } else {
128            0
129        };
130        Self {
131            red: value,
132            green: value,
133            blue: value,
134        }
135    }
136}