Skip to main content

slt/style/
color.rs

1/// Terminal color.
2///
3/// Covers the standard 16 named colors, 256-color palette indices, and
4/// 24-bit RGB true color. Use [`Color::Reset`] to restore the terminal's
5/// default foreground or background.
6#[non_exhaustive]
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub enum Color {
9    /// Reset to the terminal's default color.
10    Reset,
11    /// Standard black (color index 0).
12    Black,
13    /// Standard red (color index 1).
14    Red,
15    /// Standard green (color index 2).
16    Green,
17    /// Standard yellow (color index 3).
18    Yellow,
19    /// Standard blue (color index 4).
20    Blue,
21    /// Standard magenta (color index 5).
22    Magenta,
23    /// Standard cyan (color index 6).
24    Cyan,
25    /// Standard white (color index 7).
26    White,
27    /// Bright black / dark gray (color index 8).
28    DarkGray,
29    /// Bright red (color index 9).
30    LightRed,
31    /// Bright green (color index 10).
32    LightGreen,
33    /// Bright yellow (color index 11).
34    LightYellow,
35    /// Bright blue (color index 12).
36    LightBlue,
37    /// Bright magenta (color index 13).
38    LightMagenta,
39    /// Bright cyan (color index 14).
40    LightCyan,
41    /// Bright white (color index 15).
42    LightWhite,
43    /// 24-bit true color.
44    Rgb(u8, u8, u8),
45    /// 256-color palette index.
46    Indexed(u8),
47}
48
49#[inline]
50fn to_linear(c: f64) -> f64 {
51    if c <= 0.04045 {
52        c / 12.92
53    } else {
54        ((c + 0.055) / 1.055).powf(2.4)
55    }
56}
57
58impl Color {
59    /// Resolve to `(r, g, b)` for luminance and blending operations.
60    ///
61    /// Named colors map to their typical terminal palette values.
62    /// [`Color::Reset`] maps to black; [`Color::Indexed`] maps to the xterm-256 palette.
63    pub(crate) fn to_rgb(self) -> (u8, u8, u8) {
64        match self {
65            Color::Rgb(r, g, b) => (r, g, b),
66            Color::Black => (0, 0, 0),
67            Color::Red => (205, 49, 49),
68            Color::Green => (13, 188, 121),
69            Color::Yellow => (229, 229, 16),
70            Color::Blue => (36, 114, 200),
71            Color::Magenta => (188, 63, 188),
72            Color::Cyan => (17, 168, 205),
73            Color::White => (229, 229, 229),
74            Color::DarkGray => (128, 128, 128),
75            Color::LightRed => (255, 0, 0),
76            Color::LightGreen => (0, 255, 0),
77            Color::LightYellow => (255, 255, 0),
78            Color::LightBlue => (0, 0, 255),
79            Color::LightMagenta => (255, 0, 255),
80            Color::LightCyan => (0, 255, 255),
81            Color::LightWhite => (255, 255, 255),
82            Color::Reset => (0, 0, 0),
83            Color::Indexed(idx) => xterm256_to_rgb(idx),
84        }
85    }
86
87    /// Compute relative luminance using ITU-R BT.709 coefficients.
88    ///
89    /// Returns a value in `[0.0, 1.0]` where 0 is darkest and 1 is brightest.
90    /// Use this to determine whether text on a given background should be
91    /// light or dark.
92    ///
93    /// # Example
94    ///
95    /// ```
96    /// use slt::Color;
97    ///
98    /// let dark = Color::Rgb(30, 30, 46);
99    /// assert!(dark.luminance_f64() < 0.15);
100    ///
101    /// let light = Color::Rgb(205, 214, 244);
102    /// assert!(light.luminance_f64() > 0.6);
103    /// ```
104    pub fn luminance_f64(self) -> f64 {
105        let (r, g, b) = self.to_rgb();
106        let rf = to_linear(f64::from(r) / 255.0);
107        let gf = to_linear(f64::from(g) / 255.0);
108        let bf = to_linear(f64::from(b) / 255.0);
109        0.2126 * rf + 0.7152 * gf + 0.0722 * bf
110    }
111
112    /// Deprecated `f32` alias for [`luminance_f64`](Self::luminance_f64).
113    #[deprecated(
114        since = "0.22.2",
115        note = "use Color::luminance_f64() to keep public float APIs on f64"
116    )]
117    pub fn luminance(self) -> f32 {
118        self.luminance_f64() as f32
119    }
120
121    /// Return a contrasting foreground color for the given background.
122    ///
123    /// Uses the WCAG 2.1 relative luminance threshold (0.179) to decide
124    /// between white and black text. For theme-aware contrast, prefer using
125    /// this over hardcoding `theme.bg` as the foreground.
126    ///
127    /// # Example
128    ///
129    /// ```
130    /// use slt::Color;
131    ///
132    /// let bg = Color::Rgb(189, 147, 249); // Dracula purple
133    /// let fg = Color::contrast_fg(bg);
134    /// // Dracula purple → white (WCAG luminance 0.385 < 0.179 threshold)
135    /// ```
136    pub fn contrast_fg(bg: Color) -> Color {
137        if bg.luminance_f64() > 0.179 {
138            Color::Rgb(0, 0, 0)
139        } else {
140            Color::Rgb(255, 255, 255)
141        }
142    }
143
144    /// Blend this color over another with the given alpha.
145    ///
146    /// `alpha` is in `[0.0, 1.0]` where 0.0 returns `other` unchanged and
147    /// 1.0 returns `self` unchanged. Both colors are resolved to RGB.
148    ///
149    /// # Example
150    ///
151    /// ```
152    /// use slt::Color;
153    ///
154    /// let white = Color::Rgb(255, 255, 255);
155    /// let black = Color::Rgb(0, 0, 0);
156    /// let gray = white.blend_f64(black, 0.5);
157    /// // ≈ Rgb(128, 128, 128)
158    /// ```
159    pub fn blend_f64(self, other: Color, alpha: f64) -> Color {
160        let alpha = alpha.clamp(0.0, 1.0);
161        let (r1, g1, b1) = self.to_rgb();
162        let (r2, g2, b2) = other.to_rgb();
163        let r = (f64::from(r1) * alpha + f64::from(r2) * (1.0 - alpha)).round() as u8;
164        let g = (f64::from(g1) * alpha + f64::from(g2) * (1.0 - alpha)).round() as u8;
165        let b = (f64::from(b1) * alpha + f64::from(b2) * (1.0 - alpha)).round() as u8;
166        Color::Rgb(r, g, b)
167    }
168
169    /// Deprecated `f32` alias for [`blend_f64`](Self::blend_f64).
170    #[deprecated(
171        since = "0.22.2",
172        note = "use Color::blend_f64() to keep public float APIs on f64"
173    )]
174    pub fn blend(self, other: Color, alpha: f32) -> Color {
175        self.blend_f64(other, f64::from(alpha))
176    }
177
178    /// Lighten this color by the given amount (0.0–1.0).
179    ///
180    /// Blends toward white. `amount = 0.0` returns the original color;
181    /// `amount = 1.0` returns white.
182    pub fn lighten_f64(self, amount: f64) -> Color {
183        Color::Rgb(255, 255, 255).blend_f64(self, 1.0 - amount.clamp(0.0, 1.0))
184    }
185
186    /// Deprecated `f32` alias for [`lighten_f64`](Self::lighten_f64).
187    #[deprecated(
188        since = "0.22.2",
189        note = "use Color::lighten_f64() to keep public float APIs on f64"
190    )]
191    pub fn lighten(self, amount: f32) -> Color {
192        self.lighten_f64(f64::from(amount))
193    }
194
195    /// Darken this color by the given amount (0.0–1.0).
196    ///
197    /// Blends toward black. `amount = 0.0` returns the original color;
198    /// `amount = 1.0` returns black.
199    pub fn darken_f64(self, amount: f64) -> Color {
200        Color::Rgb(0, 0, 0).blend_f64(self, 1.0 - amount.clamp(0.0, 1.0))
201    }
202
203    /// Deprecated `f32` alias for [`darken_f64`](Self::darken_f64).
204    #[deprecated(
205        since = "0.22.2",
206        note = "use Color::darken_f64() to keep public float APIs on f64"
207    )]
208    pub fn darken(self, amount: f32) -> Color {
209        self.darken_f64(f64::from(amount))
210    }
211
212    /// Compute the WCAG 2.1 contrast ratio between two colors.
213    ///
214    /// Returns a value >= 1.0. A ratio >= 4.5 meets WCAG AA for normal text;
215    /// >= 3.0 meets AA for large text.
216    ///
217    /// # Example
218    ///
219    /// ```
220    /// use slt::Color;
221    ///
222    /// let ratio = Color::contrast_ratio_f64(Color::White, Color::Black);
223    /// assert!(ratio > 15.0);
224    /// ```
225    pub fn contrast_ratio_f64(a: Color, b: Color) -> f64 {
226        let la = a.luminance_f64() + 0.05;
227        let lb = b.luminance_f64() + 0.05;
228        if la > lb { la / lb } else { lb / la }
229    }
230
231    /// Deprecated `f32` alias for [`contrast_ratio_f64`](Self::contrast_ratio_f64).
232    #[deprecated(
233        since = "0.22.2",
234        note = "use Color::contrast_ratio_f64() to keep public float APIs on f64"
235    )]
236    pub fn contrast_ratio(a: Color, b: Color) -> f32 {
237        Self::contrast_ratio_f64(a, b) as f32
238    }
239
240    /// Returns `true` if the contrast ratio between two colors meets WCAG AA
241    /// for normal text (ratio >= 4.5).
242    pub fn meets_contrast_aa(fg: Color, bg: Color) -> bool {
243        Self::contrast_ratio_f64(fg, bg) >= 4.5
244    }
245
246    /// Downsample this color to fit the given color depth.
247    ///
248    /// - `TrueColor`: returns self unchanged.
249    /// - `EightBit`: converts `Rgb` to the nearest `Indexed` color.
250    /// - `Basic`: converts `Rgb` and `Indexed` to the nearest named color.
251    /// - `NoColor`: returns [`Color::Reset`] — emit no ANSI color at all.
252    ///
253    /// Named colors (`Red`, `Green`, etc.) and `Reset` pass through at
254    /// depths other than `NoColor`.
255    pub fn downsampled(self, depth: ColorDepth) -> Color {
256        match depth {
257            ColorDepth::TrueColor => self,
258            ColorDepth::EightBit => match self {
259                Color::Rgb(r, g, b) => Color::Indexed(rgb_to_ansi256(r, g, b)),
260                other => other,
261            },
262            ColorDepth::Basic => match self {
263                Color::Rgb(r, g, b) => rgb_to_ansi16(r, g, b),
264                Color::Indexed(i) => {
265                    let (r, g, b) = xterm256_to_rgb(i);
266                    rgb_to_ansi16(r, g, b)
267                }
268                other => other,
269            },
270            ColorDepth::NoColor => Color::Reset,
271        }
272    }
273
274    /// Parse a hex string (`#rgb` or `#rrggbb`) into [`Color::Rgb`].
275    ///
276    /// The leading `#` is required. Short form `#rgb` expands each nibble
277    /// (`#abc` → `Rgb(0xaa, 0xbb, 0xcc)`). Returns `None` for any malformed
278    /// input (wrong length, non-hex digits, missing `#`).
279    ///
280    /// # Example
281    ///
282    /// ```
283    /// use slt::Color;
284    ///
285    /// assert_eq!(Color::from_hex("#ff6b6b"), Some(Color::Rgb(255, 107, 107)));
286    /// assert_eq!(Color::from_hex("#abc"), Some(Color::Rgb(170, 187, 204)));
287    /// assert_eq!(Color::from_hex("ff6b6b"), None); // missing '#'
288    /// assert_eq!(Color::from_hex("#xyz"), None); // non-hex
289    /// ```
290    #[doc(alias = "parse")]
291    pub fn from_hex(s: &str) -> Option<Color> {
292        let hex = s.strip_prefix('#')?;
293        match hex.len() {
294            3 => {
295                let mut it = hex.chars().map(|c| c.to_digit(16));
296                let r = it.next()??;
297                let g = it.next()??;
298                let b = it.next()??;
299                // Expand each nibble: 0xa -> 0xaa.
300                Some(Color::Rgb((r * 17) as u8, (g * 17) as u8, (b * 17) as u8))
301            }
302            6 => {
303                let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
304                let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
305                let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
306                Some(Color::Rgb(r, g, b))
307            }
308            _ => None,
309        }
310    }
311
312    /// Format an `Rgb` color as a `#rrggbb` hex string.
313    ///
314    /// Non-`Rgb` variants are first resolved to their RGB equivalent via the
315    /// internal palette, so the result is always a valid `#rrggbb` token.
316    ///
317    /// # Example
318    ///
319    /// ```
320    /// use slt::Color;
321    ///
322    /// assert_eq!(Color::Rgb(255, 107, 107).to_hex(), "#ff6b6b");
323    /// ```
324    pub fn to_hex(self) -> String {
325        let (r, g, b) = self.to_rgb();
326        format!("#{r:02x}{g:02x}{b:02x}")
327    }
328
329    /// Construct an [`Color::Rgb`] from HSL components.
330    ///
331    /// `h` is the hue in degrees (wrapped into `0..360`), `s` is the
332    /// saturation and `l` the lightness, both clamped to `[0.0, 1.0]`.
333    ///
334    /// # Example
335    ///
336    /// ```
337    /// use slt::Color;
338    ///
339    /// assert_eq!(Color::from_hsl_f64(0.0, 1.0, 0.5), Color::Rgb(255, 0, 0));
340    /// assert_eq!(Color::from_hsl_f64(120.0, 1.0, 0.5), Color::Rgb(0, 255, 0));
341    /// assert_eq!(Color::from_hsl_f64(240.0, 1.0, 0.5), Color::Rgb(0, 0, 255));
342    /// ```
343    pub fn from_hsl_f64(h: f64, s: f64, l: f64) -> Color {
344        let (r, g, b) = hsl_to_rgb(h, s.clamp(0.0, 1.0), l.clamp(0.0, 1.0));
345        Color::Rgb(r, g, b)
346    }
347
348    /// Deprecated `f32` alias for [`from_hsl_f64`](Self::from_hsl_f64).
349    #[deprecated(
350        since = "0.22.2",
351        note = "use Color::from_hsl_f64() to keep public float APIs on f64"
352    )]
353    pub fn from_hsl(h: f32, s: f32, l: f32) -> Color {
354        Self::from_hsl_f64(f64::from(h), f64::from(s), f64::from(l))
355    }
356
357    /// Construct an [`Color::Rgb`] from HSV (a.k.a. HSB) components.
358    ///
359    /// `h` is the hue in degrees (wrapped into `0..360`), `s` is the
360    /// saturation and `v` the value/brightness, both clamped to `[0.0, 1.0]`.
361    ///
362    /// # Example
363    ///
364    /// ```
365    /// use slt::Color;
366    ///
367    /// assert_eq!(Color::from_hsv_f64(0.0, 1.0, 1.0), Color::Rgb(255, 0, 0));
368    /// assert_eq!(Color::from_hsv_f64(120.0, 1.0, 1.0), Color::Rgb(0, 255, 0));
369    /// assert_eq!(Color::from_hsv_f64(0.0, 0.0, 1.0), Color::Rgb(255, 255, 255));
370    /// ```
371    pub fn from_hsv_f64(h: f64, s: f64, v: f64) -> Color {
372        let (r, g, b) = hsv_to_rgb(h, s.clamp(0.0, 1.0), v.clamp(0.0, 1.0));
373        Color::Rgb(r, g, b)
374    }
375
376    /// Deprecated `f32` alias for [`from_hsv_f64`](Self::from_hsv_f64).
377    #[deprecated(
378        since = "0.22.2",
379        note = "use Color::from_hsv_f64() to keep public float APIs on f64"
380    )]
381    pub fn from_hsv(h: f32, s: f32, v: f32) -> Color {
382        Self::from_hsv_f64(f64::from(h), f64::from(s), f64::from(v))
383    }
384
385    /// Rotate the hue of this color by `degrees` around the HSL color wheel.
386    ///
387    /// The color is resolved to RGB, converted to HSL, rotated, and converted
388    /// back to [`Color::Rgb`]. Positive values rotate forward (red → green →
389    /// blue); negative values rotate backward. The result is always an
390    /// `Rgb` color regardless of the input variant — named and indexed colors
391    /// are first resolved via the internal palette.
392    ///
393    /// # Example
394    ///
395    /// ```
396    /// use slt::Color;
397    ///
398    /// // Rotating pure red by 120° lands on pure green.
399    /// assert_eq!(Color::Rgb(255, 0, 0).rotate_hue_f64(120.0), Color::Rgb(0, 255, 0));
400    /// ```
401    pub fn rotate_hue_f64(self, degrees: f64) -> Color {
402        let (r, g, b) = self.to_rgb();
403        let (h, s, l) = rgb_to_hsl(r, g, b);
404        let (nr, ng, nb) = hsl_to_rgb(h + degrees, s, l);
405        Color::Rgb(nr, ng, nb)
406    }
407
408    /// Deprecated `f32` alias for [`rotate_hue_f64`](Self::rotate_hue_f64).
409    #[deprecated(
410        since = "0.22.2",
411        note = "use Color::rotate_hue_f64() to keep public float APIs on f64"
412    )]
413    pub fn rotate_hue(self, degrees: f32) -> Color {
414        self.rotate_hue_f64(f64::from(degrees))
415    }
416}
417
418impl From<(u8, u8, u8)> for Color {
419    /// Construct an [`Color::Rgb`] from an `(r, g, b)` tuple.
420    fn from((r, g, b): (u8, u8, u8)) -> Color {
421        Color::Rgb(r, g, b)
422    }
423}
424
425impl From<[u8; 3]> for Color {
426    /// Construct an [`Color::Rgb`] from an `[r, g, b]` array.
427    fn from([r, g, b]: [u8; 3]) -> Color {
428        Color::Rgb(r, g, b)
429    }
430}
431
432impl From<u32> for Color {
433    /// Construct an [`Color::Rgb`] from a packed `0xRRGGBB` integer.
434    ///
435    /// The high byte (alpha / `0xAA______`) is ignored.
436    ///
437    /// # Example
438    ///
439    /// ```
440    /// use slt::Color;
441    ///
442    /// assert_eq!(Color::from(0xff6b6b), Color::Rgb(255, 107, 107));
443    /// ```
444    fn from(value: u32) -> Color {
445        let r = ((value >> 16) & 0xff) as u8;
446        let g = ((value >> 8) & 0xff) as u8;
447        let b = (value & 0xff) as u8;
448        Color::Rgb(r, g, b)
449    }
450}
451
452/// Error returned when [`Color`] fails to parse from a string.
453///
454/// Produced by the [`std::str::FromStr`] implementation for [`Color`].
455///
456/// # Example
457///
458/// ```
459/// use slt::Color;
460///
461/// let err = "#zz0011".parse::<Color>().unwrap_err();
462/// // Display renders a human-readable reason.
463/// assert!(err.to_string().contains("non-hex digit"));
464/// ```
465#[non_exhaustive]
466#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
467pub enum ColorParseError {
468    /// The input had a hex form (`#…` or all hex-looking) but the wrong
469    /// number of digits (only 3 or 6 are accepted).
470    InvalidLength,
471    /// The input contained a character that is not a valid hex digit.
472    InvalidHexDigit,
473    /// The input did not match any known hex form or named color.
474    Unknown,
475}
476
477impl std::fmt::Display for ColorParseError {
478    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
479        let msg = match self {
480            ColorParseError::InvalidLength => "invalid color: hex form must have 3 or 6 digits",
481            ColorParseError::InvalidHexDigit => "invalid color: non-hex digit in hex form",
482            ColorParseError::Unknown => {
483                "invalid color: expected #rgb/#rrggbb, rrggbb, or a named color"
484            }
485        };
486        f.write_str(msg)
487    }
488}
489
490impl core::error::Error for ColorParseError {}
491
492impl std::str::FromStr for Color {
493    type Err = ColorParseError;
494
495    /// Parse a color from a string.
496    ///
497    /// Accepts hex (`#rgb`, `#rrggbb`, or bare `rrggbb` / `rgb` without the
498    /// leading `#`) and case-insensitive named colors (`"red"`, `"lightblue"`,
499    /// `"darkgray"`, `"reset"`, …).
500    ///
501    /// # Errors
502    ///
503    /// Returns [`ColorParseError`] when the input matches no known form:
504    /// [`ColorParseError::InvalidLength`] for a hex token of the wrong
505    /// length, [`ColorParseError::InvalidHexDigit`] for non-hex digits in a
506    /// `#`-prefixed token, and [`ColorParseError::Unknown`] otherwise.
507    ///
508    /// # Example
509    ///
510    /// ```
511    /// use slt::Color;
512    ///
513    /// assert_eq!("#ff6b6b".parse::<Color>(), Ok(Color::Rgb(255, 107, 107)));
514    /// assert_eq!("ff6b6b".parse::<Color>(), Ok(Color::Rgb(255, 107, 107)));
515    /// assert_eq!("#abc".parse::<Color>(), Ok(Color::Rgb(170, 187, 204)));
516    /// assert_eq!("cyan".parse::<Color>(), Ok(Color::Cyan));
517    /// assert!("nope".parse::<Color>().is_err());
518    /// ```
519    fn from_str(s: &str) -> Result<Color, ColorParseError> {
520        let trimmed = s.trim();
521
522        // Named colors take priority over the no-`#` hex path so that a name
523        // like "red" is never mistaken for a hex token.
524        if let Some(c) = named_color(trimmed) {
525            return Ok(c);
526        }
527
528        let had_hash = trimmed.starts_with('#');
529        let hex = trimmed.strip_prefix('#').unwrap_or(trimmed);
530
531        match hex.len() {
532            3 => {
533                let mut it = hex.chars().map(|c| c.to_digit(16));
534                let r = it
535                    .next()
536                    .flatten()
537                    .ok_or(ColorParseError::InvalidHexDigit)?;
538                let g = it
539                    .next()
540                    .flatten()
541                    .ok_or(ColorParseError::InvalidHexDigit)?;
542                let b = it
543                    .next()
544                    .flatten()
545                    .ok_or(ColorParseError::InvalidHexDigit)?;
546                Ok(Color::Rgb((r * 17) as u8, (g * 17) as u8, (b * 17) as u8))
547            }
548            6 => {
549                let r = u8::from_str_radix(&hex[0..2], 16)
550                    .map_err(|_| ColorParseError::InvalidHexDigit)?;
551                let g = u8::from_str_radix(&hex[2..4], 16)
552                    .map_err(|_| ColorParseError::InvalidHexDigit)?;
553                let b = u8::from_str_radix(&hex[4..6], 16)
554                    .map_err(|_| ColorParseError::InvalidHexDigit)?;
555                Ok(Color::Rgb(r, g, b))
556            }
557            // A `#`-prefixed token that isn't 3 or 6 digits is clearly a
558            // malformed hex token; an unprefixed token of an odd length is
559            // simply an unknown name.
560            _ if had_hash => Err(ColorParseError::InvalidLength),
561            _ => Err(ColorParseError::Unknown),
562        }
563    }
564}
565
566/// Resolve a case-insensitive named color token (no `#`, no `indexed:`).
567///
568/// Returns `None` for anything that is not one of the 16 standard names plus
569/// the common aliases (`grey`, `default`).
570fn named_color(s: &str) -> Option<Color> {
571    let lower = s.to_ascii_lowercase();
572    Some(match lower.as_str() {
573        "reset" | "default" => Color::Reset,
574        "black" => Color::Black,
575        "red" => Color::Red,
576        "green" => Color::Green,
577        "yellow" => Color::Yellow,
578        "blue" => Color::Blue,
579        "magenta" => Color::Magenta,
580        "cyan" => Color::Cyan,
581        "white" => Color::White,
582        "darkgray" | "darkgrey" | "gray" | "grey" => Color::DarkGray,
583        "lightred" => Color::LightRed,
584        "lightgreen" => Color::LightGreen,
585        "lightyellow" => Color::LightYellow,
586        "lightblue" => Color::LightBlue,
587        "lightmagenta" => Color::LightMagenta,
588        "lightcyan" => Color::LightCyan,
589        "lightwhite" => Color::LightWhite,
590        _ => return None,
591    })
592}
593
594/// Convert HSL (`h` in degrees, `s`/`l` in `[0.0, 1.0]`) to `(r, g, b)`.
595///
596/// The hue is wrapped into `0..360`. Inputs are assumed already clamped by
597/// the caller.
598fn hsl_to_rgb(h: f64, s: f64, l: f64) -> (u8, u8, u8) {
599    let h = wrap_hue(h);
600    let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
601    let x = c * (1.0 - (((h / 60.0) % 2.0) - 1.0).abs());
602    let m = l - c / 2.0;
603    let (r1, g1, b1) = hue_sextant(h, c, x);
604    (
605        round_channel(r1 + m),
606        round_channel(g1 + m),
607        round_channel(b1 + m),
608    )
609}
610
611/// Convert HSV (`h` in degrees, `s`/`v` in `[0.0, 1.0]`) to `(r, g, b)`.
612///
613/// The hue is wrapped into `0..360`. Inputs are assumed already clamped by
614/// the caller.
615fn hsv_to_rgb(h: f64, s: f64, v: f64) -> (u8, u8, u8) {
616    let h = wrap_hue(h);
617    let c = v * s;
618    let x = c * (1.0 - (((h / 60.0) % 2.0) - 1.0).abs());
619    let m = v - c;
620    let (r1, g1, b1) = hue_sextant(h, c, x);
621    (
622        round_channel(r1 + m),
623        round_channel(g1 + m),
624        round_channel(b1 + m),
625    )
626}
627
628/// Convert `(r, g, b)` to HSL with `h` in degrees `[0, 360)` and `s`/`l` in
629/// `[0.0, 1.0]`.
630fn rgb_to_hsl(r: u8, g: u8, b: u8) -> (f64, f64, f64) {
631    let rf = f64::from(r) / 255.0;
632    let gf = f64::from(g) / 255.0;
633    let bf = f64::from(b) / 255.0;
634    let max = rf.max(gf).max(bf);
635    let min = rf.min(gf).min(bf);
636    let delta = max - min;
637    let l = (max + min) / 2.0;
638
639    if delta <= f64::EPSILON {
640        // Achromatic: hue is undefined, conventionally 0.
641        return (0.0, 0.0, l);
642    }
643
644    let s = if l > 0.5 {
645        delta / (2.0 - max - min)
646    } else {
647        delta / (max + min)
648    };
649
650    let h = if max == rf {
651        let h = (gf - bf) / delta;
652        h % 6.0
653    } else if max == gf {
654        (bf - rf) / delta + 2.0
655    } else {
656        (rf - gf) / delta + 4.0
657    } * 60.0;
658
659    (wrap_hue(h), s, l)
660}
661
662/// Map a hue (already wrapped into `0..360`) and chroma components onto the
663/// six RGB sextants, returning the un-offset `(r, g, b)` floats.
664#[inline]
665fn hue_sextant(h: f64, c: f64, x: f64) -> (f64, f64, f64) {
666    match h {
667        h if h < 60.0 => (c, x, 0.0),
668        h if h < 120.0 => (x, c, 0.0),
669        h if h < 180.0 => (0.0, c, x),
670        h if h < 240.0 => (0.0, x, c),
671        h if h < 300.0 => (x, 0.0, c),
672        _ => (c, 0.0, x),
673    }
674}
675
676/// Wrap a hue in degrees into the half-open range `[0.0, 360.0)`.
677#[inline]
678fn wrap_hue(h: f64) -> f64 {
679    let h = h % 360.0;
680    if h < 0.0 { h + 360.0 } else { h }
681}
682
683/// Scale a `[0.0, 1.0]` channel to a rounded, clamped `u8`.
684#[inline]
685fn round_channel(v: f64) -> u8 {
686    (v * 255.0).round().clamp(0.0, 255.0) as u8
687}
688
689#[cfg(feature = "serde")]
690impl Color {
691    /// Serialized token for a named color, or `None` for non-named variants.
692    fn named_token(self) -> Option<&'static str> {
693        Some(match self {
694            Color::Reset => "reset",
695            Color::Black => "black",
696            Color::Red => "red",
697            Color::Green => "green",
698            Color::Yellow => "yellow",
699            Color::Blue => "blue",
700            Color::Magenta => "magenta",
701            Color::Cyan => "cyan",
702            Color::White => "white",
703            Color::DarkGray => "darkgray",
704            Color::LightRed => "lightred",
705            Color::LightGreen => "lightgreen",
706            Color::LightYellow => "lightyellow",
707            Color::LightBlue => "lightblue",
708            Color::LightMagenta => "lightmagenta",
709            Color::LightCyan => "lightcyan",
710            Color::LightWhite => "lightwhite",
711            Color::Rgb(..) | Color::Indexed(_) => return None,
712        })
713    }
714
715    /// Parse a color from a human-friendly token used in theme files.
716    ///
717    /// Accepts `#rgb` / `#rrggbb` hex, named colors (case-insensitive, e.g.
718    /// `"cyan"`, `"lightblue"`, `"darkgray"`, `"reset"`), and `indexed:N`
719    /// palette indices (`0..=255`).
720    fn from_token(s: &str) -> Option<Color> {
721        if let Some(c) = Color::from_hex(s) {
722            return Some(c);
723        }
724        let lower = s.trim().to_ascii_lowercase();
725        if let Some(rest) = lower.strip_prefix("indexed:") {
726            return rest.trim().parse::<u8>().ok().map(Color::Indexed);
727        }
728        Some(match lower.as_str() {
729            "reset" | "default" => Color::Reset,
730            "black" => Color::Black,
731            "red" => Color::Red,
732            "green" => Color::Green,
733            "yellow" => Color::Yellow,
734            "blue" => Color::Blue,
735            "magenta" => Color::Magenta,
736            "cyan" => Color::Cyan,
737            "white" => Color::White,
738            "darkgray" | "darkgrey" | "gray" | "grey" => Color::DarkGray,
739            "lightred" => Color::LightRed,
740            "lightgreen" => Color::LightGreen,
741            "lightyellow" => Color::LightYellow,
742            "lightblue" => Color::LightBlue,
743            "lightmagenta" => Color::LightMagenta,
744            "lightcyan" => Color::LightCyan,
745            "lightwhite" => Color::LightWhite,
746            _ => return None,
747        })
748    }
749
750    /// The canonical serialized token for this color.
751    ///
752    /// Named colors emit their lowercase name, `Rgb` emits `#rrggbb`,
753    /// `Indexed(n)` emits `indexed:n`. This is the inverse of [`Color::from_token`].
754    fn to_token(self) -> String {
755        if let Some(name) = self.named_token() {
756            return name.to_string();
757        }
758        match self {
759            Color::Indexed(n) => format!("indexed:{n}"),
760            // `Rgb` and any other true-color variant.
761            other => other.to_hex(),
762        }
763    }
764}
765
766#[cfg(feature = "serde")]
767impl serde::Serialize for Color {
768    /// Serialize as a human-friendly string token (`#rrggbb`, a named color,
769    /// or `indexed:N`) so theme files stay hand-editable and round-trip.
770    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
771    where
772        S: serde::Serializer,
773    {
774        serializer.serialize_str(&self.to_token())
775    }
776}
777
778#[cfg(feature = "serde")]
779impl<'de> serde::Deserialize<'de> for Color {
780    /// Deserialize from a token string: `#rgb`/`#rrggbb`, a named color
781    /// (case-insensitive), or `indexed:N`.
782    fn deserialize<D>(deserializer: D) -> Result<Color, D::Error>
783    where
784        D: serde::Deserializer<'de>,
785    {
786        struct ColorVisitor;
787
788        impl serde::de::Visitor<'_> for ColorVisitor {
789            type Value = Color;
790
791            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
792                f.write_str("a color token like \"#ff6b6b\", \"cyan\", or \"indexed:245\"")
793            }
794
795            fn visit_str<E>(self, value: &str) -> Result<Color, E>
796            where
797                E: serde::de::Error,
798            {
799                Color::from_token(value).ok_or_else(|| {
800                    E::custom(format!(
801                        "invalid color token {value:?}: expected #rgb/#rrggbb, a named color, or indexed:N"
802                    ))
803                })
804            }
805        }
806
807        deserializer.deserialize_str(ColorVisitor)
808    }
809}
810
811/// Terminal color depth capability.
812///
813/// Determines the maximum number of colors a terminal can display.
814/// Use [`ColorDepth::detect`] for automatic detection via environment
815/// variables, or specify explicitly in [`crate::RunConfig`].
816#[non_exhaustive]
817#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
818#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
819pub enum ColorDepth {
820    /// 24-bit true color (16 million colors).
821    TrueColor,
822    /// 256-color palette (xterm-256color).
823    EightBit,
824    /// 16 basic ANSI colors.
825    Basic,
826    /// No color output — every color is downsampled to [`Color::Reset`] and
827    /// the terminal emits no SGR color codes. Selected automatically by
828    /// [`ColorDepth::detect`] when the `NO_COLOR` environment variable is
829    /// set to any non-empty value, per <https://no-color.org>.
830    NoColor,
831}
832
833#[cfg(test)]
834mod color_depth_tests {
835    use super::{Color, ColorDepth};
836
837    #[test]
838    fn no_color_downsamples_everything_to_reset() {
839        assert_eq!(Color::Red.downsampled(ColorDepth::NoColor), Color::Reset);
840        assert_eq!(
841            Color::Rgb(10, 20, 30).downsampled(ColorDepth::NoColor),
842            Color::Reset
843        );
844        assert_eq!(
845            Color::Indexed(44).downsampled(ColorDepth::NoColor),
846            Color::Reset
847        );
848    }
849}
850
851impl ColorDepth {
852    /// Detect the terminal's color depth from environment variables.
853    ///
854    /// Order of precedence:
855    /// 1. `NO_COLOR` (any non-empty value) → [`ColorDepth::NoColor`]
856    /// 2. `COLORTERM=truecolor|24bit` → [`ColorDepth::TrueColor`]
857    /// 3. `TERM` contains `256color` → [`ColorDepth::EightBit`]
858    /// 4. Fallback → [`ColorDepth::Basic`] (16 colors)
859    pub fn detect() -> Self {
860        // https://no-color.org — ANY non-empty value disables color.
861        if std::env::var("NO_COLOR")
862            .ok()
863            .is_some_and(|v| !v.is_empty())
864        {
865            return Self::NoColor;
866        }
867        if let Ok(ct) = std::env::var("COLORTERM") {
868            let ct = ct.to_lowercase();
869            if ct == "truecolor" || ct == "24bit" {
870                return Self::TrueColor;
871            }
872        }
873        if let Ok(term) = std::env::var("TERM")
874            && term.contains("256color")
875        {
876            return Self::EightBit;
877        }
878        Self::Basic
879    }
880}
881
882fn rgb_to_ansi256(r: u8, g: u8, b: u8) -> u8 {
883    if r == g && g == b {
884        if r < 8 {
885            return 16;
886        }
887        if r >= 248 {
888            return 231;
889        }
890        return 232 + (((r as u16 - 8) * 24 / 240) as u8);
891    }
892
893    let ri = if r < 48 {
894        0
895    } else {
896        ((r as u16 - 35) / 40) as u8
897    };
898    let gi = if g < 48 {
899        0
900    } else {
901        ((g as u16 - 35) / 40) as u8
902    };
903    let bi = if b < 48 {
904        0
905    } else {
906        ((b as u16 - 35) / 40) as u8
907    };
908    16 + 36 * ri.min(5) + 6 * gi.min(5) + bi.min(5)
909}
910
911fn rgb_to_ansi16(r: u8, g: u8, b: u8) -> Color {
912    let lum = 0.2126 * to_linear(f64::from(r) / 255.0)
913        + 0.7152 * to_linear(f64::from(g) / 255.0)
914        + 0.0722 * to_linear(f64::from(b) / 255.0);
915
916    let max = r.max(g).max(b);
917    let min = r.min(g).min(b);
918    let saturation = if max == 0 {
919        0.0
920    } else {
921        (max - min) as f32 / max as f32
922    };
923
924    if saturation < 0.2 {
925        // Grayscale: classify purely by luminance.
926        return match lum {
927            l if l < 0.05 => Color::Black,
928            l if l < 0.25 => Color::DarkGray,
929            l if l < 0.7 => Color::White,
930            _ => Color::White, // LightWhite is not available in Color enum
931        };
932    }
933
934    // For chromatic colors the "bright" variant of each ANSI hue (e.g. xterm
935    // LightRed = `(255, 85, 85)`) is distinguished by the minimum channel
936    // being lifted off zero — it's a brighter, partially desaturated version
937    // of the same hue. A perfectly saturated primary (`min == 0`) like pure
938    // red `(255, 0, 0)` should map to the standard color. We pick the bright
939    // variant only when both the value is high and the color is desaturated
940    // enough to look "lifted".
941    let bright = max >= 200 && min >= 64;
942
943    let rf = r as f32;
944    let gf = g as f32;
945    let bf = b as f32;
946
947    if rf >= gf && rf >= bf {
948        if gf > bf * 1.5 {
949            if bright {
950                Color::LightYellow
951            } else {
952                Color::Yellow
953            }
954        } else if bf > gf * 1.5 {
955            if bright {
956                Color::LightMagenta
957            } else {
958                Color::Magenta
959            }
960        } else if bright {
961            Color::LightRed
962        } else {
963            Color::Red
964        }
965    } else if gf >= rf && gf >= bf {
966        if bf > rf * 1.5 {
967            if bright {
968                Color::LightCyan
969            } else {
970                Color::Cyan
971            }
972        } else if bright {
973            Color::LightGreen
974        } else {
975            Color::Green
976        }
977    } else if rf > gf * 1.5 {
978        if bright {
979            Color::LightMagenta
980        } else {
981            Color::Magenta
982        }
983    } else if gf > rf * 1.5 {
984        if bright {
985            Color::LightCyan
986        } else {
987            Color::Cyan
988        }
989    } else if bright {
990        Color::LightBlue
991    } else {
992        Color::Blue
993    }
994}
995
996fn xterm256_to_rgb(idx: u8) -> (u8, u8, u8) {
997    match idx {
998        0 => (0, 0, 0),
999        1 => (128, 0, 0),
1000        2 => (0, 128, 0),
1001        3 => (128, 128, 0),
1002        4 => (0, 0, 128),
1003        5 => (128, 0, 128),
1004        6 => (0, 128, 128),
1005        7 => (192, 192, 192),
1006        8 => (128, 128, 128),
1007        9 => (255, 0, 0),
1008        10 => (0, 255, 0),
1009        11 => (255, 255, 0),
1010        12 => (0, 0, 255),
1011        13 => (255, 0, 255),
1012        14 => (0, 255, 255),
1013        15 => (255, 255, 255),
1014        16..=231 => {
1015            let n = idx - 16;
1016            let b_idx = n % 6;
1017            let g_idx = (n / 6) % 6;
1018            let r_idx = n / 36;
1019            let to_val = |i: u8| if i == 0 { 0u8 } else { 55 + 40 * i };
1020            (to_val(r_idx), to_val(g_idx), to_val(b_idx))
1021        }
1022        232..=255 => {
1023            let v = 8 + 10 * (idx - 232);
1024            (v, v, v)
1025        }
1026    }
1027}
1028
1029#[cfg(test)]
1030mod tests {
1031    #![allow(clippy::unwrap_used)]
1032    use super::*;
1033
1034    #[test]
1035    fn blend_halfway_rounds_to_128() {
1036        assert_eq!(
1037            Color::Rgb(255, 255, 255).blend_f64(Color::Rgb(0, 0, 0), 0.5),
1038            Color::Rgb(128, 128, 128)
1039        );
1040    }
1041
1042    #[test]
1043    fn contrast_ratio_white_on_black_is_high() {
1044        let ratio = Color::contrast_ratio_f64(Color::White, Color::Black);
1045        assert!(ratio > 15.0);
1046    }
1047
1048    #[test]
1049    fn contrast_ratio_same_color_is_one() {
1050        let ratio = Color::contrast_ratio_f64(Color::Rgb(100, 100, 100), Color::Rgb(100, 100, 100));
1051        assert!((ratio - 1.0).abs() < 0.01);
1052    }
1053
1054    #[test]
1055    fn meets_contrast_aa_white_on_black() {
1056        assert!(Color::meets_contrast_aa(Color::White, Color::Black));
1057    }
1058
1059    #[test]
1060    fn meets_contrast_aa_low_contrast_fails() {
1061        assert!(!Color::meets_contrast_aa(
1062            Color::Rgb(180, 180, 180),
1063            Color::Rgb(200, 200, 200)
1064        ));
1065    }
1066
1067    // --- regression: issue #104 rgb_to_ansi256 overflow at r=g=b=248 ---
1068
1069    #[test]
1070    fn rgb_to_ansi256_no_overflow_full_range() {
1071        // 256^3 exhaustive — guarantees no panic in debug or release builds
1072        for r in 0u8..=255 {
1073            for g in 0u8..=255 {
1074                for b in 0u8..=255 {
1075                    let _ = Color::Rgb(r, g, b).downsampled(ColorDepth::EightBit);
1076                }
1077            }
1078        }
1079    }
1080
1081    #[test]
1082    fn rgb_248_maps_to_231() {
1083        assert_eq!(
1084            Color::Rgb(248, 248, 248).downsampled(ColorDepth::EightBit),
1085            Color::Indexed(231)
1086        );
1087    }
1088
1089    // --- regression: issue #105 WCAG luminance sRGB gamma ---
1090
1091    #[test]
1092    fn luminance_dracula_purple_wcag() {
1093        let l = Color::Rgb(189, 147, 249).luminance_f64();
1094        assert!((l - 0.385).abs() < 0.01, "expected ~0.385, got {l}");
1095    }
1096
1097    #[test]
1098    fn contrast_aa_dracula_pair() {
1099        let p = Color::Rgb(189, 147, 249);
1100        let bg = Color::Rgb(40, 42, 54);
1101        assert!(Color::meets_contrast_aa(p, bg));
1102        let r = Color::contrast_ratio_f64(p, bg);
1103        assert!((r - 5.90).abs() < 0.1, "expected ~5.90, got {r}");
1104    }
1105
1106    #[test]
1107    fn contrast_white_on_black_is_21() {
1108        let r = Color::contrast_ratio_f64(Color::Rgb(255, 255, 255), Color::Rgb(0, 0, 0));
1109        assert!((r - 21.0).abs() < 0.5, "expected ~21.0, got {r}");
1110    }
1111
1112    // --- regression: issue #107 rgb_to_ansi16 includes bright (8-15) colors ---
1113
1114    #[test]
1115    fn rgb_to_ansi16_bright_variants() {
1116        // bright red → LightRed
1117        assert_eq!(
1118            Color::Rgb(255, 80, 80).downsampled(ColorDepth::Basic),
1119            Color::LightRed
1120        );
1121        // dark red → Red
1122        assert_eq!(
1123            Color::Rgb(128, 20, 20).downsampled(ColorDepth::Basic),
1124            Color::Red
1125        );
1126        // bright gray → White
1127        assert_eq!(
1128            Color::Rgb(200, 200, 200).downsampled(ColorDepth::Basic),
1129            Color::White
1130        );
1131        // dark gray → DarkGray
1132        assert_eq!(
1133            Color::Rgb(80, 80, 80).downsampled(ColorDepth::Basic),
1134            Color::DarkGray
1135        );
1136    }
1137
1138    // --- v0.21.1: ergonomic constructors / conversions ---
1139
1140    use std::str::FromStr;
1141
1142    #[test]
1143    fn from_tuple_and_array() {
1144        assert_eq!(Color::from((255, 107, 107)), Color::Rgb(255, 107, 107));
1145        assert_eq!(Color::from([1u8, 2, 3]), Color::Rgb(1, 2, 3));
1146        // Generic `.into()` path resolves through the same impls.
1147        let c: Color = (10, 20, 30).into();
1148        assert_eq!(c, Color::Rgb(10, 20, 30));
1149    }
1150
1151    #[test]
1152    fn from_u32_packs_rrggbb() {
1153        assert_eq!(Color::from(0xff6b6b_u32), Color::Rgb(255, 107, 107));
1154        assert_eq!(Color::from(0x000000_u32), Color::Rgb(0, 0, 0));
1155        assert_eq!(Color::from(0xffffff_u32), Color::Rgb(255, 255, 255));
1156        // High byte (alpha) is ignored.
1157        assert_eq!(Color::from(0xff00ff00_u32), Color::Rgb(0, 255, 0));
1158    }
1159
1160    #[test]
1161    fn from_str_hex_round_trips() {
1162        assert_eq!(
1163            Color::from_str("#ff6b6b").unwrap(),
1164            Color::Rgb(255, 107, 107)
1165        );
1166        // No leading '#'.
1167        assert_eq!(
1168            Color::from_str("ff6b6b").unwrap(),
1169            Color::Rgb(255, 107, 107)
1170        );
1171        // Short form expands nibbles.
1172        assert_eq!(Color::from_str("#abc").unwrap(), Color::Rgb(170, 187, 204));
1173        assert_eq!(Color::from_str("abc").unwrap(), Color::Rgb(170, 187, 204));
1174        // Whitespace is trimmed.
1175        assert_eq!(
1176            Color::from_str("  #ff6b6b  ").unwrap(),
1177            Color::Rgb(255, 107, 107)
1178        );
1179        // Hex parse matches to_hex round-trip.
1180        let c = Color::Rgb(18, 52, 86);
1181        assert_eq!(Color::from_str(&c.to_hex()).unwrap(), c);
1182    }
1183
1184    #[test]
1185    fn from_str_named_colors() {
1186        assert_eq!(Color::from_str("cyan").unwrap(), Color::Cyan);
1187        assert_eq!(Color::from_str("LightBlue").unwrap(), Color::LightBlue);
1188        assert_eq!(Color::from_str("DARKGRAY").unwrap(), Color::DarkGray);
1189        assert_eq!(Color::from_str("grey").unwrap(), Color::DarkGray);
1190        assert_eq!(Color::from_str("reset").unwrap(), Color::Reset);
1191        assert_eq!(Color::from_str("default").unwrap(), Color::Reset);
1192    }
1193
1194    #[test]
1195    fn from_str_error_cases() {
1196        // Wrong length with '#' → InvalidLength.
1197        assert_eq!(
1198            Color::from_str("#ff6b").unwrap_err(),
1199            ColorParseError::InvalidLength
1200        );
1201        // Non-hex digit in a '#'-prefixed 6-char token → InvalidHexDigit.
1202        assert_eq!(
1203            Color::from_str("#zz0011").unwrap_err(),
1204            ColorParseError::InvalidHexDigit
1205        );
1206        // Non-hex digit in a 3-char token → InvalidHexDigit.
1207        assert_eq!(
1208            Color::from_str("#xyz").unwrap_err(),
1209            ColorParseError::InvalidHexDigit
1210        );
1211        // Unknown name of non-hex length → Unknown.
1212        assert_eq!(
1213            Color::from_str("nope").unwrap_err(),
1214            ColorParseError::Unknown
1215        );
1216        assert_eq!(Color::from_str("").unwrap_err(), ColorParseError::Unknown);
1217    }
1218
1219    #[test]
1220    fn color_parse_error_display_and_error_trait() {
1221        // Display is non-empty and Error trait is implemented.
1222        let e = ColorParseError::InvalidLength;
1223        assert!(!e.to_string().is_empty());
1224        let _: &dyn std::error::Error = &e;
1225    }
1226
1227    #[test]
1228    fn from_hsl_primaries() {
1229        assert_eq!(Color::from_hsl_f64(0.0, 1.0, 0.5), Color::Rgb(255, 0, 0));
1230        assert_eq!(Color::from_hsl_f64(120.0, 1.0, 0.5), Color::Rgb(0, 255, 0));
1231        assert_eq!(Color::from_hsl_f64(240.0, 1.0, 0.5), Color::Rgb(0, 0, 255));
1232        // Lightness extremes.
1233        assert_eq!(Color::from_hsl_f64(0.0, 1.0, 0.0), Color::Rgb(0, 0, 0));
1234        assert_eq!(
1235            Color::from_hsl_f64(0.0, 1.0, 1.0),
1236            Color::Rgb(255, 255, 255)
1237        );
1238        // Zero saturation → gray regardless of hue.
1239        assert_eq!(
1240            Color::from_hsl_f64(123.0, 0.0, 0.5),
1241            Color::Rgb(128, 128, 128)
1242        );
1243    }
1244
1245    #[test]
1246    fn from_hsl_wraps_and_clamps() {
1247        // Hue 360 wraps to 0 → red.
1248        assert_eq!(Color::from_hsl_f64(360.0, 1.0, 0.5), Color::Rgb(255, 0, 0));
1249        // Negative hue wraps: -120 == 240 → blue.
1250        assert_eq!(Color::from_hsl_f64(-120.0, 1.0, 0.5), Color::Rgb(0, 0, 255));
1251        // Out-of-range s/l are clamped, no panic.
1252        assert_eq!(
1253            Color::from_hsl_f64(0.0, 5.0, 2.0),
1254            Color::Rgb(255, 255, 255)
1255        );
1256    }
1257
1258    #[test]
1259    fn from_hsv_primaries() {
1260        assert_eq!(Color::from_hsv_f64(0.0, 1.0, 1.0), Color::Rgb(255, 0, 0));
1261        assert_eq!(Color::from_hsv_f64(120.0, 1.0, 1.0), Color::Rgb(0, 255, 0));
1262        assert_eq!(Color::from_hsv_f64(240.0, 1.0, 1.0), Color::Rgb(0, 0, 255));
1263        // White and black.
1264        assert_eq!(
1265            Color::from_hsv_f64(0.0, 0.0, 1.0),
1266            Color::Rgb(255, 255, 255)
1267        );
1268        assert_eq!(Color::from_hsv_f64(0.0, 0.0, 0.0), Color::Rgb(0, 0, 0));
1269    }
1270
1271    #[test]
1272    fn rotate_hue_primary_round_trip() {
1273        // Red rotated 120° → green, another 120° → blue.
1274        assert_eq!(
1275            Color::Rgb(255, 0, 0).rotate_hue_f64(120.0),
1276            Color::Rgb(0, 255, 0)
1277        );
1278        assert_eq!(
1279            Color::Rgb(0, 255, 0).rotate_hue_f64(120.0),
1280            Color::Rgb(0, 0, 255)
1281        );
1282        // 180° on red lands on cyan.
1283        assert_eq!(
1284            Color::Rgb(255, 0, 0).rotate_hue_f64(180.0),
1285            Color::Rgb(0, 255, 255)
1286        );
1287        // Full 360° rotation is a no-op (within rounding) for a primary.
1288        assert_eq!(
1289            Color::Rgb(255, 0, 0).rotate_hue_f64(360.0),
1290            Color::Rgb(255, 0, 0)
1291        );
1292    }
1293
1294    #[test]
1295    fn rotate_hue_resolves_named_to_rgb() {
1296        // Named/indexed colors resolve through the palette and yield Rgb.
1297        let rotated = Color::Red.rotate_hue_f64(0.0);
1298        assert_eq!(rotated, Color::Rgb(205, 49, 49));
1299        let gray = Color::Rgb(120, 120, 120).rotate_hue_f64(90.0);
1300        // Achromatic input stays achromatic (gray) after rotation.
1301        assert_eq!(gray, Color::Rgb(120, 120, 120));
1302    }
1303}