embedded_graphics_simulator/
theme.rs

1use embedded_graphics::pixelcolor::{Rgb888, RgbColor};
2
3/// Color theme for binary displays
4#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
5pub enum BinaryColorTheme {
6    /// A simple on/off, non-styled display with black background and white pixels
7    #[default]
8    Default,
9
10    /// Inverted colors.
11    Inverted,
12
13    /// An on/off classic LCD-like display with white background
14    LcdWhite,
15
16    /// An on/off classic LCD-like display with green background and dark grey pixels
17    LcdGreen,
18
19    /// An on/off LCD-like display with light blue background and blue-white pixels
20    LcdBlue,
21
22    /// An on/off OLED-like display with a black background and white pixels
23    OledWhite,
24
25    /// An on/off OLED-like display with a dark blue background and light blue pixels
26    OledBlue,
27
28    /// Custom binary color theme/mapping
29    Custom {
30        /// The color used for the "off" state pixels.
31        color_off: Rgb888,
32        /// The color used for the "on" state pixels.
33        color_on: Rgb888,
34    },
35}
36
37fn map_color(color: Rgb888, color_off: Rgb888, color_on: Rgb888) -> Rgb888 {
38    match color {
39        Rgb888::BLACK => color_off,
40        _ => color_on,
41    }
42}
43
44impl BinaryColorTheme {
45    /// Gets the theme's pixel color for a given pixel state.
46    pub(crate) fn convert(self, color: Rgb888) -> Rgb888 {
47        match self {
48            BinaryColorTheme::Default => color,
49            BinaryColorTheme::Custom {
50                color_off,
51                color_on,
52            } => map_color(color, color_off, color_on),
53            BinaryColorTheme::Inverted => {
54                Rgb888::new(255 - color.r(), 255 - color.g(), 255 - color.b())
55            }
56            BinaryColorTheme::LcdWhite => {
57                map_color(color, Rgb888::new(245, 245, 245), Rgb888::new(32, 32, 32))
58            }
59            BinaryColorTheme::LcdGreen => {
60                map_color(color, Rgb888::new(120, 185, 50), Rgb888::new(32, 32, 32))
61            }
62            BinaryColorTheme::LcdBlue => {
63                map_color(color, Rgb888::new(70, 80, 230), Rgb888::new(230, 230, 255))
64            }
65            BinaryColorTheme::OledBlue => {
66                map_color(color, Rgb888::new(0, 20, 40), Rgb888::new(0, 210, 255))
67            }
68            BinaryColorTheme::OledWhite => map_color(color, Rgb888::new(20, 20, 20), Rgb888::WHITE),
69        }
70    }
71}