#[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
impl Color
Sourcepub fn rgb(r: u16, g: u16, b: u16) -> Self
pub fn rgb(r: u16, g: u16, b: u16) -> Self
Creates a RGB color from its three channels: Red, Green and Blue.
Sourcepub fn perceived_lightness(&self) -> f32
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}
Sourcepub fn scale_to_8bit(&self) -> (u8, u8, u8)
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?
More examples
Trait Implementations§
impl Eq for Color
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more