Skip to main content

Color

Enum Color 

Source
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

Source

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)”
Source

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.

Source

pub fn from_triplet(triplet: ColorTriplet) -> Color

Creates a Color from an RGB triplet.

Source

pub fn from_rgb(red: u8, green: u8, blue: u8) -> Color

Creates a Color from RGB components.

Source

pub fn default_color() -> Color

Returns the default terminal color.

Source

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.

Source

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.

Source

pub fn number(&self) -> Option<u8>

The palette number, if applicable. None for Default and TrueColor.

Source

pub fn triplet(&self) -> Option<ColorTriplet>

The RGB triplet, if this is a Color::TrueColor.

Source

pub fn system(&self) -> ColorSystem

Returns the native color system for this color.

Source

pub fn is_system_defined(&self) -> bool

Returns true if the color is system-defined (not 8-bit/truecolor).

Source

pub fn is_default(&self) -> bool

Returns true if this is the default color.

Source

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).
Source

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.
Source

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.

Source

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.

Source

pub fn downgrade(&self, system: ColorSystem) -> Color

Downgrades the color to a lower color system.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Color

Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Color

Source§

impl Hash for Color

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.