Struct Color

Source
#[non_exhaustive]
pub struct Color { pub r: u16, pub g: u16, pub b: u16, }
Expand description

An RGB color with 16 bits per channel. You can use Color::scale_to_8bit to convert to an 8bit RGB color.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§r: u16

Red

§g: u16

Green

§b: u16

Blue

Implementations§

Source§

impl Color

Source

pub fn rgb(r: u16, g: u16, b: u16) -> Self

Creates a RGB color from its three channels: Red, Green and Blue.

Source

pub fn perceived_lightness(&self) -> f32

Perceptual lightness (L*) as a value between 0.0 (black) and 1.0 (white) where 0.5 is the perceptual middle gray.

Note that the color’s alpha is ignored.

let is_dark = color.perceived_lightness() <= 0.5;
Examples found in repository?
examples/theme.rs (line 16)
6fn main() -> Result<(), display::DisplayAsDebug<Error>> {
7    let colors = color_palette(QueryOptions::default())?;
8
9    let theme = match colors.theme_mode() {
10        ThemeMode::Dark => "dark",
11        ThemeMode::Light => "light",
12    };
13
14    println!(
15        "{theme}, fg: {}, bg: {}",
16        colors.foreground.perceived_lightness(),
17        colors.background.perceived_lightness()
18    );
19
20    Ok(())
21}
Source

pub fn scale_to_8bit(&self) -> (u8, u8, u8)

Converts the color to 8 bit precision per channel by scaling each channel.

let white = Color::rgb(u16::MAX, u16::MAX, u16::MAX);
assert_eq!((u8::MAX, u8::MAX, u8::MAX), white.scale_to_8bit());

let black = Color::rgb(0, 0, 0);
assert_eq!((0, 0, 0), black.scale_to_8bit());
Examples found in repository?
examples/bg.rs (line 7)
5fn main() -> Result<(), display::DisplayAsDebug<Error>> {
6    let bg = background_color(QueryOptions::default())?;
7    let bg_8bit = bg.scale_to_8bit();
8    println!("rgb16({}, {}, {})", bg.r, bg.g, bg.b);
9    println!("rgb8({}, {}, {})", bg_8bit.0, bg_8bit.1, bg_8bit.2);
10    Ok(())
11}
More examples
Hide additional examples
examples/fg.rs (line 7)
5fn main() -> Result<(), display::DisplayAsDebug<Error>> {
6    let fg = foreground_color(QueryOptions::default())?;
7    let fg_8bit = fg.scale_to_8bit();
8    println!("rgb16({}, {}, {})", fg.r, fg.g, fg.b);
9    println!("rgb8({}, {}, {})", fg_8bit.0, fg_8bit.1, fg_8bit.2);
10    Ok(())
11}

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Color

Source§

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

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

impl Default for Color

Source§

fn default() -> Color

Returns the “default value” for a type. Read more
Source§

impl From<Color> for RGB16

Available on crate feature rgb only.
Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for RGB8

Available on crate feature rgb only.
Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for RgbColor

Available on crate feature anstyle only.
Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Rgb<u16>> for Color

Available on crate feature rgb only.
Source§

fn from(value: RGB16) -> Self

Converts to this type from the input type.
Source§

impl Hash for Color

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 Color

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Color

Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

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<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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.