#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
BrightBlack,
BrightRed,
BrightGreen,
BrightYellow,
BrightBlue,
BrightMagenta,
BrightCyan,
BrightWhite,
Rgb(u8, u8, u8),
Fixed(u8),
Reset,
}
impl Color {
pub fn to_fg_str(&self) -> String {
match self {
Color::Black => "\x1b[30m".to_string(),
Color::Red => "\x1b[31m".to_string(),
Color::Green => "\x1b[32m".to_string(),
Color::Yellow => "\x1b[33m".to_string(),
Color::Blue => "\x1b[34m".to_string(),
Color::Magenta => "\x1b[35m".to_string(),
Color::Cyan => "\x1b[36m".to_string(),
Color::White => "\x1b[37m".to_string(),
Color::BrightBlack => "\x1b[90m".to_string(),
Color::BrightRed => "\x1b[91m".to_string(),
Color::BrightGreen => "\x1b[92m".to_string(),
Color::BrightYellow => "\x1b[93m".to_string(),
Color::BrightBlue => "\x1b[94m".to_string(),
Color::BrightMagenta => "\x1b[95m".to_string(),
Color::BrightCyan => "\x1b[96m".to_string(),
Color::BrightWhite => "\x1b[97m".to_string(),
Color::Rgb(r, g, b) => format!("\x1b[38;2;{};{};{}m", r, g, b),
Color::Fixed(n) => format!("\x1b[38;5;{}m", n),
Color::Reset => "\x1b[39m".to_string(),
}
}
pub fn to_bg_str(&self) -> String {
match self {
Color::Black => "\x1b[40m".to_string(),
Color::Red => "\x1b[41m".to_string(),
Color::Green => "\x1b[42m".to_string(),
Color::Yellow => "\x1b[43m".to_string(),
Color::Blue => "\x1b[44m".to_string(),
Color::Magenta => "\x1b[45m".to_string(),
Color::Cyan => "\x1b[46m".to_string(),
Color::White => "\x1b[47m".to_string(),
Color::BrightBlack => "\x1b[100m".to_string(),
Color::BrightRed => "\x1b[101m".to_string(),
Color::BrightGreen => "\x1b[102m".to_string(),
Color::BrightYellow => "\x1b[103m".to_string(),
Color::BrightBlue => "\x1b[104m".to_string(),
Color::BrightMagenta => "\x1b[105m".to_string(),
Color::BrightCyan => "\x1b[106m".to_string(),
Color::BrightWhite => "\x1b[107m".to_string(),
Color::Rgb(r, g, b) => format!("\x1b[48;2;{};{};{}m", r, g, b),
Color::Fixed(n) => format!("\x1b[48;5;{}m", n),
Color::Reset => "\x1b[49m".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_standard_colors() {
assert_eq!(Color::Red.to_fg_str(), "\x1b[31m");
assert_eq!(Color::Green.to_bg_str(), "\x1b[42m");
}
#[test]
fn test_rgb_colors() {
let rgb_color = Color::Rgb(255, 100, 50);
assert_eq!(rgb_color.to_fg_str(), "\x1b[38;2;255;100;50m");
}
}