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