Skip to content

Commit

Permalink
Merge pull request #13 from joshtriplett/24-bit-color
Browse files Browse the repository at this point in the history
Implement 24-bit color, as specified by ISO-8613-3
  • Loading branch information
ogham committed Jun 21, 2016
2 parents 10dbe8e + 34cbc8f commit e8e5a03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -97,6 +97,12 @@ Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup");
The first sixteen of these values are the same as the normal and bold standard colour variants.
There’s nothing stopping you from using these as `Fixed` colours instead, but there’s nothing to be gained by doing so either.

You can also access full 24-bit color by using the `RGB` colour variant, which takes separate `u8` arguments for red, green, and blue:

```rust
use ansi_term::Colour::RGB;
RGB(70, 130, 180).paint("Steel blue");
```

## Combining successive coloured strings

Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Expand Up @@ -89,6 +89,11 @@
//! `Fixed` colours instead, but there’s nothing to be gained by doing so
//! either.
//!
//! You can also access full 24-bit color by using the `RGB` colour variant,
//! which takes separate `u8` arguments for red, green, and blue:
//!
//! use ansi_term::Colour::RGB;
//! RGB(70, 130, 180).paint("Steel blue");
//!
//! ## Combining successive coloured strings
//!
Expand Down Expand Up @@ -257,6 +262,9 @@ pub enum Colour {
/// It might make more sense to look at a [colour chart][cc].
/// [cc]: https://upload.wikimedia.org/wikipedia/en/1/15/Xterm_256color_chart.svg
Fixed(u8),

/// A 24-bit RGB color, as specified by ISO-8613-3.
RGB(u8, u8, u8),
}

/// Color is a type alias for Colour for those who can't be bothered.
Expand All @@ -280,6 +288,7 @@ impl Colour {
Cyan => write!(f, "36"),
White => write!(f, "37"),
Fixed(num) => write!(f, "38;5;{}", &num),
RGB(r,g,b) => write!(f, "38;2;{};{};{}", &r, &g, &b),
}
}

Expand All @@ -294,6 +303,7 @@ impl Colour {
Cyan => write!(f, "46"),
White => write!(f, "47"),
Fixed(num) => write!(f, "48;5;{}", &num),
RGB(r,g,b) => write!(f, "48;2;{};{};{}", &r, &g, &b),
}
}

Expand Down Expand Up @@ -706,6 +716,10 @@ mod tests {
test!(fixed: Fixed(100); "hi" => "\x1B[38;5;100mhi\x1B[0m");
test!(fixed_on_purple: Fixed(100).on(Purple); "hi" => "\x1B[45;38;5;100mhi\x1B[0m");
test!(fixed_on_fixed: Fixed(100).on(Fixed(200)); "hi" => "\x1B[48;5;200;38;5;100mhi\x1B[0m");
test!(rgb: RGB(70,130,180); "hi" => "\x1B[38;2;70;130;180mhi\x1B[0m");
test!(rgb_on_blue: RGB(70,130,180).on(Blue); "hi" => "\x1B[44;38;2;70;130;180mhi\x1B[0m");
test!(blue_on_rgb: Blue.on(RGB(70,130,180)); "hi" => "\x1B[48;2;70;130;180;34mhi\x1B[0m");
test!(rgb_on_rgb: RGB(70,130,180).on(RGB(5,10,15)); "hi" => "\x1B[48;2;5;10;15;38;2;70;130;180mhi\x1B[0m");
test!(bold: Style::new().bold(); "hi" => "\x1B[1mhi\x1B[0m");
test!(underline: Style::new().underline(); "hi" => "\x1B[4mhi\x1B[0m");
test!(bunderline: Style::new().bold().underline(); "hi" => "\x1B[1;4mhi\x1B[0m");
Expand Down

0 comments on commit e8e5a03

Please sign in to comment.