#[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(255, 0, 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: Color
pub const BRIGHT_BLACK: Color
Bright Black / dark grey (ANSI).
Sourcepub const BRIGHT_RED: Color
pub const BRIGHT_RED: Color
Bright Red (ANSI).
Sourcepub const BRIGHT_GREEN: Color
pub const BRIGHT_GREEN: Color
Bright Green (ANSI).
Sourcepub const BRIGHT_YELLOW: Color
pub const BRIGHT_YELLOW: Color
Bright Yellow (ANSI).
Sourcepub const BRIGHT_BLUE: Color
pub const BRIGHT_BLUE: Color
Bright Blue (ANSI).
Sourcepub const BRIGHT_MAGENTA: Color
pub const BRIGHT_MAGENTA: Color
Bright Magenta (ANSI).
Sourcepub const BRIGHT_CYAN: Color
pub const BRIGHT_CYAN: Color
Bright Cyan (ANSI).
Sourcepub const BRIGHT_WHITE: Color
pub const BRIGHT_WHITE: Color
Bright White (ANSI).
Sourcepub const fn rgb(r: u8, g: u8, b: u8) -> Color
pub const fn rgb(r: u8, g: u8, b: u8) -> Color
Constructs an Rgb variant from its three channels.
Sourcepub const fn hex(rgb: u32) -> Color
pub const fn hex(rgb: u32) -> Color
Constructs an Rgb variant from a packed 0xRRGGBB value. The high byte is ignored.
Round-trips the #rrggbb form Display writes, so a color logged
out of a running app can be pasted straight back into source. Unlike
from_hex, there is no CSS short-form expansion: Color::hex(0xF80) is
#000f80, not #ff8800.
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) -> Color
pub fn from_srgb(srgb: Srgb) -> Color
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: Color, b: Color, t: f32) -> Color
pub fn lerp(a: Color, b: Color, t: f32) -> Color
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) -> Color
pub fn lighten(self, amount: f32) -> Color
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) -> Color
pub fn darken(self, amount: f32) -> Color
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) -> Color
pub fn saturate(self, amount: f32) -> Color
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) -> Color
pub fn desaturate(self, amount: f32) -> Color
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) -> Color
pub fn complement(self) -> Color
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) -> Color
pub fn to_indexed(self) -> Color
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(0, 0, 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) -> Color
pub fn to_indexed_with(self, metric: Quantize) -> Color
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(250, 128, 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) -> Color
pub fn to_ansi(self) -> Color
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(255, 0, 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) -> Color
pub fn to_ansi_with(self, metric: Quantize) -> Color
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(210, 105, 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<Color>
pub fn from_named(name: &str) -> Option<Color>
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(255, 215, 0)));
assert_eq!(Color::from_named("not-a-color"), None);Sourcepub fn from_hex(hex: &str) -> Option<Color>
pub fn from_hex(hex: &str) -> Option<Color>
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(255, 128, 0));
assert_eq!(Color::from_hex("not-color"), None);Trait Implementations§
impl Copy for Color
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<Color, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Color, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl Display for Color
impl Display for Color
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
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(255, 128, 0).to_string(), "#ff8000");impl Eq for Color
Source§impl FromStr for Color
impl FromStr for Color
Source§fn from_str(s: &str) -> Result<Color, <Color as FromStr>::Err>
fn from_str(s: &str) -> Result<Color, <Color as FromStr>::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(255, 128, 0)));
assert_eq!("#f80".parse(), Ok(Color::rgb(255, 136, 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 as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::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 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<T> ToSmolStr for T
impl<T> ToSmolStr for T
fn to_smolstr(&self) -> SmolStr
impl<T> WasmNotSend for Twhere
T: Send,
impl<T> WasmNotSendSync for Twhere
T: WasmNotSend + WasmNotSync,
impl<T> WasmNotSync for Twhere
T: Sync,
§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]).