use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum Color {
Hex(String),
Rgb(u8, u8, u8),
Rgba(u8, u8, u8, f32),
Named(String),
Transparent,
}
impl Color {
pub fn hex(hex: &str) -> Self {
let hex = hex.strip_prefix('#').unwrap_or(hex);
Color::Hex(hex.to_string())
}
pub fn rgb(r: u8, g: u8, b: u8) -> Self {
Color::Rgb(r, g, b)
}
pub fn rgba(r: u8, g: u8, b: u8, a: f32) -> Self {
Color::Rgba(r, g, b, a)
}
pub fn white() -> Self {
Color::Hex("ffffff".to_string())
}
pub fn black() -> Self {
Color::Hex("000000".to_string())
}
pub fn transparent() -> Self {
Color::Transparent
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Color::Hex(hex) => write!(f, "#{hex}"),
Color::Rgb(r, g, b) => write!(f, "rgb({r},{g},{b})"),
Color::Rgba(r, g, b, a) => write!(f, "rgba({r},{g},{b},{a})"),
Color::Named(name) => write!(f, "{name}"),
Color::Transparent => write!(f, "transparent"),
}
}
}