Trait ansi_colours::ColourExt
source · pub trait ColourExt: Sized {
fn approx_rgb(r: u8, g: u8, b: u8) -> Self;
fn to_256(&self) -> Self;
fn to_rgb(&self) -> (u8, u8, u8);
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.
Implementations on Foreign Types§
source§impl ColourExt for Colour
impl ColourExt for Colour
source§fn approx_rgb(r: u8, g: u8, b: u8) -> Self
fn approx_rgb(r: u8, g: u8, b: u8) -> Self
Constructs a Fixed colour which approximates given sRGB colour.
This implementation is present only if ansi_term crate feature is
enabled.
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));source§fn 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 a Fixed
variant using ansi256_from_rgb function. Otherwise, returns the
colour unchanged.
This implementation is present only if ansi_term crate feature is
enabled.
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());source§fn to_rgb(&self) -> (u8, u8, u8)
fn to_rgb(&self) -> (u8, u8, u8)
Converts the colour into sRGB.
Named colours (Black, Red etc. through White) are treated like
Fixed colours with indexes 0 through 7. Fixed colours are converted
into sRGB using rgb_from_ansi256 function. RGB colours are
returned unchanged.
This implementation is present only if ansi_term crate feature is
enabled.
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());source§impl ColourExt for Color
impl ColourExt for Color
source§fn approx_rgb(r: u8, g: u8, b: u8) -> Self
fn approx_rgb(r: u8, g: u8, b: u8) -> Self
Constructs a Ansi256 colour which approximates given sRGB colour.
This implementation is present only if termcolor crate feature is
enabled.
Examples
use ansi_colours::ColourExt;
use termcolor::Color;
assert_eq!(Color::Ansi256( 16), Color::approx_rgb( 0, 0, 0));
assert_eq!(Color::Ansi256( 16), Color::approx_rgb( 0, 1, 2));
assert_eq!(Color::Ansi256( 67), Color::approx_rgb( 95, 135, 175));
assert_eq!(Color::Ansi256(231), Color::approx_rgb(255, 255, 255));source§fn 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 an Ansi256
variant using ansi256_from_rgb function. Otherwise, returns the
colour unchanged.
This implementation is present only if termcolor crate feature is
enabled.
Examples
use ansi_colours::ColourExt;
use termcolor::Color;
assert_eq!(Color::Red, Color::Red.to_256());
assert_eq!(Color::Ansi256( 11), Color::Ansi256(11).to_256());
assert_eq!(Color::Ansi256( 16), Color::Rgb( 0, 0, 0).to_256());
assert_eq!(Color::Ansi256( 16), Color::Rgb( 0, 1, 2).to_256());
assert_eq!(Color::Ansi256( 67), Color::Rgb( 95, 135, 175).to_256());
assert_eq!(Color::Ansi256(231), Color::Rgb(255, 255, 255).to_256());source§fn to_rgb(&self) -> (u8, u8, u8)
fn to_rgb(&self) -> (u8, u8, u8)
Converts the colour into sRGB.
Named colours (Black, Red etc. through White) are treated like
Ansi256 colours with indexes 0 through 7. Ansi256 colours are
converted into sRGB using rgb_from_ansi256 function. Rgb colours
are returned unchanged.
This implementation is present only if termcolor crate feature is
enabled.
Examples
use ansi_colours::ColourExt;
use termcolor::Color;
assert_eq!(( 0, 0, 0), Color::Ansi256( 16).to_rgb());
assert_eq!(( 95, 135, 175), Color::Ansi256( 67).to_rgb());
assert_eq!((255, 255, 255), Color::Ansi256(231).to_rgb());
assert_eq!((238, 238, 238), Color::Ansi256(255).to_rgb());
assert_eq!(( 42, 24, 0), Color::Rgb(42, 24, 0).to_rgb());