Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement 24-bit color, as specified by ISO-8613-3 #13

Merged
merged 1 commit into from Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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