Trait ansi_colours::AsRGB

source ·
pub trait AsRGB {
    // Required method
    fn as_u32(&self) -> u32;

    // Provided method
    fn to_ansi256(&self) -> u8 { ... }
}
Expand description

Type which represents a colour convertible to sRGB. Used to provide overloaded versions of ansi256_from_rgb function.

Required Methods§

source

fn as_u32(&self) -> u32

Returns representation of the sRGB colour as a 24-bit 0xRRGGBB integer.

Provided Methods§

source

fn to_ansi256(&self) -> u8

Returns index of a colour in 256-colour ANSI palette approximating given sRGB colour.

This is provided by default and uses Self::as_u32 to determine 24-bit sRGB representation of the colour.

An implementation should provide its own definition if it can offer a more direct approximation. For example, if Self represents shades of grey, it’s faster to use ansi256_from_grey than relay on to_u32 conversion; or if it represents a variant which can store index in the palette or an RGB colour, it’s better to either return the index or perform approximation depending on the variant.

Implementations on Foreign Types§

source§

impl<C: Component> AsRGB for Gray<C>

source§

fn to_ansi256(&self) -> u8

Returns index of a colour in 256-colour ANSI palette approximating given shade of grey.

This implementation is present only if rgb crate feature is enabled. Implementation is provided for u8 and u16 colour component types.

Examples
use ansi_colours::ansi256_from_rgb;

assert_eq!(244, ansi256_from_rgb(rgb::alt::Gray::<u8>(128)));
assert_eq!(244, ansi256_from_rgb(rgb::alt::Gray::<u16>(33023)));
source§

fn as_u32(&self) -> u32

source§

impl AsRGB for Color

source§

fn as_u32(&self) -> u32

Returns sRGB colour corresponding to escape code represented by termcolor::Color.

Behaves slightly differently depending on the variant of the enum.

  • For named colour variants (Black, Red etc. up till White), returns corresponding system colour with indexes going from 0 to 7.
  • For Ansi256 variant returns colour corresponding to specified index. See rgb_from_ansi256.
  • For Rgb variant converts it to 24-bit 0xRRGGBB representation.

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

source§

fn to_ansi256(&self) -> u8

Returns index of a colour in 256-colour ANSI palette approximating given sRGB colour.

Behaves slightly differently depending on the variant of the enum.

  • For named colour variants (Black, Red etc. up till White), returns index going from 0 to 7.
  • For Ansi256 variant simply returns index encoded in the variant.
  • Lastly, for Rgb variant, approximates the colour and returns index of closest colour in 256-colour palette.

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

Examples
use ansi_colours::AsRGB;

assert_eq!(  0, termcolor::Color::Black.to_ansi256());
assert_eq!(  7, termcolor::Color::White.to_ansi256());
assert_eq!( 42, termcolor::Color::Ansi256(42).to_ansi256());
assert_eq!( 16, termcolor::Color::Rgb(  0,   0,   0).to_ansi256());
assert_eq!( 16, termcolor::Color::Rgb(  1,   1,   1).to_ansi256());
assert_eq!( 16, termcolor::Color::Rgb(  0,   1,   2).to_ansi256());
assert_eq!( 67, termcolor::Color::Rgb( 95, 135, 175).to_ansi256());
assert_eq!(231, termcolor::Color::Rgb(255, 255, 255).to_ansi256());
source§

impl<C: Component> AsRGB for BGR<C>

source§

fn as_u32(&self) -> u32

Returns representation of the sRGB colour as a 24-bit 0xRRGGBB integer.

This implementation is present only if rgb crate feature is enabled. Implementation is provided for u8 and u16 colour component types.

Examples
use ansi_colours::{AsRGB, ansi256_from_rgb};
use rgb::alt::{BGR8, BGR16};

assert_eq!(0x123456, BGR8 { b: 0x56, g: 0x34, r: 0x12 }.as_u32());

assert_eq!( 16, ansi256_from_rgb(BGR8 { r:   1, g:   1, b:   1 }));
assert_eq!( 16, ansi256_from_rgb(BGR8 { r:   0, g:   1, b:   2 }));
assert_eq!( 67, ansi256_from_rgb(BGR8 { r:  95, g: 135, b: 175 }));
assert_eq!(231, ansi256_from_rgb(BGR8 { r: 255, g: 255, b: 255 }));

assert_eq!(0x123456, BGR16 { b: 0x56ef, g: 0x34cd, r: 0x12ab }.as_u32());

assert_eq!( 16, ansi256_from_rgb(BGR16 { r:   256, g:   511, b:   256 }));
assert_eq!( 16, ansi256_from_rgb(BGR16 { r:   128, g:   256, b:   512 }));
assert_eq!( 67, ansi256_from_rgb(BGR16 { r: 24500, g: 34600, b: 44800 }));
assert_eq!(231, ansi256_from_rgb(BGR16 { r: 65535, g: 65535, b: 65535 }));
source§

impl AsRGB for [u8; 3]

source§

fn as_u32(&self) -> u32

source§

impl<'a, T: AsRGB + ?Sized> AsRGB for &'a T

source§

fn as_u32(&self) -> u32

source§

impl AsRGB for (u8, u8, u8)

source§

fn as_u32(&self) -> u32

source§

impl<C: Component> AsRGB for RGB<C>

source§

fn as_u32(&self) -> u32

Returns representation of the sRGB colour as a 24-bit 0xRRGGBB integer.

This implementation is present only if rgb crate feature is enabled. Implementation is provided for u8 and u16 colour component types.

Examples
use ansi_colours::{AsRGB, ansi256_from_rgb};

assert_eq!(0x123456, rgb::RGB8::new(0x12, 0x34, 0x56).as_u32());

assert_eq!( 16, ansi256_from_rgb(rgb::RGB8::new(  1,   1,   1)));
assert_eq!( 16, ansi256_from_rgb(rgb::RGB8::new(  0,   1,   2)));
assert_eq!( 67, ansi256_from_rgb(rgb::RGB8::new( 95, 135, 175)));
assert_eq!(231, ansi256_from_rgb(rgb::RGB8::new(255, 255, 255)));

assert_eq!(0x123456, rgb::RGB16::new(0x12ab, 0x34cd, 0x56ef).as_u32());

assert_eq!( 16, ansi256_from_rgb(rgb::RGB16::new(  256,   511,   256)));
assert_eq!( 16, ansi256_from_rgb(rgb::RGB16::new(  128,   256,   512)));
assert_eq!( 67, ansi256_from_rgb(rgb::RGB16::new(24500, 34600, 44800)));
assert_eq!(231, ansi256_from_rgb(rgb::RGB16::new(65535, 65535, 65535)));
source§

impl AsRGB for u32

Representation of an RGB colour as 24-bit 0xRRGGBB integer.

source§

fn as_u32(&self) -> u32

source§

impl AsRGB for Colour

source§

fn as_u32(&self) -> u32

Returns sRGB colour corresponding to escape code represented by the object.

Behaves slightly differently depending on the variant of the enum.

  • For named colour variants (Black, Red etc. up till White), returns corresponding system colour with indexes going from 0 to 7.
  • For Fixed variant returns colour corresponding to specified index. See rgb_from_ansi256.
  • For RGB variant converts it to 24-bit 0xRRGGBB representation.

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

source§

fn to_ansi256(&self) -> u8

Returns index of a colour in 256-colour ANSI palette approximating given sRGB colour.

Behaves slightly differently depending on the variant of the enum.

  • For named colour variants (Black, Red etc. up till White), returns index going from 0 to 7.
  • For Fixed variant simply returns index encoded in the variant.
  • Lastly, for RGB variant, approximates the colour and returns index of closest colour in 256-colour palette.

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

Examples
use ansi_colours::AsRGB;

assert_eq!(  0, ansi_term::Colour::Black.to_ansi256());
assert_eq!(  7, ansi_term::Colour::White.to_ansi256());
assert_eq!( 42, ansi_term::Colour::Fixed(42).to_ansi256());
assert_eq!( 16, ansi_term::Colour::RGB(  0,   0,   0).to_ansi256());
assert_eq!( 16, ansi_term::Colour::RGB(  1,   1,   1).to_ansi256());
assert_eq!( 16, ansi_term::Colour::RGB(  0,   1,   2).to_ansi256());
assert_eq!( 67, ansi_term::Colour::RGB( 95, 135, 175).to_ansi256());
assert_eq!(231, ansi_term::Colour::RGB(255, 255, 255).to_ansi256());

Implementors§