Crate ansi_colours
source · [−]Expand description
ansi_colours
is a library which converts between 24-bit sRGB colours and
8-bit colour palette used by ANSI terminals such as xterm on rxvt-unicode in
256-colour mode.
The most common use case is when using 24-bit colours in a terminal emulator which only support 8-bit colour palette. This package allows true-colours to be approximated by values supported by the terminal.
When mapping true-colour into available 256-colour palette (of which only 240 are actually usable), this package tries to balance accuracy and performance. It doesn’t implement the fastest algorithm nor is it the most accurate, instead it uses a formula which should be fast enough and accurate enough for most use-cases.
The crate defines an rgb
feature which adds support for the RGB
type
from rgb
crate. The feature is enabled by
default. RGB8
(a.k.a. RGB<u8>
) as well as RGB16
(a.k.a. RGB<u16>
)
types are supported. Currently, in the latter type the 8 least significant
bits of each channel are simply ignored.
Usage
Using this library with Cargo projects is as simple as adding a single dependency:
[dependencies]
ansi_colours = "^1.0"
and then using one of the two functions that the library provides:
use ansi_colours::*;
fn main() {
// Colour at given index:
println!("{:-3}: {:?}", 50, ansi_colours::rgb_from_ansi256(50));
// Approximate true-colour by colour in the palette:
let rgb = (100, 200, 150);
let index = ansi256_from_rgb(rgb);
println!("{:?} ~ {:-3} {:?}",
rgb, index, ansi_colours::rgb_from_ansi256(index));
// Approximate true-colour by colour in the palette:
let rgb = rgb::RGB8::new(100, 200, 150);
let index = ansi256_from_rgb(rgb);
println!("{:?} ~ {:-3} {:?}",
rgb, index, ansi_colours::rgb_from_ansi256(index));
}
Traits
A trait for types which (can) represent an sRGB colour. Used to provide
overloaded versions of ansi256_from_rgb
function.
Functions
Returns indexf o a colour in 256-colour ANSI palette approximating given shade of grey.
Returns index of a colour in 256-colour ANSI palette approximating given sRGB colour.
Returns sRGB colour corresponding to the index in the 256-colour ANSI palette.