1
2#[repr(transparent)]
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct Color {
15 pub rgb565: u16,
16}
17
18pub const COLOR_BLACK: Color = Color::from_888(0, 0, 0);
19pub const COLOR_WHITE: Color = Color::from_888(255, 255, 255);
20pub const COLOR_RED: Color = Color::from_888(255, 0, 0);
21pub const COLOR_GREEN: Color = Color::from_888(0, 255, 0);
22pub const COLOR_BLUE: Color = Color::from_888(0, 0, 255);
23pub const COLOR_YELLOW: Color = Color::from_888(255, 255, 0);
24pub const COLOR_CYAN: Color = Color::from_888(0, 255, 255);
25pub const COLOR_MAGENTA: Color = Color::from_888(255, 0, 255);
26pub const COLOR_GRAY: Color = Color::from_888(128, 128, 128);
27
28impl Color {
29 #[inline]
30 pub const fn from_components(r: u8, g: u8, b: u8) -> Self {
32 Color {
33 rgb565: ((r as u16) << 11) | ((g as u16) << 5) | (b as u16),
34 }
35 }
36 pub const fn from_888(r: u8, g: u8, b: u8) -> Self {
38 Color::from_components(r >> 3, g >> 2, b >> 3)
39 }
40
41 pub fn apply_light(&self, light_level: u8) -> Self {
43 let light_level = light_level as u16;
44 let (r, g, b) = self.get_components();
45 Color::from_components(
46 ((r as u16 * light_level / 255).min(31)) as u8,
47 ((g as u16 * light_level / 255).min(63)) as u8,
48 ((b as u16 * light_level / 255).min(31)) as u8,
49 )
50 }
51
52 pub const fn get_components(&self) -> (u8, u8, u8) {
54 let r = self.rgb565 >> 11;
55 let g = (self.rgb565 & 0b0000011111100000) >> 5;
56 let b = self.rgb565 & 0b0000000000011111;
57
58 (r as u8, g as u8, b as u8)
59 }
60
61 pub const fn get_888(&self) -> (u8, u8, u8) {
63 let (r, g, b) = self.get_components();
64
65 return (
66 ((r << 3) | (r >> 2)) as u8,
67 ((g << 2) | (g >> 4)) as u8,
68 ((b << 3) | (b >> 2)) as u8,
69 );
70 }
71}