pub struct Rgb {
pub r: u8,
pub g: u8,
pub b: u8,
}
Expand description
A color represented by three u8
components.
Fields§
§r: u8
The red component.
g: u8
The green component.
b: u8
The blue component.
Implementations§
source§impl Rgb
impl Rgb
sourcepub const fn new(r: u8, g: u8, b: u8) -> Rgb
pub const fn new(r: u8, g: u8, b: u8) -> Rgb
Creates a new color.
§Examples
use chocodye::Rgb;
assert_eq!(Rgb::new(5, 7, 11), Rgb { r: 5, g: 7, b: 11 });
sourcepub const fn gray(rgb: u8) -> Rgb
pub const fn gray(rgb: u8) -> Rgb
Creates a new color with all three components set to the same value.
§Examples
use chocodye::Rgb;
assert_eq!(Rgb::gray(127), Rgb::new(127, 127, 127));
sourcepub fn from_hex(s: &str) -> Result<Rgb, ParseHexError>
pub fn from_hex(s: &str) -> Result<Rgb, ParseHexError>
Parses a hex color.
§Examples
use chocodye::Rgb;
assert_eq!(Rgb::from_hex("#ffffff"), Ok(Rgb::new(255, 255, 255)));
assert!(Rgb::from_hex("#fff").is_err());
assert!(Rgb::from_hex("ffffff").is_err());
sourcepub const fn checked_add_signed(self, r: i8, g: i8, b: i8) -> Option<Rgb>
pub const fn checked_add_signed(self, r: i8, g: i8, b: i8) -> Option<Rgb>
Checked addition with three signed components.
Computes self + rgb
, returning None
if overflow occured.
§Examples
use chocodye::Rgb;
assert_eq!(Rgb::new(20, 30, 40).checked_add_signed(2, -2, 0), Some(Rgb::new(22, 28, 40)));
assert_eq!(Rgb::new(10, 2, 250).checked_add_signed(4, -5, 1), None); // `g` would underflow.
sourcepub const fn distance(self, other: Rgb) -> u32
pub const fn distance(self, other: Rgb) -> u32
Computes the squared Euclidian distance
between self
and other
. Does not take human perception into consideration. Useful for intermediate algorithms.
§Examples
use chocodye::Rgb;
assert_eq!(Rgb::WHITE.distance(Rgb::WHITE), 0);
assert_eq!(Rgb::gray(8).distance(Rgb::gray(16)), Rgb::gray(24).distance(Rgb::gray(16)));
sourcepub fn luma(self) -> u8
pub fn luma(self) -> u8
Computes the luma, the brightness of self
.
Takes human perception into account. Useful for sorting colors.
§Examples
use chocodye::Rgb;
assert_eq!(Rgb::WHITE.luma(), 255);
assert_eq!(Rgb::BLACK.luma(), 0);
assert_eq!(Rgb::new(10, 20, 30).luma(), 18);
assert_eq!(Rgb::new(20, 40, 60).luma(), 36);
assert!(Rgb::GREEN.luma() > Rgb::RED.luma()); // Humans are more sensitive to green.
Trait Implementations§
source§impl TryFrom<Rgb> for Dye
impl TryFrom<Rgb> for Dye
source§fn try_from(value: Rgb) -> Result<Dye, Self::Error>
fn try_from(value: Rgb) -> Result<Dye, Self::Error>
Converts a color to a dye, returning Ok(_)
for an exact match, or Err(_)
for the closest match.
§Examples
use chocodye::{Dye, Rgb};
assert_eq!(Dye::AppleGreen.color(), Rgb::new(155, 179, 99));
assert_eq!(Dye::try_from(Rgb::new(155, 179, 99)), Ok(Dye::AppleGreen));
assert_eq!(Dye::try_from(Rgb::new(155, 179, 98)), Err(Dye::AppleGreen));
// use `std::convert::identity` if you don't care about exact matches
assert_eq!(Dye::try_from(Rgb::WHITE).unwrap_or_else(identity), Dye::LotusPink);
assert_eq!(Dye::try_from(Rgb::BLACK).unwrap_or_else(identity), Dye::InkBlue);
impl Copy for Rgb
impl Eq for Rgb
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 UnwindSafe for Rgb
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