use rocketsplash_formats::Rgb;
use crate::{parse_color, Error};
#[derive(Clone, Debug)]
pub enum Color {
Rgb(Rgb),
Hex(String),
}
impl Color {
pub fn hex(s: &str) -> Result<Self, Error> {
let rgb = parse_color(s)?;
Ok(Self::Rgb(rgb))
}
pub fn rgb(r: u8, g: u8, b: u8) -> Self {
Self::Rgb(Rgb { r, g, b })
}
pub fn to_rgb(&self) -> Result<Rgb, Error> {
match self {
Self::Rgb(rgb) => Ok(*rgb),
Self::Hex(hex) => parse_color(hex),
}
}
}
impl From<Rgb> for Color {
fn from(value: Rgb) -> Self {
Self::Rgb(value)
}
}
impl From<(u8, u8, u8)> for Color {
fn from(value: (u8, u8, u8)) -> Self {
Self::Rgb(Rgb {
r: value.0,
g: value.1,
b: value.2,
})
}
}
impl From<&str> for Color {
fn from(value: &str) -> Self {
Self::Hex(value.trim().to_string())
}
}