Trait ansi_colours::ColourExt
source · pub trait ColourExt: Sized {
// Required methods
fn approx_rgb(r: u8, g: u8, b: u8) -> Self;
fn to_256(&self) -> Self;
fn to_rgb(&self) -> (u8, u8, u8);
// Provided method
fn approx<C: AsRGB>(rgb: C) -> Self { ... }
}
Expand description
Extension to types representing ANSI colours adding methods converting between RGB and indexed (a.k.a. fixed) representations.
Required Methods§
sourcefn approx_rgb(r: u8, g: u8, b: u8) -> Self
fn approx_rgb(r: u8, g: u8, b: u8) -> Self
Constructs an indexed colour which approximates given sRGB colour.
Examples
use ansi_colours::ColourExt;
use ansi_term::Colour;
assert_eq!(Colour::Fixed( 16), Colour::approx_rgb( 0, 0, 0));
assert_eq!(Colour::Fixed( 16), Colour::approx_rgb( 0, 1, 2));
assert_eq!(Colour::Fixed( 67), Colour::approx_rgb( 95, 135, 175));
assert_eq!(Colour::Fixed(231), Colour::approx_rgb(255, 255, 255));
Note that the example requires ansi_term
cargo feature to be enabled.
sourcefn to_256(&self) -> Self
fn to_256(&self) -> Self
Converts the colour into 256-colour-compatible format.
If the colour represents an RGB colour, converts it into indexed
representation using ansi256_from_rgb
function. Otherwise, returns
the colour unchanged.
Examples
use ansi_colours::ColourExt;
use ansi_term::Colour;
assert_eq!(Colour::Red, Colour::Red.to_256());
assert_eq!(Colour::Fixed( 11), Colour::Fixed(11).to_256());
assert_eq!(Colour::Fixed( 16), Colour::RGB( 0, 0, 0).to_256());
assert_eq!(Colour::Fixed( 16), Colour::RGB( 0, 1, 2).to_256());
assert_eq!(Colour::Fixed( 67), Colour::RGB( 95, 135, 175).to_256());
assert_eq!(Colour::Fixed(231), Colour::RGB(255, 255, 255).to_256());
Note that the example requires ansi_term
cargo feature to be enabled.
sourcefn to_rgb(&self) -> (u8, u8, u8)
fn to_rgb(&self) -> (u8, u8, u8)
Converts the colour colour into sRGB.
Named colours (black, red etc. through white) are treated like indexed
colours with indexes 0 through 7. Indexed colours are converted into
sRGB using rgb_from_ansi256
function. RGB colours are returned
unchanged.
Examples
use ansi_colours::ColourExt;
use ansi_term::Colour;
assert_eq!(( 0, 0, 0), Colour::Fixed( 16).to_rgb());
assert_eq!(( 95, 135, 175), Colour::Fixed( 67).to_rgb());
assert_eq!((255, 255, 255), Colour::Fixed(231).to_rgb());
assert_eq!((238, 238, 238), Colour::Fixed(255).to_rgb());
assert_eq!(( 42, 24, 0), Colour::RGB(42, 24, 0).to_rgb());
Note that the example requires ansi_term
cargo feature to be enabled.
Provided Methods§
sourcefn approx<C: AsRGB>(rgb: C) -> Self
fn approx<C: AsRGB>(rgb: C) -> Self
Constructs an indexed colour which approximates given sRGB colour.
Behaves like approx_rgb
but takes a single
argument which implements AsRGB
. Note that types which implement
ColourExt
typically also implement AsRGB
which means this method can
be called with Self
argument. It’s usually better to call
to_256
instead.