Skip to main content

Rgb

Struct Rgb 

Source
pub struct Rgb {
    pub r: u8,
    pub g: u8,
    pub b: u8,
}
Expand description

A 24-bit sRGB colour.

Fields§

§r: u8

Red channel.

§g: u8

Green channel.

§b: u8

Blue channel.

Implementations§

Source§

impl Rgb

Source

pub const fn new(r: u8, g: u8, b: u8) -> Self

Creates a colour from its channels.

Source

pub fn parse_hex(text: &str) -> Option<Self>

Parses #RRGGBB or #RGB, in either letter case.

Source

pub fn mix(self, other: Self, t: f32) -> Self

Blends towards other: t = 0 gives self, t = 1 gives other. t is clamped to 0..=1.

Source

pub fn relative_luminance(self) -> f64

WCAG relative luminance, from 0 (black) to 1 (white).

Source

pub fn contrast_ratio(self, other: Self) -> f64

WCAG contrast ratio between two colours, from 1 to 21.

Source

pub fn oklab(self) -> [f64; 3]

The colour in the OKLab perceptual space as [L, a, b].

Source

pub fn perceptual_distance(self, other: Self) -> f64

Euclidean distance in OKLab. Around 0.02 is barely visible; 0.10 reads as a clearly different colour.

Source

pub fn to_ansi256(self) -> u8

Nearest entry of the xterm 256-colour palette (indices 16..=255).

Source

pub fn to_ansi256_text(self, bg: Self) -> u8

The entry of the xterm 256-colour palette this colour takes as text on bg.

Mostly the nearest entry, as Rgb::to_ansi256 gives it. Faint text, such as a page dimmed behind a dialog, can land on an entry that no longer reads on its background’s — under 1.6:1 in the WCAG ratio, where a glyph starts to disappear; it then takes the entry closest to it in OKLab that still keeps 1.6:1, so faint text stays faint rather than vanishing. Text drawn in the very colour of its background is left as it is, since that is a fill rather than something to read.

Source

pub fn to_ansi16(self) -> u8

Nearest of the 16 standard terminal colours (xterm defaults), by distance alone.

This is the plain reduction: on a dark screen every surface tone lands on black here. What a sixteen-colour frame actually shows is Rgb::to_ansi16_on for backgrounds and Rgb::to_ansi16_text for text, which keep lifted surfaces and faint text apart from the ground.

Source

pub fn from_ansi16(index: u8) -> Self

The colour of entry index of the 16 standard terminal colours, as xterm shows them by default; an index past 15 gives the last entry, white. A terminal may be themed differently, so these are the colours a reduction can count on, not the ones a person necessarily sees.

Source

pub fn from_ansi256(index: u8) -> Self

The colour of entry index of the xterm 256-colour palette as xterm shows it by default: the 16 standard colours as Rgb::from_ansi16 gives them, then the 6×6×6 colour cube, then the 24 greys from 8 to 238.

Source

pub fn to_ansi16_on(self, ground: Self) -> u8

The entry of the 16 standard colours this colour takes as a background on a screen whose ground is ground (the theme’s canvas).

Mostly the nearest entry, as Rgb::to_ansi16 gives it. The exception is a tone the eye tells from the ground in full colour — at least 0.05 away in OKLab, the same distance under which a floating surface melts into the ground — that would still land on the ground’s own entry: it moves one step along the grey ladder black, bright black, white, bright white, away from the ground. On a dark theme the ground stays black and raised surfaces, tab strips and dialogs become bright black; on a light theme they step down from bright white to white. Panels a shade off the ground stay on it, as in full colour they barely differ.

Source

pub fn to_ansi16_text(self, bg: Self, ground: Self) -> u8

The entry of the 16 standard colours this colour takes as text on bg, on a screen whose ground is ground.

Both colours are reduced as Rgb::to_ansi16_on does. When the text would then no longer read on its background — under 1.6:1 in the WCAG ratio, which is where a glyph starts to disappear — it takes the quietest grey that still keeps 3:1 against it: faint text stays faint rather than vanishing. On bright black that is white, on black bright black. Text drawn in the very colour of its background is left as it is, since that is a fill rather than something to read.

Trait Implementations§

Source§

impl Clone for Rgb

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Rgb

Source§

impl Debug for Rgb

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Rgb

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Rgb

Source§

impl From<Rgb> for CellColor

Source§

fn from(color: Rgb) -> Self

A hard-coded colour, written #RRGGBB.

Source§

impl Hash for Rgb

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Rgb

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Rgb

Auto Trait Implementations§

§

impl Freeze for Rgb

§

impl RefUnwindSafe for Rgb

§

impl Send for Rgb

§

impl Sync for Rgb

§

impl Unpin for Rgb

§

impl UnsafeUnpin for Rgb

§

impl UnwindSafe for Rgb

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToLine for T
where T: Display,

Source§

fn to_line(&self) -> Line<'_>

Converts the value to a Line.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSpan for T
where T: Display,

Source§

fn to_span(&self) -> Span<'_>

Converts the value to a Span.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToText for T
where T: Display,

Source§

fn to_text(&self) -> Text<'_>

Converts the value to a Text.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.