#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Color {
pub const fn new(r: u8, g: u8, b: u8) -> Self {
Self { r, g, b }
}
pub const fn from_rgb24(rgb: u32) -> Self {
Self {
r: ((rgb >> 16) & 0xFF) as u8,
g: ((rgb >> 8) & 0xFF) as u8,
b: (rgb & 0xFF) as u8,
}
}
pub const fn to_apa102_data(&self) -> u32 {
(self.b as u32) << 8 | (self.g as u32) << 16 | (self.r as u32) << 24
}
pub const BLACK: Color = Color::new(0, 0, 0);
pub const RED: Color = Color::new(255, 0, 0);
pub const GREEN: Color = Color::new(0, 255, 0);
pub const BLUE: Color = Color::new(0, 0, 255);
pub const YELLOW: Color = Color::new(255, 255, 0);
pub const CYAN: Color = Color::new(0, 255, 255);
pub const MAGENTA: Color = Color::new(255, 0, 255);
pub const WHITE: Color = Color::new(255, 255, 255);
pub const ORANGE: Color = Color::new(255, 165, 0);
pub const PURPLE: Color = Color::new(128, 0, 128);
}
impl From<(u8, u8, u8)> for Color {
fn from((r, g, b): (u8, u8, u8)) -> Self {
Self::new(r, g, b)
}
}
impl From<Color> for (u8, u8, u8) {
fn from(color: Color) -> Self {
(color.r, color.g, color.b)
}
}
impl From<u32> for Color {
fn from(rgb: u32) -> Self {
Self::from_rgb24(rgb)
}
}