use crate::{
ColorFormattable,
models::{
RGB,
HSL,
HSV,
HEX
},
utils,
error::{
ColorError,
ColorConversionError
}
};
#[derive(Debug, Clone, PartialEq)]
pub enum CustomColor {
Rgb(RGB),
Hsl(HSL),
Hsv(HSV),
Hex(HEX)
}
impl CustomColor {
fn to_rgb(&self) -> Result<RGB, ColorError> {
match self {
CustomColor::Rgb(rgb) => Ok(*rgb),
CustomColor::Hex(hex) => utils::hex_to_rgb(hex).map_err(|e: ColorConversionError| e.into()),
CustomColor::Hsl(hsl) => utils::hsl_to_rgb(hsl).map_err(|e: ColorConversionError| e.into()),
CustomColor::Hsv(hsv) => utils::hsv_to_rgb(hsv).map_err(|e: ColorConversionError| e.into()),
}
}
}
impl ColorFormattable for CustomColor {
fn format(&self, is_background: bool) -> String {
let rgb: RGB = self.to_rgb().unwrap();
if is_background {
format!("\x1B[48;2;{};{};{}m", rgb.r, rgb.g, rgb.b)
} else {
format!("\x1B[38;2;{};{};{}m", rgb.r, rgb.g, rgb.b)
}
}
}
impl From<RGB> for CustomColor {
fn from(rgb: RGB) -> Self {
CustomColor::Rgb(rgb)
}
}
impl From<HSL> for CustomColor {
fn from(hsl: HSL) -> Self {
CustomColor::Hsl(hsl)
}
}
impl From<HSV> for CustomColor {
fn from(hsv: HSV) -> Self {
CustomColor::Hsv(hsv)
}
}
impl From<HEX> for CustomColor {
fn from(hex: HEX) -> Self {
CustomColor::Hex(hex)
}
}