Expand description
Terminal color values, palettes, and capability profiles.
§Color values
Color covers three palette spaces in one enum: the sixteen named ANSI
colors (Color::Green, Color::BrightBlue, and so on),
Color::Indexed for the xterm 256-color palette, and Color::Rgb for
24-bit true color. All of them convert to RGB with Color::to_rgb, which
makes palette colors usable with true-color helpers such as
Color::to_hex and Color::to_hsl.
§Named colors
The sixteen named variants are the standard ANSI palette: Black through
White are normal intensity (palette indices 0..=7) and BrightBlack
through BrightWhite are the bright/high-intensity variants (8..=15).
Color::named_index returns that 0..=15 index for a named color and
None for Color::Indexed/Color::Rgb; Color::from_named
goes the other way.
§Hex and HSL helpers
Color::hex accepts optional-# three-, six-, or eight-digit hex input
and returns Color::Rgb. Eight-digit input parses and ignores the final
alpha byte because terminal colors carry no alpha channel. Color::hsl
wraps hue into 0..360, clamps saturation/lightness into 0.0..=1.0, and
rounds computed RGB channels to the nearest byte.
Color::to_hex and Color::to_hsl first resolve named and indexed
colors through the xterm palette. Gray HSL colors report hue 0.0 because
the hue is undefined when saturation is zero.
§Profile downsampling
Profile describes the color capability of an output stream. Converting a
color through a profile preserves true color when possible, quantizes to the
nearest supported palette for Ansi256/Ansi, and drops color entirely for
Ascii/Disabled.
Color::Rgb / Indexed / named
│
├─ Profile::TrueColor ──► original color
├─ Profile::Ansi256 ──► nearest xterm index
├─ Profile::Ansi ──► nearest named color
└─ Ascii / Disabled ──► Noneuse uncurses::color::{Color, Profile};
let orange = Color::Rgb(255, 128, 0);
let green = Color::Green;
assert_eq!(Profile::TrueColor.convert(orange), Some(orange));
assert!(matches!(Profile::Ansi256.convert(orange), Some(Color::Indexed(_))));
assert_eq!(Profile::Disabled.convert(green), None);