2016-09-07 10:39:32 +01:00
|
|
|
//! Color managemement.
|
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use termion::{color, style};
|
|
|
|
//!
|
|
|
|
//! use std::io;
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! println!("{}Red", color::Fg(color::Red));
|
|
|
|
//! println!("{}Blue", color::Fg(color::Blue));
|
|
|
|
//! println!("{}Back again", color::Fg(color::Reset));
|
|
|
|
//! }
|
|
|
|
//! ```
|
2016-07-23 15:40:27 +01:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
2016-03-07 16:39:25 +00:00
|
|
|
/// A terminal color.
|
2016-07-02 14:06:47 +01:00
|
|
|
pub trait Color {
|
2016-07-23 15:40:27 +01:00
|
|
|
/// Write the foreground version of this color.
|
|
|
|
fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result;
|
|
|
|
/// Write the background version of this color.
|
|
|
|
fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result;
|
2016-07-02 14:06:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! derive_color {
|
|
|
|
($doc:expr, $name:ident, $value:expr) => {
|
|
|
|
#[doc = $doc]
|
2016-10-03 06:15:12 +01:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2016-07-02 14:06:47 +01:00
|
|
|
pub struct $name;
|
|
|
|
|
|
|
|
impl Color for $name {
|
|
|
|
#[inline]
|
2016-07-23 15:40:27 +01:00
|
|
|
fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("38;5;", $value, "m"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("48;5;", $value, "m"))
|
2016-07-02 14:06:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
derive_color!("Black.", Black, "0");
|
|
|
|
derive_color!("Red.", Red, "1");
|
|
|
|
derive_color!("Green.", Green, "2");
|
|
|
|
derive_color!("Yellow.", Yellow, "3");
|
|
|
|
derive_color!("Blue.", Blue, "4");
|
|
|
|
derive_color!("Magenta.", Magenta, "5");
|
|
|
|
derive_color!("Cyan.", Cyan, "6");
|
|
|
|
derive_color!("White.", White, "7");
|
|
|
|
derive_color!("High-intensity light black.", LightBlack, "8");
|
|
|
|
derive_color!("High-intensity light red.", LightRed, "9");
|
|
|
|
derive_color!("High-intensity light green.", LightGreen, "10");
|
|
|
|
derive_color!("High-intensity light yellow.", LightYellow, "11");
|
|
|
|
derive_color!("High-intensity light blue.", LightBlue, "12");
|
|
|
|
derive_color!("High-intensity light magenta.", LightMagenta, "13");
|
|
|
|
derive_color!("High-intensity light cyan.", LightCyan, "14");
|
|
|
|
derive_color!("High-intensity light white.", LightWhite, "15");
|
2016-07-02 14:06:47 +01:00
|
|
|
|
|
|
|
/// An arbitrary ANSI color value.
|
2016-10-03 06:15:12 +01:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2016-07-02 14:06:47 +01:00
|
|
|
pub struct AnsiValue(pub u8);
|
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
impl AnsiValue {
|
2016-03-08 07:27:59 +00:00
|
|
|
/// 216-color (r, g, b ≤ 5) RGB.
|
2016-07-23 15:40:27 +01:00
|
|
|
pub fn rgb(r: u8, g: u8, b: u8) -> AnsiValue {
|
|
|
|
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);
|
2016-03-07 16:39:25 +00:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
AnsiValue(16 + 36 * r + 6 * g + b)
|
2016-03-07 16:39:25 +00:00
|
|
|
}
|
2016-03-09 16:18:31 +00:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
/// Grayscale color.
|
|
|
|
///
|
|
|
|
/// There are 24 shades of gray.
|
|
|
|
pub fn grayscale(shade: u8) -> AnsiValue {
|
|
|
|
// 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);
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
AnsiValue(0xE8 + shade)
|
2016-03-09 16:18:31 +00:00
|
|
|
}
|
2016-07-23 15:40:27 +01:00
|
|
|
}
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
impl Color for AnsiValue {
|
|
|
|
#[inline]
|
|
|
|
fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("38;5;{}m"), self.0)
|
2016-03-09 16:18:31 +00:00
|
|
|
}
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
#[inline]
|
|
|
|
fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("48;5;{}m"), self.0)
|
2016-07-02 14:06:47 +01:00
|
|
|
}
|
2016-07-23 15:40:27 +01:00
|
|
|
}
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
/// A truecolor RGB.
|
2016-10-22 07:40:06 +01:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
2016-07-23 15:40:27 +01:00
|
|
|
pub struct Rgb(pub u8, pub u8, pub u8);
|
2016-03-09 16:18:31 +00:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
impl Color for Rgb {
|
|
|
|
#[inline]
|
|
|
|
fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("38;2;{};{};{}m"), self.0, self.1, self.2)
|
2016-03-09 16:18:31 +00:00
|
|
|
}
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
#[inline]
|
|
|
|
fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("48;2;{};{};{}m"), self.0, self.1, self.2)
|
2016-03-09 16:18:31 +00:00
|
|
|
}
|
2016-07-23 15:40:27 +01:00
|
|
|
}
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-24 15:23:10 +01:00
|
|
|
/// Reset colors to defaults.
|
2016-10-09 06:13:41 +01:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2016-07-24 15:23:10 +01:00
|
|
|
pub struct Reset;
|
|
|
|
|
2016-07-24 15:24:49 +01:00
|
|
|
impl Color for Reset {
|
|
|
|
#[inline]
|
2016-07-24 15:23:10 +01:00
|
|
|
fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("39m"))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, csi!("49m"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
/// A foreground color.
|
2016-10-09 06:13:41 +01:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2016-07-23 15:40:27 +01:00
|
|
|
pub struct Fg<C: Color>(pub C);
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
impl<C: Color> fmt::Display for Fg<C> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.write_fg(f)
|
2016-07-02 14:06:47 +01:00
|
|
|
}
|
2016-07-23 15:40:27 +01:00
|
|
|
}
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
/// A background color.
|
2016-10-09 06:13:41 +01:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2016-07-23 15:40:27 +01:00
|
|
|
pub struct Bg<C: Color>(pub C);
|
2016-07-02 14:06:47 +01:00
|
|
|
|
2016-07-23 15:40:27 +01:00
|
|
|
impl<C: Color> fmt::Display for Bg<C> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
self.0.write_bg(f)
|
2016-03-09 16:18:31 +00:00
|
|
|
}
|
|
|
|
}
|