use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Color {
StaticNamed(&'static str),
Named(String),
Rgb(u8, u8, u8, Option<u8>),
}
impl Color {
pub const fn new(name: &'static str) -> Self {
Self::StaticNamed(name)
}
pub fn rgb(r: u8, g: u8, b: u8, a: Option<u8>) -> Self {
Self::Rgb(r, g, b, a)
}
pub const RED: Color = Color::new("red");
pub const GREEN: Color = Color::new("green");
pub const BLUE: Color = Color::new("blue");
pub const CYAN: Color = Color::new("cyan");
pub const MAGENTA: Color = Color::new("magenta");
pub const YELLOW: Color = Color::new("yellow");
pub const BLACK: Color = Color::new("black");
pub const WHITE: Color = Color::new("white");
pub const GRAY: Color = Color::new("gray");
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Color::StaticNamed(name) => write!(f, "{}", name),
Color::Named(name) => write!(f, "{}", name),
Color::Rgb(r, g, b, a) => {
let components = [r, g, b]
.into_iter()
.map(|c| format!("{:02x}", c))
.chain(a.map(|a| format!("{:02x}", a)))
.collect::<String>();
write!(f, "#{}", components)
}
}
}
}
impl<T: Into<String>> From<T> for Color {
fn from(value: T) -> Self {
Color::Named(value.into())
}
}