Struct terminal_colorsaurus::Color
source · 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
impl Color
sourcepub fn perceived_lightness(&self) -> u8
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(())
}
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 { 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 PartialEq for Color
impl PartialEq for Color
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