Skip to main content

Color

Enum Color 

Source
pub enum Color {
Show 18 variants Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, BrightBlack, BrightRed, BrightGreen, BrightYellow, BrightBlue, BrightMagenta, BrightCyan, BrightWhite, Indexed(u8), Rgb(u8, u8, u8),
}
Expand description

A terminal color in one of the supported palette spaces.

The sixteen named variants are the standard ANSI palette. Use Color::Rgb when exact 24-bit color should be preserved on true-color terminals and Color::Indexed when targeting a specific xterm 256-color palette entry. A Profile can downsample any variant to the capability of a particular output stream.

Variants§

§

Black

Black (palette 0, foreground 30, background 40).

§

Red

Red (palette 1, foreground 31, background 41).

§

Green

Green (palette 2, foreground 32, background 42).

§

Yellow

Yellow (palette 3, foreground 33, background 43).

§

Blue

Blue (palette 4, foreground 34, background 44).

§

Magenta

Magenta (palette 5, foreground 35, background 45).

§

Cyan

Cyan (palette 6, foreground 36, background 46).

§

White

White (palette 7, foreground 37, background 47).

§

BrightBlack

Bright black (palette 8, foreground 90, background 100).

§

BrightRed

Bright red (palette 9, foreground 91, background 101).

§

BrightGreen

Bright green (palette 10, foreground 92, background 102).

§

BrightYellow

Bright yellow (palette 11, foreground 93, background 103).

§

BrightBlue

Bright blue (palette 12, foreground 94, background 104).

§

BrightMagenta

Bright magenta (palette 13, foreground 95, background 105).

§

BrightCyan

Bright cyan (palette 14, foreground 96, background 106).

§

BrightWhite

Bright white (palette 15, foreground 97, background 107).

§

Indexed(u8)

Extended xterm 256-color palette index (0..=255).

§

Rgb(u8, u8, u8)

24-bit true color as red, green, and blue bytes.

Implementations§

Source§

impl Color

Source

pub const fn rgb(r: u8, g: u8, b: u8) -> Self

Construct a 24-bit RGB color.

Returns Color::Rgb with the supplied red, green, and blue bytes. This is a const convenience constructor for code that prefers named parameters over the enum variant.

Source

pub const fn named_index(self) -> Option<u8>

Return the 0..=15 ANSI palette index for a named color.

Returns Some(0..=15) for the sixteen named variants and None for Color::Indexed and Color::Rgb. The index is also the xterm 256-color palette index used when resolving a named color through Color::to_rgb.

Source

pub const fn from_named(v: u8) -> Option<Self>

Convert a 0..=15 ANSI palette index into the matching named color.

Returns Some for values in 0..=15 and None for any higher value.

Source

pub const fn is_named(self) -> bool

Return whether this is one of the sixteen named ANSI colors.

Source

pub const fn is_named_bright(self) -> bool

Return whether this is a bright/high-intensity named color.

Bright colors are the named variants with palette indices 8..=15 and encode as foreground SGR 90..=97 or background SGR 100..=107. Color::Indexed and Color::Rgb are never bright.

Source

pub fn to_rgb(self) -> (u8, u8, u8)

Convert this color to (red, green, blue) bytes.

Color::Rgb returns its stored components unchanged. Named colors and Color::Indexed are resolved through the xterm 256-color palette, where named colors use their 0..=15 palette index.

Source

pub fn to_hex(self) -> String

Format this color as a lower-case #rrggbb hex string.

Palette and indexed colors are resolved with Color::to_rgb first, so the result is always the six-digit true-color form. The returned string never includes alpha.

Source

pub fn to_hsl(self) -> (f32, f32, f32)

Convert this color to HSL components.

Returns (h, s, l) with hue in degrees (0.0..360.0) and saturation and lightness in 0.0..=1.0. Palette and indexed colors are resolved to RGB first. If the color is gray (r == g == b), saturation is 0.0 and hue is reported as 0.0.

Source

pub fn hex(s: &str) -> Option<Color>

Parse a hex color string into Color::Rgb.

The leading # is optional. Accepted forms are:

  • rgb / #rgb: shorthand, each nibble is doubled (f becomes ff).
  • rrggbb / #rrggbb: one byte per channel.
  • rrggbbaa / #rrggbbaa: a trailing alpha byte is parsed for validity but ignored.

Returns None for any other length or for non-hexadecimal input. The function does not panic.

Source

pub fn hsl(h: f32, s: f32, l: f32) -> Color

Construct Color::Rgb from HSL.

h is hue in degrees and is wrapped into 0.0..360.0 with f32::rem_euclid. s (saturation) and l (lightness) are clamped to 0.0..=1.0. The intermediate RGB values are rounded to the nearest byte and clamped to 0..=255.

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 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
§

impl<T> ErasedDestructor for T
where T: 'static,

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