use std::fmt;
use crate::checked_value::impl_checked_value;
use crate::{
FiniteF64, InvalidCssNumber, PercentageChannel, UnitInterval,
number::{assert_finite, try_finite},
serialization::CssWriteTo,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CssColorName {
Transparent,
CurrentColor,
Black,
Silver,
Gray,
White,
Maroon,
Red,
Purple,
Fuchsia,
Green,
Lime,
Olive,
Yellow,
Navy,
Blue,
Teal,
Aqua,
}
impl CssColorName {
pub const fn as_str(self) -> &'static str {
match self {
Self::Transparent => "transparent",
Self::CurrentColor => "currentcolor",
Self::Black => "black",
Self::Silver => "silver",
Self::Gray => "gray",
Self::White => "white",
Self::Maroon => "maroon",
Self::Red => "red",
Self::Purple => "purple",
Self::Fuchsia => "fuchsia",
Self::Green => "green",
Self::Lime => "lime",
Self::Olive => "olive",
Self::Yellow => "yellow",
Self::Navy => "navy",
Self::Blue => "blue",
Self::Teal => "teal",
Self::Aqua => "aqua",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum CssColor {
Rgb(u8, u8, u8),
Rgba(u8, u8, u8, UnitInterval),
Hsl(FiniteF64, PercentageChannel, PercentageChannel),
Hsla(
FiniteF64,
PercentageChannel,
PercentageChannel,
UnitInterval,
),
Named(CssColorName),
}
impl CssWriteTo for CssColor {
#[allow(clippy::many_single_char_names)]
fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
match self {
Self::Rgb(r, g, b) => write!(w, "rgb({r}, {g}, {b})"),
Self::Rgba(r, g, b, a) => write!(w, "rgba({r}, {g}, {b}, {a})"),
Self::Hsl(h, s, l) => write!(w, "hsl({h}, {s}%, {l}%)"),
Self::Hsla(h, s, l, a) => write!(w, "hsla({h}, {s}%, {l}%, {a})"),
Self::Named(name) => w.write_str(name.as_str()),
}
}
}
pub const fn rgb(r: u8, g: u8, b: u8) -> CssColor {
CssColor::Rgb(r, g, b)
}
pub fn rgba(r: u8, g: u8, b: u8, a: f64) -> CssColor {
CssColor::Rgba(r, g, b, UnitInterval::new(a))
}
pub fn try_rgba(r: u8, g: u8, b: u8, a: f64) -> Result<CssColor, InvalidCssNumber> {
UnitInterval::try_new(a).map(|a| CssColor::Rgba(r, g, b, a))
}
pub fn hsl(h: impl Into<f64>, s: impl Into<f64>, l: impl Into<f64>) -> CssColor {
let h = h.into();
let s = s.into();
let l = l.into();
CssColor::Hsl(
assert_finite(h, "hsl hue"),
PercentageChannel::new(s),
PercentageChannel::new(l),
)
}
pub fn try_hsl(
h: impl Into<f64>,
s: impl Into<f64>,
l: impl Into<f64>,
) -> Result<CssColor, InvalidCssNumber> {
let h = try_finite(h.into(), "hsl hue")?;
let s = PercentageChannel::try_new(s.into())?;
let l = PercentageChannel::try_new(l.into())?;
Ok(CssColor::Hsl(h, s, l))
}
pub fn hsla(
h: impl Into<f64>,
s: impl Into<f64>,
l: impl Into<f64>,
a: impl Into<f64>,
) -> CssColor {
let h = h.into();
let s = s.into();
let l = l.into();
let a = a.into();
CssColor::Hsla(
assert_finite(h, "hsla hue"),
PercentageChannel::new(s),
PercentageChannel::new(l),
UnitInterval::new(a),
)
}
pub fn try_hsla(
h: impl Into<f64>,
s: impl Into<f64>,
l: impl Into<f64>,
a: impl Into<f64>,
) -> Result<CssColor, InvalidCssNumber> {
let h = try_finite(h.into(), "hsla hue")?;
let s = PercentageChannel::try_new(s.into())?;
let l = PercentageChannel::try_new(l.into())?;
let a = UnitInterval::try_new(a.into())?;
Ok(CssColor::Hsla(h, s, l, a))
}
impl_checked_value!(CssColor);