1use std::fmt;
2
3use crate::checked_value::impl_checked_value;
4use crate::{
5 FiniteF64, InvalidCssNumber, PercentageChannel, UnitInterval,
6 number::{assert_finite, try_finite},
7 serialization::CssWriteTo,
8};
9
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16pub enum CssColorName {
17 Transparent,
19 CurrentColor,
21 Black,
23 Silver,
25 Gray,
27 White,
29 Maroon,
31 Red,
33 Purple,
35 Fuchsia,
37 Green,
39 Lime,
41 Olive,
43 Yellow,
45 Navy,
47 Blue,
49 Teal,
51 Aqua,
53}
54
55impl CssColorName {
56 pub const fn as_str(self) -> &'static str {
58 match self {
59 Self::Transparent => "transparent",
60 Self::CurrentColor => "currentcolor",
61 Self::Black => "black",
62 Self::Silver => "silver",
63 Self::Gray => "gray",
64 Self::White => "white",
65 Self::Maroon => "maroon",
66 Self::Red => "red",
67 Self::Purple => "purple",
68 Self::Fuchsia => "fuchsia",
69 Self::Green => "green",
70 Self::Lime => "lime",
71 Self::Olive => "olive",
72 Self::Yellow => "yellow",
73 Self::Navy => "navy",
74 Self::Blue => "blue",
75 Self::Teal => "teal",
76 Self::Aqua => "aqua",
77 }
78 }
79}
80
81#[derive(Clone, Copy, Debug, PartialEq)]
85#[non_exhaustive]
86pub enum CssColor {
87 Rgb(u8, u8, u8),
89 Rgba(u8, u8, u8, UnitInterval),
91 Hsl(FiniteF64, PercentageChannel, PercentageChannel),
95 Hsla(
99 FiniteF64,
100 PercentageChannel,
101 PercentageChannel,
102 UnitInterval,
103 ),
104 Named(CssColorName),
106}
107
108impl CssWriteTo for CssColor {
109 #[allow(clippy::many_single_char_names)]
110 fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
111 match self {
112 Self::Rgb(r, g, b) => write!(w, "rgb({r}, {g}, {b})"),
113 Self::Rgba(r, g, b, a) => write!(w, "rgba({r}, {g}, {b}, {a})"),
114 Self::Hsl(h, s, l) => write!(w, "hsl({h}, {s}%, {l}%)"),
115 Self::Hsla(h, s, l, a) => write!(w, "hsla({h}, {s}%, {l}%, {a})"),
116 Self::Named(name) => w.write_str(name.as_str()),
117 }
118 }
119}
120
121pub const fn rgb(r: u8, g: u8, b: u8) -> CssColor {
123 CssColor::Rgb(r, g, b)
124}
125
126pub fn rgba(r: u8, g: u8, b: u8, a: f64) -> CssColor {
132 CssColor::Rgba(r, g, b, UnitInterval::new(a))
133}
134
135pub fn try_rgba(r: u8, g: u8, b: u8, a: f64) -> Result<CssColor, InvalidCssNumber> {
141 UnitInterval::try_new(a).map(|a| CssColor::Rgba(r, g, b, a))
142}
143
144pub fn hsl(h: impl Into<f64>, s: impl Into<f64>, l: impl Into<f64>) -> CssColor {
155 let h = h.into();
156 let s = s.into();
157 let l = l.into();
158 CssColor::Hsl(
159 assert_finite(h, "hsl hue"),
160 PercentageChannel::new(s),
161 PercentageChannel::new(l),
162 )
163}
164
165pub fn try_hsl(
172 h: impl Into<f64>,
173 s: impl Into<f64>,
174 l: impl Into<f64>,
175) -> Result<CssColor, InvalidCssNumber> {
176 let h = try_finite(h.into(), "hsl hue")?;
177 let s = PercentageChannel::try_new(s.into())?;
178 let l = PercentageChannel::try_new(l.into())?;
179 Ok(CssColor::Hsl(h, s, l))
180}
181
182pub fn hsla(
194 h: impl Into<f64>,
195 s: impl Into<f64>,
196 l: impl Into<f64>,
197 a: impl Into<f64>,
198) -> CssColor {
199 let h = h.into();
200 let s = s.into();
201 let l = l.into();
202 let a = a.into();
203 CssColor::Hsla(
204 assert_finite(h, "hsla hue"),
205 PercentageChannel::new(s),
206 PercentageChannel::new(l),
207 UnitInterval::new(a),
208 )
209}
210
211pub fn try_hsla(
218 h: impl Into<f64>,
219 s: impl Into<f64>,
220 l: impl Into<f64>,
221 a: impl Into<f64>,
222) -> Result<CssColor, InvalidCssNumber> {
223 let h = try_finite(h.into(), "hsla hue")?;
224 let s = PercentageChannel::try_new(s.into())?;
225 let l = PercentageChannel::try_new(l.into())?;
226 let a = UnitInterval::try_new(a.into())?;
227 Ok(CssColor::Hsla(h, s, l, a))
228}
229
230impl_checked_value!(CssColor);