2016-03-07 16:39:25 +00:00
|
|
|
/// A terminal color.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
|
|
|
pub enum Color {
|
|
|
|
/// Black.
|
|
|
|
Black,
|
|
|
|
/// Red.
|
|
|
|
Red,
|
|
|
|
/// Green.
|
|
|
|
Green,
|
|
|
|
/// Yellow.
|
|
|
|
Yellow,
|
|
|
|
/// Blue.
|
|
|
|
Blue,
|
|
|
|
/// Megenta.
|
|
|
|
Magenta,
|
|
|
|
/// Cyan.
|
|
|
|
Cyan,
|
|
|
|
/// White.
|
|
|
|
White,
|
|
|
|
/// High-intensity black.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightBlack,
|
2016-03-07 16:39:25 +00:00
|
|
|
/// High-intensity red.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightRed,
|
2016-03-07 16:39:25 +00:00
|
|
|
/// High-intensity green.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightGreen,
|
2016-03-07 16:39:25 +00:00
|
|
|
/// High-intensity yellow.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightYellow,
|
2016-03-07 16:39:25 +00:00
|
|
|
/// High-intensity blue.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightBlue,
|
2016-03-09 08:39:22 +00:00
|
|
|
/// High-intensity magenta.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightMagenta,
|
2016-03-07 16:39:25 +00:00
|
|
|
/// High-intensity cyan.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightCyan,
|
2016-03-07 16:39:25 +00:00
|
|
|
/// High-intensity white.
|
2016-03-09 10:19:51 +00:00
|
|
|
LightWhite,
|
2016-03-08 07:27:59 +00:00
|
|
|
/// 216-color (r, g, b ≤ 5) RGB.
|
2016-03-07 16:39:25 +00:00
|
|
|
Rgb(u8, u8, u8),
|
|
|
|
/// Grayscale (max value: 24)
|
|
|
|
Grayscale(u8),
|
|
|
|
}
|
|
|
|
|
|
|
|
use Color::*;
|
|
|
|
|
|
|
|
impl Color {
|
2016-03-09 08:39:22 +00:00
|
|
|
/// Get the corresponding ANSI value.
|
|
|
|
///
|
|
|
|
/// Panics
|
|
|
|
/// ======
|
|
|
|
///
|
|
|
|
/// This method will panic in debug mode, if `self` is invalid (that is, the values are out of
|
|
|
|
/// bound).
|
2016-03-07 16:39:25 +00:00
|
|
|
pub fn to_ansi_val(self) -> u8 {
|
|
|
|
self.debug_check();
|
|
|
|
|
|
|
|
match self {
|
|
|
|
Black => 0x0,
|
|
|
|
Red => 0x1,
|
|
|
|
Green => 0x2,
|
|
|
|
Yellow => 0x3,
|
|
|
|
Blue => 0x4,
|
|
|
|
Magenta => 0x5,
|
|
|
|
Cyan => 0x6,
|
|
|
|
White => 0x7,
|
2016-03-09 10:19:51 +00:00
|
|
|
LightBlack => 0x8,
|
|
|
|
LightRed => 0x9,
|
|
|
|
LightGreen => 0xA,
|
|
|
|
LightYellow => 0xB,
|
|
|
|
LightBlue => 0xC,
|
|
|
|
LightMagenta => 0xD,
|
|
|
|
LightCyan => 0xE,
|
|
|
|
LightWhite => 0xF,
|
2016-03-07 16:39:25 +00:00
|
|
|
Rgb(r, g, b) => 16 + 36 * r + 6 * g + b,
|
|
|
|
Grayscale(shade) => 0xE8 + shade,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-08 07:27:59 +00:00
|
|
|
fn debug_check(self) {
|
2016-03-07 16:39:25 +00:00
|
|
|
match self {
|
|
|
|
Rgb(r, g, b) => {
|
|
|
|
debug_assert!(r <= 5, "Red color fragment (r = {}) is out of bound. Make sure r ≤ 5.", r);
|
|
|
|
debug_assert!(g <= 5, "Green color fragment (g = {}) is out of bound. Make sure g ≤ 5.", g);
|
|
|
|
debug_assert!(b <= 5, "Blue color fragment (b = {}) is out of bound. Make sure b ≤ 5.", b);
|
|
|
|
},
|
|
|
|
Grayscale(shade) => {
|
|
|
|
// Unfortunately, there are a little less than fifty shades.
|
|
|
|
debug_assert!(shade < 24, "Grayscale out of bound (shade = {}). There are only 24 shades of gray.", shade);
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|