pub enum Color {
}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
impl Color
Sourcepub const fn rgb(r: u8, g: u8, b: u8) -> Self
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.
Sourcepub const fn named_index(self) -> Option<u8>
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.
Sourcepub const fn from_named(v: u8) -> Option<Self>
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.
Sourcepub const fn is_named(self) -> bool
pub const fn is_named(self) -> bool
Return whether this is one of the sixteen named ANSI colors.
Sourcepub const fn is_named_bright(self) -> bool
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.
Sourcepub fn to_rgb(self) -> (u8, u8, u8)
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.
Sourcepub fn to_hex(self) -> String
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.
Sourcepub fn to_hsl(self) -> (f32, f32, f32)
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.
Sourcepub fn hex(s: &str) -> Option<Color>
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 (fbecomesff).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.