Function ansi_colours::ansi256_from_rgb
source · pub fn ansi256_from_rgb<C: AsRGB>(rgb: C) -> u8
Expand description
Returns index of a colour in 256-colour ANSI palette approximating given sRGB colour.
Because the first 16 colours of the palette are not standardised and usually user-configurable, the function usually ignores them.
The first argument uses AsRGB
trait so that the function can be called in
multiple ways using different representations of RGB colours such as
0xRRGGBB
integer, (r, g, b)
tuple or [r, g, b]
array. Calling the
function is equivalent to calling AsRGB::to_ansi256
method.
Examples
assert_eq!( 16, ansi_colours::ansi256_from_rgb(0x000000));
assert_eq!( 16, ansi_colours::ansi256_from_rgb( ( 1, 1, 1)));
assert_eq!( 16, ansi_colours::ansi256_from_rgb( [ 0, 1, 2]));
assert_eq!( 67, ansi_colours::ansi256_from_rgb(&( 95, 135, 175)));
assert_eq!(231, ansi_colours::ansi256_from_rgb(&[255, 255, 255]));
let rgb = rgb::RGB8 { r: 175, g: 0, b: 215 };
assert_eq!(128, ansi_colours::ansi256_from_rgb(rgb));
let bgr = rgb::RGB8 { b: 215, g: 0, r: 175 };
assert_eq!(128, ansi_colours::ansi256_from_rgb(bgr));
let grey = rgb::alt::Gray::<u8>(128);
assert_eq!(244, ansi_colours::ansi256_from_rgb(grey));