Skip to main content

display_driver/
color.rs

1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2/// Color format used by the display.
3pub enum ColorFormat {
4    /// 1-bit per pixel (Monochrome).
5    Binary,
6    /// 2-bit grayscale.
7    Gray2,
8    /// 4-bit grayscale.
9    Gray4,
10    /// 8-bit grayscale.
11    Gray8,
12    /// 16-bit RGB565.
13    RGB565,
14    /// 18-bit RGB666.
15    RGB666,
16    /// 24-bit RGB888.
17    RGB888,
18}
19
20impl ColorFormat {
21    /// Returns the number of bits per pixel for this format.
22    pub fn size_bits(self) -> u8 {
23        match self {
24            ColorFormat::Binary => 1,
25            ColorFormat::Gray2 => 2,
26            ColorFormat::Gray4 => 4,
27            ColorFormat::Gray8 => 8,
28            ColorFormat::RGB565 => 16,
29            ColorFormat::RGB666 => 18,
30            ColorFormat::RGB888 => 24,
31        }
32    }
33
34    pub fn size_bytes(self) -> u8 {
35        match self {
36            ColorFormat::Binary => 1,
37            ColorFormat::Gray2 => 1,
38            ColorFormat::Gray4 => 1,
39            ColorFormat::Gray8 => 1,
40            ColorFormat::RGB565 => 2,
41            ColorFormat::RGB666 => 3,
42            ColorFormat::RGB888 => 3,
43        }
44    }
45}
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48pub enum ColorType {
49    Gray(u8),
50    Rgb(u8, u8, u8),
51}
52
53#[derive(Clone, PartialEq, Eq)]
54pub struct SolidColor {
55    pub raw: [u8; 3],
56    pub format: ColorFormat,
57    pub color: ColorType,
58}
59
60#[cfg(feature = "embedded-graphics")]
61mod eg_impls {
62    use super::*;
63    use embedded_graphics_core::pixelcolor::{
64        raw::ToBytes, PixelColor, Rgb565, Rgb666, Rgb888, RgbColor,
65    };
66
67    impl<'a> From<Rgb565> for SolidColor {
68        fn from(value: Rgb565) -> Self {
69            let mut raw = [0u8; 3];
70            raw[0..2].copy_from_slice(&<Rgb565 as PixelColor>::Raw::from(value).to_be_bytes());
71            SolidColor {
72                raw,
73                format: ColorFormat::RGB565,
74                color: ColorType::Rgb(value.r(), value.g(), value.b()),
75            }
76        }
77    }
78
79    #[cfg(feature = "embedded-graphics")]
80    impl<'a> From<Rgb666> for SolidColor {
81        fn from(value: Rgb666) -> Self {
82            let mut raw = [0u8; 3];
83            raw.copy_from_slice(&<Rgb666 as PixelColor>::Raw::from(value).to_be_bytes());
84            SolidColor {
85                raw,
86                format: ColorFormat::RGB666,
87                color: ColorType::Rgb(value.r(), value.g(), value.b()),
88            }
89        }
90    }
91
92    #[cfg(feature = "embedded-graphics")]
93    impl<'a> From<Rgb888> for SolidColor {
94        fn from(value: Rgb888) -> Self {
95            let mut raw = [0u8; 3];
96            raw.copy_from_slice(&<Rgb888 as PixelColor>::Raw::from(value).to_be_bytes());
97            SolidColor {
98                raw,
99                format: ColorFormat::RGB888,
100                color: ColorType::Rgb(value.r(), value.g(), value.b()),
101            }
102        }
103    }
104}