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§

source

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.

source

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.

source

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§

source

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.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl ColourExt for Colour

source§

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

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)

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

source§

fn approx_rgb(r: u8, g: u8, b: u8) -> Self

Constructs an ANSI 256 colour which best approximates given sRGB colour.

This implementation is present only if anstyle crate feature is enabled.

§Examples
use ansi_colours::ColourExt;
use anstyle::{Ansi256Color, Color};

assert_eq!(Color::Ansi256(Ansi256Color( 16)),
           Color::approx_rgb(  0,   0,   0));
assert_eq!(Color::Ansi256(Ansi256Color( 16)),
           Color::approx_rgb(  0,   1,   2));
assert_eq!(Color::Ansi256(Ansi256Color( 67)),
           Color::approx_rgb( 95, 135, 175));
assert_eq!(Color::Ansi256(Ansi256Color(231)),
           Color::approx_rgb(255, 255, 255));
source§

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 anstyle crate feature is enabled.

§Examples
use ansi_colours::ColourExt;
use anstyle::{Ansi256Color, AnsiColor, Color, RgbColor};

assert_eq!(Color::Ansi(AnsiColor::Red),
           Color::Ansi(AnsiColor::Red).to_256());
assert_eq!(Color::Ansi256(Ansi256Color( 11)),
           Color::Ansi256(Ansi256Color( 11)).to_256());
assert_eq!(Color::Ansi256(Ansi256Color( 16)),
           Color::Rgb(RgbColor(  0,   0,   0)).to_256());
assert_eq!(Color::Ansi256(Ansi256Color( 16)),
           Color::Rgb(RgbColor(  0,   1,   2)).to_256());
assert_eq!(Color::Ansi256(Ansi256Color( 67)),
           Color::Rgb(RgbColor( 95, 135, 175)).to_256());
assert_eq!(Color::Ansi256(Ansi256Color(231)),
           Color::Rgb(RgbColor(255, 255, 255)).to_256());
source§

fn to_rgb(&self) -> (u8, u8, u8)

Converts the colour into sRGB.

AnsiColour and Ansi256Color colour variants are converted into sRGB using rgb_from_ansi256 function. Rgb colours are returned unchanged.

This implementation is present only if anstyle crate feature is enabled.

§Examples
use ansi_colours::ColourExt;
use anstyle::{Ansi256Color, AnsiColor, Color, RgbColor};

assert_eq!((  0,   0,   0), Color::Ansi256(Ansi256Color( 16)).to_rgb());
assert_eq!(( 95, 135, 175), Color::Ansi256(Ansi256Color( 67)).to_rgb());
assert_eq!((255, 255, 255), Color::Ansi256(Ansi256Color(231)).to_rgb());
assert_eq!((238, 238, 238), Color::Ansi256(Ansi256Color(255)).to_rgb());
assert_eq!(( 42,  24,   0), Color::Rgb(RgbColor(42, 24, 0)).to_rgb());
source§

impl ColourExt for Color

source§

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

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)

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());
source§

impl ColourExt for Ansi256Color

source§

fn approx_rgb(r: u8, g: u8, b: u8) -> Self

Constructs an colour which best approximates given sRGB colour.

This implementation is present only if anstyle crate feature is enabled.

§Examples
use ansi_colours::ColourExt;
use anstyle::Ansi256Color;

assert_eq!(Ansi256Color( 16), Ansi256Color::approx_rgb(  0,   0,   0));
assert_eq!(Ansi256Color( 16), Ansi256Color::approx_rgb(  0,   1,   2));
assert_eq!(Ansi256Color( 67), Ansi256Color::approx_rgb( 95, 135, 175));
assert_eq!(Ansi256Color(231), Ansi256Color::approx_rgb(255, 255, 255));
source§

fn to_256(&self) -> Self

Returns self.

This implementation is present only if anstyle crate feature is enabled.

source§

fn to_rgb(&self) -> (u8, u8, u8)

Converts the colour into sRGB.

This implementation is present only if anstyle crate feature is enabled.

§Examples
use ansi_colours::ColourExt;
use anstyle::Ansi256Color;

assert_eq!((  0,   0,   0), Ansi256Color( 16).to_rgb());
assert_eq!(( 95, 135, 175), Ansi256Color( 67).to_rgb());
assert_eq!((255, 255, 255), Ansi256Color(231).to_rgb());
assert_eq!((238, 238, 238), Ansi256Color(255).to_rgb());

Implementors§