pub struct Color {
pub name: String,
pub color_type: ColorType,
pub number: Option<u8>,
pub triplet: Option<ColorTriplet>,
}Expand description
A terminal color definition.
Colors can be:
- Default (terminal’s default color)
- Standard (16 ANSI colors, 0-15)
- EightBit (256 colors, 0-255)
- TrueColor (24-bit RGB)
§Examples
use rich_rs::Color;
// Parse from string
let red = Color::parse("red").unwrap();
let hex = Color::parse("#ff0000").unwrap();
let rgb = Color::parse("rgb(255,0,0)").unwrap();
let indexed = Color::parse("color(196)").unwrap();
// Create directly
let custom = Color::from_rgb(255, 128, 0);Fields§
§name: StringThe original name/representation of the color.
color_type: ColorTypeThe type of color.
number: Option<u8>The color number (for Standard and EightBit types).
triplet: Option<ColorTriplet>The RGB triplet (for TrueColor type).
Implementations§
Source§impl Color
impl Color
Sourcepub fn new(
name: impl Into<String>,
color_type: ColorType,
number: Option<u8>,
triplet: Option<ColorTriplet>,
) -> Self
pub fn new( name: impl Into<String>, color_type: ColorType, number: Option<u8>, triplet: Option<ColorTriplet>, ) -> Self
Create a new Color with the given parameters.
Sourcepub fn default_color() -> Self
pub fn default_color() -> Self
Create the default terminal color.
Sourcepub fn from_triplet(triplet: ColorTriplet) -> Self
pub fn from_triplet(triplet: ColorTriplet) -> Self
Create a color from an RGB triplet.
Sourcepub fn system(&self) -> ColorSystem
pub fn system(&self) -> ColorSystem
Get the color system for this color.
Sourcepub fn is_system_defined(&self) -> bool
pub fn is_system_defined(&self) -> bool
Check if this color is system-defined (Standard or Windows).
Sourcepub fn is_default(&self) -> bool
pub fn is_default(&self) -> bool
Check if this is the default color.
Sourcepub fn get_truecolor(&self, foreground: bool) -> ColorTriplet
pub fn get_truecolor(&self, foreground: bool) -> ColorTriplet
Get the RGB triplet for this color.
For Standard and EightBit colors, looks up the color in the palette. For Default colors, returns a typical terminal foreground/background.
Sourcepub fn parse(color: &str) -> Result<Self, ParseError>
pub fn parse(color: &str) -> Result<Self, ParseError>
Parse a color from a string.
Supported formats:
- Named colors: “red”, “bright_blue”, “grey50”
- Hex: “#ff0000” or “#f00”
- RGB: “rgb(255,0,0)”
- Indexed: “color(196)”
- Default: “default”
§Examples
use rich_rs::Color;
assert!(Color::parse("red").is_ok());
assert!(Color::parse("#ff0000").is_ok());
assert!(Color::parse("rgb(255,0,0)").is_ok());
assert!(Color::parse("color(196)").is_ok());
assert!(Color::parse("invalid").is_err());Sourcepub fn get_ansi_codes(&self, foreground: bool) -> Vec<String>
pub fn get_ansi_codes(&self, foreground: bool) -> Vec<String>
Get the ANSI escape codes for this color.
Returns a vector of strings that should be joined with “;” to form the SGR (Select Graphic Rendition) parameter.
§Arguments
foreground- If true, generate foreground color codes; otherwise background.
§Examples
use rich_rs::Color;
let red = Color::parse("red").unwrap();
assert_eq!(red.get_ansi_codes(true), vec!["31"]);
assert_eq!(red.get_ansi_codes(false), vec!["41"]);
let bright_red = Color::parse("bright_red").unwrap();
assert_eq!(bright_red.get_ansi_codes(true), vec!["91"]);
let color256 = Color::parse("color(196)").unwrap();
assert_eq!(color256.get_ansi_codes(true), vec!["38", "5", "196"]);
let rgb = Color::parse("#ff0000").unwrap();
assert_eq!(rgb.get_ansi_codes(true), vec!["38", "2", "255", "0", "0"]);Sourcepub fn downgrade(&self, system: ColorSystem) -> Self
pub fn downgrade(&self, system: ColorSystem) -> Self
Downgrade this color to a lower color system.
§Arguments
system- The target color system.
§Returns
A new Color that can be represented in the target system.
§Examples
use rich_rs::{Color, ColorSystem, ColorType};
let rgb = Color::from_rgb(255, 100, 50);
let downgraded = rgb.downgrade(ColorSystem::EightBit);
assert_eq!(downgraded.color_type, ColorType::EightBit);Trait Implementations§
Source§impl From<Color> for SimpleColor
impl From<Color> for SimpleColor
Source§impl From<SimpleColor> for Color
impl From<SimpleColor> for Color
Source§fn from(simple: SimpleColor) -> Self
fn from(simple: SimpleColor) -> Self
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 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§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.