#[non_exhaustive]pub enum Color {
Default,
Ansi(AnsiColor),
Indexed(u8),
Rgb {
r: u8,
g: u8,
b: u8,
},
}Expand description
Represents a color in the terminal grid.
§Examples
use retroglyph_core::color::Color;
let named = Color::GREEN;
let rgb = Color::Rgb { r: 255, g: 0, b: 0 };
let indexed = Color::Indexed(42);
assert_ne!(named, rgb);
assert_ne!(rgb, indexed);Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Default
Backend’s default foreground/background color.
This tells the rendering backend to use the terminal’s configured default colors (e.g., the user’s background color preference).
Ansi(AnsiColor)
One of the 16 standard ANSI colors.
Use these to respect the user’s terminal theme.
Indexed(u8)
256-color palette index.
Rgb
24-bit RGB color.
Use this for exact color matching regardless of terminal settings.
Implementations§
Source§impl Color
impl Color
Sourcepub const BRIGHT_BLACK: Self
pub const BRIGHT_BLACK: Self
Bright Black / dark grey (ANSI).
Sourcepub const BRIGHT_RED: Self
pub const BRIGHT_RED: Self
Bright Red (ANSI).
Sourcepub const BRIGHT_GREEN: Self
pub const BRIGHT_GREEN: Self
Bright Green (ANSI).
Sourcepub const BRIGHT_YELLOW: Self
pub const BRIGHT_YELLOW: Self
Bright Yellow (ANSI).
Sourcepub const BRIGHT_BLUE: Self
pub const BRIGHT_BLUE: Self
Bright Blue (ANSI).
Sourcepub const BRIGHT_MAGENTA: Self
pub const BRIGHT_MAGENTA: Self
Bright Magenta (ANSI).
Sourcepub const BRIGHT_CYAN: Self
pub const BRIGHT_CYAN: Self
Bright Cyan (ANSI).
Sourcepub const BRIGHT_WHITE: Self
pub const BRIGHT_WHITE: Self
Bright White (ANSI).
Sourcepub const fn resolve_rgb(self, default: (u8, u8, u8)) -> (u8, u8, u8)
pub const fn resolve_rgb(self, default: (u8, u8, u8)) -> (u8, u8, u8)
Resolves this color to a concrete 24-bit (r, g, b) triple, substituting default for
Color::Default.
This is the canonical color-to-RGB resolution every graphical backend shares, so that a
glyph drawn through the CPU rasterizer (retroglyph-software) and the GPU atlas
(retroglyph-gl) comes out the same pixel color:
Rgbpasses through unchanged.Ansiresolves throughAnsiColor::to_rgb(the one canonical ANSI palette).Indexedresolves through the 256-color palette (16 ANSI + 6×6×6 cube- grayscale ramp).
Default(and any future non-exhaustive variant this crate can’t yet resolve) returnsdefault, which the caller picks per channel (foreground vs background).
Terminal backends do not use this: they emit ANSI/indexed colors as-is and let the terminal apply the user’s theme. It exists specifically for pixel/GPU backends that must produce real RGB.
Sourcepub fn to_srgb(self) -> Option<Srgb>
pub fn to_srgb(self) -> Option<Srgb>
Converts an Rgb variant to gem::space::Srgb.
Returns None for non-RGB variants (Default, Ansi, Indexed).
Sourcepub fn from_srgb(srgb: Srgb) -> Self
pub fn from_srgb(srgb: Srgb) -> Self
Constructs an Rgb variant from a gem::space::Srgb color.
Channels are clamped to [0.0, 1.0] and rounded to the nearest u8 (ties away from
zero), via gem::rgb::Rgb888’s own Srgb conversion, the same round-to-nearest rule
every other integer channel operation in this crate follows (see
tests/rounding_conformance.rs).
Sourcepub fn lerp(a: Self, b: Self, t: f32) -> Self
pub fn lerp(a: Self, b: Self, t: f32) -> Self
Linearly interpolates between two colors, always returning a concrete Rgb result.
Both inputs are resolved to (r, g, b) via Color::resolve_rgb before blending, so
non-Rgb variants (Ansi, Indexed) contribute their real color rather than being
skipped. Color::Default has no intrinsic RGB value, so it falls back to
(0, 0, 0) when it appears as a and (255, 255, 255) when it appears as b.
Sourcepub fn lighten(self, amount: f32) -> Self
pub fn lighten(self, amount: f32) -> Self
Lightens a color by amount (0.0 = no change, 1.0 = white).
Non-Rgb variants are resolved to (r, g, b) via Color::resolve_rgb before the
transform is applied, rather than being returned unchanged. Color::Default has no
intrinsic RGB value, so it resolves to (0, 0, 0).
Sourcepub fn darken(self, amount: f32) -> Self
pub fn darken(self, amount: f32) -> Self
Darkens a color by amount (0.0 = no change, 1.0 = black).
Non-Rgb variants are resolved to (r, g, b) via Color::resolve_rgb before the
transform is applied, rather than being returned unchanged. Color::Default has no
intrinsic RGB value, so it resolves to (0, 0, 0).
Sourcepub fn saturate(self, amount: f32) -> Self
pub fn saturate(self, amount: f32) -> Self
Increases saturation of a color by amount (0.0–1.0).
Non-Rgb variants are resolved to (r, g, b) via Color::resolve_rgb before the
transform is applied, rather than being returned unchanged. Color::Default has no
intrinsic RGB value, so it resolves to (0, 0, 0).
Sourcepub fn desaturate(self, amount: f32) -> Self
pub fn desaturate(self, amount: f32) -> Self
Decreases saturation of a color by amount (0.0–1.0).
Non-Rgb variants are resolved to (r, g, b) via Color::resolve_rgb before the
transform is applied, rather than being returned unchanged. Color::Default has no
intrinsic RGB value, so it resolves to (0, 0, 0).
Sourcepub fn complement(self) -> Self
pub fn complement(self) -> Self
Returns the complementary color (hue shifted by 180 degrees).
Non-Rgb variants are resolved to (r, g, b) via Color::resolve_rgb before the
transform is applied, rather than being returned unchanged. Color::Default has no
intrinsic RGB value, so it resolves to (0, 0, 0).
Sourcepub fn to_indexed(self) -> Self
pub fn to_indexed(self) -> Self
Quantizes an RGB color to the nearest entry in the standard 256-color palette, by perceptual (Oklab) distance.
Equivalent to to_indexed_with(Quantize::Perceptual); see there
for the full contract and for the euclidean alternative.
§Examples
use retroglyph_core::color::Color;
let black = Color::Rgb { r: 0, g: 0, b: 0 };
assert_eq!(black.to_indexed(), Color::Indexed(0));
// Non-RGB colors pass through unchanged.
assert_eq!(Color::Default.to_indexed(), Color::Default);Backends that render to terminals without full RGB support can call this method to
downgrade colors before emitting them; retroglyph-core never downgrades colors on
its own. See Color::to_ansi to quantize to the smaller 16-color ANSI palette.
Sourcepub fn to_indexed_with(self, metric: Quantize) -> Self
pub fn to_indexed_with(self, metric: Quantize) -> Self
Quantizes an RGB color to the nearest entry in the standard 256-color palette, under
metric.
Color::Rgbinputs are converted to the nearest 256-color palette index (0–255), searching the 16 ANSI colors (0–15), the 6×6×6 color cube (16–231), and the grayscale ramp (232–255).Color::Default,Color::Ansi, andColor::Indexedare returned unchanged: this method only downgradesRgbcolors.- Ties (multiple equidistant palette entries) are resolved by preferring the lower index.
§Examples
use retroglyph_core::color::{Color, Quantize};
let salmon = Color::Rgb { r: 250, g: 128, b: 114 };
assert_eq!(salmon.to_indexed_with(Quantize::Perceptual), Color::Indexed(210));
assert_eq!(salmon.to_indexed_with(Quantize::Euclidean), Color::Indexed(209));Sourcepub fn to_ansi(self) -> Self
pub fn to_ansi(self) -> Self
Quantizes an RGB color to the nearest of the 16 standard ANSI palette colors, by perceptual (Oklab) distance.
Equivalent to to_ansi_with(Quantize::Perceptual); see there for the
full contract and for the euclidean alternative.
§Examples
use retroglyph_core::color::{AnsiColor, Color};
let pure_red = Color::Rgb { r: 255, g: 0, b: 0 };
assert_eq!(pure_red.to_ansi(), Color::Ansi(AnsiColor::BrightRed));
// Non-RGB colors pass through unchanged.
assert_eq!(Color::Default.to_ansi(), Color::Default);Use this method when rendering to terminals limited to 16 colors, or when a caller
otherwise needs to reduce color depth. See Color::to_indexed to quantize to the
larger 256-color palette instead.
Sourcepub fn to_ansi_with(self, metric: Quantize) -> Self
pub fn to_ansi_with(self, metric: Quantize) -> Self
Quantizes an RGB color to the nearest of the 16 standard ANSI palette colors, under
metric.
Color::Rgbinputs are converted to the nearest of the 16 standard ANSI colors.Color::Default,Color::Ansi, andColor::Indexedare returned unchanged: this method only downgradesRgbcolors.- Ties (multiple equidistant palette entries) are resolved by preferring the lower ANSI index.
§Examples
use retroglyph_core::color::{AnsiColor, Color, Quantize};
// Euclidean RGB distance over-weights green, so this reddish brown lands on yellow.
let chocolate = Color::Rgb { r: 210, g: 105, b: 30 };
assert_eq!(chocolate.to_ansi_with(Quantize::Perceptual), Color::Ansi(AnsiColor::BrightRed));
assert_eq!(chocolate.to_ansi_with(Quantize::Euclidean), Color::Ansi(AnsiColor::Yellow));Source§impl Color
impl Color
Sourcepub fn from_named(name: &str) -> Option<Self>
pub fn from_named(name: &str) -> Option<Self>
Looks up a CSS named color by name (case-insensitive).
Supports all 147 CSS Color Module Level 4 named colors.
Returns None for unrecognized names.
§Examples
use retroglyph_core::color::Color;
let gold = Color::from_named("gold");
assert_eq!(gold, Some(Color::Rgb { r: 255, g: 215, b: 0 }));
assert_eq!(Color::from_named("not-a-color"), None);Sourcepub fn from_hex(hex: &str) -> Option<Self>
pub fn from_hex(hex: &str) -> Option<Self>
Parses a CSS hex color string into an Rgb variant.
Accepts #rgb and #rrggbb formats (case-insensitive).
Returns None for invalid input.
§Examples
use retroglyph_core::color::Color;
let c = Color::from_hex("#ff8000")?;
assert_eq!(c, Color::Rgb { r: 255, g: 128, b: 0 });
assert_eq!(Color::from_hex("not-color"), None);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Color
Available on crate feature serde only.
impl<'de> Deserialize<'de> for Color
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Display for Color
impl Display for Color
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Writes the string form FromStr parses back and, with the serde
feature, Serialize writes: "default", an ANSI name like
"bright-red", a palette index like "42", or #rrggbb hex.
§Examples
use retroglyph_core::color::{AnsiColor, Color};
assert_eq!(Color::Default.to_string(), "default");
assert_eq!(Color::Ansi(AnsiColor::BrightRed).to_string(), "bright-red");
assert_eq!(Color::Indexed(42).to_string(), "42");
assert_eq!(Color::Rgb { r: 255, g: 128, b: 0 }.to_string(), "#ff8000");Source§impl FromStr for Color
impl FromStr for Color
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parses the string form written by Display.
Hyphens, underscores, and spaces are ignored and matching is case-insensitive, so
"BrightRed", "bright red", and "bright-red" all parse the same as the canonical
"bright-red" that Display writes. "reset" is accepted as a
synonym for "default". Accepts #rgb in addition to the #rrggbb Display writes.
§Examples
use retroglyph_core::color::{AnsiColor, Color};
assert_eq!("default".parse(), Ok(Color::Default));
assert_eq!("reset".parse(), Ok(Color::Default));
assert_eq!("bright-red".parse(), Ok(Color::Ansi(AnsiColor::BrightRed)));
assert_eq!("Bright Red".parse(), Ok(Color::Ansi(AnsiColor::BrightRed)));
assert_eq!("42".parse(), Ok(Color::Indexed(42)));
assert_eq!("#ff8000".parse(), Ok(Color::Rgb { r: 255, g: 128, b: 0 }));
assert_eq!("#f80".parse(), Ok(Color::Rgb { r: 255, g: 136, b: 0 }));
assert!("not-a-color".parse::<Color>().is_err());Source§type Err = ParseColorError
type Err = ParseColorError
Source§impl Serialize for Color
Available on crate feature serde only.
impl Serialize for Color
serde only.Source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serializes through the Display round trip, e.g. "bright-red" or
"#ff8000", rather than deriving a structural form: a hand-edited TOML/JSON theme
file stays legible and isn’t coupled to this (#[non_exhaustive]) enum’s variant shape.
impl Copy for Color
impl Eq for Color
impl StructuralPartialEq for Color
Auto Trait Implementations§
impl Freeze for Color
impl RefUnwindSafe for Color
impl Send for Color
impl Sync for Color
impl Unpin for Color
impl UnsafeUnpin for Color
impl UnwindSafe for Color
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<C> WithAlpha for Cwhere
C: Copy,
impl<C> WithAlpha for Cwhere
C: Copy,
§fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
self with alpha, storing alpha before the color in memory
(see [AlphaFirst]).§fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
self with alpha, storing alpha after the color in memory
(see [AlphaLast]).