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§

§r: u16

Red

§g: u16

Green

§b: u16

Blue

Implementations§

source§

impl Color

source

pub fn perceived_lightness(&self) -> u8

The perceived lightness of the color as a value between 0 (black) and 100 (white) where 50 is the perceptual “middle grey”.

let is_dark = color.perceived_lightness() <= 50;
Examples found in repository?
examples/theme.rs (line 18)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() -> Result<(), Box<dyn Error>> {
    let colors = color_scheme(QueryOptions::default())?;

    let theme = if colors.is_light_on_dark() {
        "light on dark"
    } else {
        "dark on light"
    };

    println!(
        "{theme}, fg: {}, bg: {}",
        colors.foreground.perceived_lightness(),
        colors.background.perceived_lightness()
    );

    Ok(())
}
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 { r: u16::MAX, g: u16::MAX, b: u16::MAX };
assert_eq!((u8::MAX, u8::MAX, u8::MAX), white.scale_to_8bit());

let black = Color { r: 0, g: 0, b: 0 };
assert_eq!((0, 0, 0), black.scale_to_8bit());

Trait Implementations§

source§

impl Clone for Color

source§

fn clone(&self) -> Color

Returns a copy 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<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 PartialEq for Color

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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> 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,

§

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>,

§

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>,

§

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.