retroglyph_core/color/mod.rs
1//! Color and style types for character cells: [`Color`] (this module) and [`Style`], a `{fg,
2//! bg}` pair of two `Color`s with no other relation to anything else in this crate.
3//!
4//! Split into private submodules by concern, `animate`/`backend`/`testing`-style: `ansi` is the
5//! 16-color ANSI palette and the shared indexed/ANSI quantization machinery, `convert` is
6//! `Color`'s inherent methods (constants, RGB resolution, `gem` color-space conversions),
7//! `named` is `Color`'s string-name/hex constructors (`from_named`, `from_hex`), `palette_oklab`
8//! is the generated Oklab table `ansi` quantizes against, `parse` is `Color`'s
9//! `Display`/`FromStr`/serde impls, `sgr` (public: shared with `retroglyph-recorder`) is the SGR
10//! ANSI encoding [`Style`] resolves to, `style` is [`Style`](crate::color::Style) itself, and
11//! `tint` is [`Tint`](crate::color::Tint), sprite colour modulation.
12
13mod ansi;
14mod convert;
15mod named;
16mod palette_oklab;
17mod parse;
18pub mod sgr;
19mod style;
20mod tint;
21
22pub use ansi::{AnsiColor, InvalidAnsiIndex, Quantize};
23pub use parse::ParseColorError;
24pub use style::Style;
25pub use tint::Tint;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
28#[non_exhaustive]
29/// Represents a color in the terminal grid.
30///
31/// # Examples
32///
33/// ```
34/// use retroglyph_core::color::Color;
35///
36/// let named = Color::GREEN;
37/// let rgb = Color::rgb(255, 0, 0);
38/// let indexed = Color::Indexed(42);
39/// assert_ne!(named, rgb);
40/// assert_ne!(rgb, indexed);
41/// ```
42pub enum Color {
43 #[default]
44 /// Backend's default foreground/background color.
45 ///
46 /// This tells the rendering backend to use the terminal's configured
47 /// default colors (e.g., the user's background color preference).
48 Default,
49 /// One of the 16 standard ANSI colors.
50 ///
51 /// Use these to respect the user's terminal theme.
52 Ansi(AnsiColor),
53 /// 256-color palette index.
54 Indexed(u8),
55 /// 24-bit RGB color.
56 ///
57 /// Use this for exact color matching regardless of terminal settings.
58 Rgb {
59 /// Red channel.
60 r: u8,
61 /// Green channel.
62 g: u8,
63 /// Blue channel.
64 b: u8,
65 },
66}