improved Color enum

This commit is contained in:
Djeeberjr 2025-05-05 15:22:38 +02:00
parent 8fd8081ced
commit 75e0734e2b

View File

@ -1,17 +1,6 @@
#[derive(Clone, Copy)] use rgb::Rgb;
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
}
impl Color { #[derive(Debug)]
pub fn to_grb(self) -> [u8; 3] {
[self.g, self.r, self.b]
}
}
#[derive(Debug, Clone, Copy)]
pub enum NamedColor { pub enum NamedColor {
Red, Red,
Green, Green,
@ -23,29 +12,29 @@ pub enum NamedColor {
Magenta, Magenta,
} }
impl From<NamedColor> for Color { impl Into<Rgb<u8>> for NamedColor {
fn from(named: NamedColor) -> Self { fn into(self) -> Rgb<u8> {
match named { match self {
NamedColor::Red => Color { r: 255, g: 0, b: 0 }, NamedColor::Red => Rgb { r: 255, g: 0, b: 0 },
NamedColor::Green => Color { r: 0, g: 255, b: 0 }, NamedColor::Green => Rgb { r: 0, g: 255, b: 0 },
NamedColor::Blue => Color { r: 0, g: 0, b: 255 }, NamedColor::Blue => Rgb { r: 0, g: 0, b: 255 },
NamedColor::White => Color { NamedColor::White => Rgb {
r: 255, r: 255,
g: 255, g: 255,
b: 255, b: 255,
}, },
NamedColor::Off => Color { r: 0, g: 0, b: 0 }, NamedColor::Off => Rgb { r: 0, g: 0, b: 0 },
NamedColor::Yellow => Color { NamedColor::Yellow => Rgb {
r: 255, r: 255,
g: 255, g: 255,
b: 0, b: 0,
}, },
NamedColor::Cyan => Color { NamedColor::Cyan => Rgb {
r: 0, r: 0,
g: 255, g: 255,
b: 255, b: 255,
}, },
NamedColor::Magenta => Color { NamedColor::Magenta => Rgb {
r: 255, r: 255,
g: 0, g: 0,
b: 255, b: 255,
@ -53,3 +42,15 @@ impl From<NamedColor> for Color {
} }
} }
} }
impl IntoIterator for NamedColor {
type Item = Self;
type IntoIter = std::vec::IntoIter<Self>;
fn into_iter(self) -> Self::IntoIter {
vec![self].into_iter()
}
}