pub enum Color {
Default,
Standard(u8),
EightBit(u8),
TrueColor(ColorTriplet),
Windows(u8),
}Expand description
A terminal color.
v0.11.0 break: Color was a struct with name, color_type, number,
triplet fields. It is now a 5-variant enum, shrinking from ~40 B + heap
String to ~4 B inline. The name field becomes Color::name (a
Cow<'_, str> method) and color_type becomes Color::kind.
§Migration
// Before:
Color { name: "default".into(), color_type: ColorType::Default,
number: None, triplet: None }
// After:
Color::Default
// Before:
Color { name: "color(42)".into(), color_type: ColorType::EightBit,
number: Some(42), triplet: None }
// After:
Color::EightBit(42)
// Before:
Color { name: "#ff0000".into(), color_type: ColorType::TrueColor,
number: None, triplet: Some(ColorTriplet::new(255, 0, 0)) }
// After:
Color::TrueColor(ColorTriplet::new(255, 0, 0))
// Before: // After:
color.name color.name()
color.color_type color.kind()Variants§
Default
Terminal default color (foreground or background).
Standard(u8)
Standard 16-color ANSI palette (number < 16).
EightBit(u8)
Extended 256-color (8-bit) palette (number 16..=255).
TrueColor(ColorTriplet)
24-bit true-color RGB.
Windows(u8)
Windows console legacy 16-color palette. Behaves identically to
Color::Standard except resolution uses the Windows palette.
Implementations§
Source§impl Color
impl Color
Sourcepub fn parse(color: &str) -> Result<Color, ColorParseError>
pub fn parse(color: &str) -> Result<Color, ColorParseError>
Parses a color string into a Color.
Supports:
- “default” - terminal default color
- Named colors: “red”, “bright_red”, “yellow4”, etc.
- Hex: “#ff0000”
- color(N): “color(100)”
- RGB: “rgb(255,0,0)”
Sourcepub fn from_ansi(number: u8) -> Color
pub fn from_ansi(number: u8) -> Color
Creates a Color from an 8-bit ANSI color number. Numbers 0..16 map
to Color::Standard; 16..=255 map to Color::EightBit.
Sourcepub fn from_triplet(triplet: ColorTriplet) -> Color
pub fn from_triplet(triplet: ColorTriplet) -> Color
Creates a Color from an RGB triplet.
Sourcepub fn default_color() -> Color
pub fn default_color() -> Color
Returns the default terminal color.
Sourcepub fn name(&self) -> Cow<'_, str>
pub fn name(&self) -> Cow<'_, str>
Human-readable name. Borrowed for known palette colors (no alloc),
owned format!("color({n})") or hex for numbered/RGB colors.
Sourcepub fn kind(&self) -> ColorType
pub fn kind(&self) -> ColorType
Classification matching the legacy ColorType for callers that
need to switch on the variant tag without exposing the inner data.
Sourcepub fn number(&self) -> Option<u8>
pub fn number(&self) -> Option<u8>
The palette number, if applicable. None for Default and TrueColor.
Sourcepub fn triplet(&self) -> Option<ColorTriplet>
pub fn triplet(&self) -> Option<ColorTriplet>
The RGB triplet, if this is a Color::TrueColor.
Sourcepub fn system(&self) -> ColorSystem
pub fn system(&self) -> ColorSystem
Returns the native color system for this color.
Sourcepub fn is_system_defined(&self) -> bool
pub fn is_system_defined(&self) -> bool
Returns true if the color is system-defined (not 8-bit/truecolor).
Sourcepub fn is_default(&self) -> bool
pub fn is_default(&self) -> bool
Returns true if this is the default color.
Sourcepub fn get_truecolor(
&self,
theme: Option<&TerminalTheme>,
foreground: bool,
) -> ColorTriplet
pub fn get_truecolor( &self, theme: Option<&TerminalTheme>, foreground: bool, ) -> ColorTriplet
Resolves the color to an RGB triplet.
§Arguments
theme- Optional theme to use for resolving system colors. If None, uses DEFAULT_TERMINAL_THEME.foreground- Whether this is a foreground color (affects default color resolution).
Sourcepub fn get_ansi_codes(&self, foreground: bool) -> Vec<String>
pub fn get_ansi_codes(&self, foreground: bool) -> Vec<String>
Gets the ANSI escape codes for this color as a Vec<String>.
Note: the production render path uses Color::write_ansi_codes
which writes directly into a pre-allocated buffer (no allocation).
Prefer that method when calling per-segment.
§Arguments
foreground- If true, returns foreground codes; otherwise background codes.
Sourcepub fn write_ansi_codes(&self, foreground: bool, buf: &mut String)
pub fn write_ansi_codes(&self, foreground: bool, buf: &mut String)
Writes ANSI escape codes for this color directly into a semicolon-separated SGR buffer, avoiding per-code String allocations.
If buf is non-empty, a leading ; separator is written first.
Sourcepub fn write_underline_color_codes(&self, buf: &mut String)
pub fn write_underline_color_codes(&self, buf: &mut String)
Writes underline color codes (SGR 58) directly into a semicolon-separated SGR buffer. Converts foreground codes to underline-color equivalents.
Sourcepub fn downgrade(&self, system: ColorSystem) -> Color
pub fn downgrade(&self, system: ColorSystem) -> Color
Downgrades the color to a lower color system.
Trait Implementations§
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,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read more