#[non_exhaustive]pub enum Color {
}Expand description
Terminal color.
Covers the standard 16 named colors, 256-color palette indices, and
24-bit RGB true color. Use Color::Reset to restore the terminal’s
default foreground or background.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Reset
Reset to the terminal’s default color.
Black
Standard black (color index 0).
Red
Standard red (color index 1).
Green
Standard green (color index 2).
Yellow
Standard yellow (color index 3).
Blue
Standard blue (color index 4).
Magenta
Standard magenta (color index 5).
Cyan
Standard cyan (color index 6).
White
Standard white (color index 7).
DarkGray
Bright black / dark gray (color index 8).
LightRed
Bright red (color index 9).
LightGreen
Bright green (color index 10).
LightYellow
Bright yellow (color index 11).
LightBlue
Bright blue (color index 12).
LightMagenta
Bright magenta (color index 13).
LightCyan
Bright cyan (color index 14).
LightWhite
Bright white (color index 15).
Rgb(u8, u8, u8)
24-bit true color.
Indexed(u8)
256-color palette index.
Implementations§
Source§impl Color
impl Color
Sourcepub fn luminance(self) -> f32
pub fn luminance(self) -> f32
Compute relative luminance using ITU-R BT.709 coefficients.
Returns a value in [0.0, 1.0] where 0 is darkest and 1 is brightest.
Use this to determine whether text on a given background should be
light or dark.
§Example
use slt::Color;
let dark = Color::Rgb(30, 30, 46);
assert!(dark.luminance() < 0.15);
let light = Color::Rgb(205, 214, 244);
assert!(light.luminance() > 0.6);Sourcepub fn contrast_fg(bg: Color) -> Color
pub fn contrast_fg(bg: Color) -> Color
Return a contrasting foreground color for the given background.
Uses the WCAG 2.1 relative luminance threshold (0.179) to decide
between white and black text. For theme-aware contrast, prefer using
this over hardcoding theme.bg as the foreground.
§Example
use slt::Color;
let bg = Color::Rgb(189, 147, 249); // Dracula purple
let fg = Color::contrast_fg(bg);
// Dracula purple → white (WCAG luminance 0.385 < 0.179 threshold)Sourcepub fn blend(self, other: Color, alpha: f32) -> Color
pub fn blend(self, other: Color, alpha: f32) -> Color
Blend this color over another with the given alpha.
alpha is in [0.0, 1.0] where 0.0 returns other unchanged and
1.0 returns self unchanged. Both colors are resolved to RGB.
§Example
use slt::Color;
let white = Color::Rgb(255, 255, 255);
let black = Color::Rgb(0, 0, 0);
let gray = white.blend(black, 0.5);
// ≈ Rgb(128, 128, 128)Sourcepub fn lighten(self, amount: f32) -> Color
pub fn lighten(self, amount: f32) -> Color
Lighten this color by the given amount (0.0–1.0).
Blends toward white. amount = 0.0 returns the original color;
amount = 1.0 returns white.
Sourcepub fn darken(self, amount: f32) -> Color
pub fn darken(self, amount: f32) -> Color
Darken this color by the given amount (0.0–1.0).
Blends toward black. amount = 0.0 returns the original color;
amount = 1.0 returns black.
Sourcepub fn contrast_ratio(a: Color, b: Color) -> f32
pub fn contrast_ratio(a: Color, b: Color) -> f32
Compute the WCAG 2.1 contrast ratio between two colors.
Returns a value >= 1.0. A ratio >= 4.5 meets WCAG AA for normal text;
= 3.0 meets AA for large text.
§Example
use slt::Color;
let ratio = Color::contrast_ratio(Color::White, Color::Black);
assert!(ratio > 15.0);Sourcepub fn meets_contrast_aa(fg: Color, bg: Color) -> bool
pub fn meets_contrast_aa(fg: Color, bg: Color) -> bool
Returns true if the contrast ratio between two colors meets WCAG AA
for normal text (ratio >= 4.5).
Sourcepub fn downsampled(self, depth: ColorDepth) -> Color
pub fn downsampled(self, depth: ColorDepth) -> Color
Downsample this color to fit the given color depth.
TrueColor: returns self unchanged.EightBit: convertsRgbto the nearestIndexedcolor.Basic: convertsRgbandIndexedto the nearest named color.NoColor: returnsColor::Reset— emit no ANSI color at all.
Named colors (Red, Green, etc.) and Reset pass through at
depths other than NoColor.