Struct chocodye::Rgb

source ·
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

source

pub const WHITE: Rgb = _

(255, 255, 255)

source

pub const BLACK: Rgb = _

(0, 0, 0)

source

pub const RED: Rgb = _

(255, 0, 0)

source

pub const GREEN: Rgb = _

(0, 255, 0)

source

pub const BLUE: Rgb = _

(0, 0, 255)

source

pub const YELLOW: Rgb = _

(255, 255, 0)

source

pub const CYAN: Rgb = _

(0, 255, 255)

source

pub const MAGENTA: Rgb = _

(255, 0, 255)

source

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 });
source

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));
source

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());
source

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.
source

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)));
source

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.
source

pub fn grayscale(self) -> Rgb

Converts this color into a gray shade. Takes human perception into account.

§Examples
use chocodye::Rgb;

assert_eq!(Rgb::RED.grayscale(), Rgb::gray(76));
assert_eq!(Rgb::WHITE.grayscale(), Rgb::WHITE);
assert_eq!(Rgb::BLACK.grayscale(), Rgb::BLACK);

Trait Implementations§

source§

impl Clone for Rgb

source§

fn clone(&self) -> Rgb

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 Rgb

source§

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

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

impl Default for Rgb

source§

fn default() -> Rgb

The default color is arbitrarily set to #5bcefa, a light blue.

source§

impl From<Dye> for Rgb

source§

fn from(dye: Dye) -> Rgb

Converts a dye into its color.

source§

impl From<Rgb> for u32

source§

fn from(value: Rgb) -> u32

Converts this color to an u32 in RRGGBBAA format. The alpha bits are set to 0xFF.

§Examples
use chocodye::Rgb;

assert_eq!(u32::from(Rgb::new(1, 2, 3)), 0x010203FF);
assert_ne!(u32::from(Rgb::from(0x0ABCDEF0)), 0x0ABCDEF0); // The alpha bits are lost.
source§

impl From<u32> for Rgb

source§

fn from(value: u32) -> Rgb

Converts an u32 in RRGGBBAA format to its corresponding color. The alpha bits are ignored.

§Examples
use chocodye::Rgb;

assert_eq!(Rgb::from(0x01020300), Rgb::new(1, 2, 3));
source§

impl Hash for Rgb

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 LowerHex for Rgb

source§

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

Formats self as a hex color.

§Examples
use chocodye::Rgb;

assert_eq!(format!("{:x}", Rgb::RED), "#ff0000");
source§

impl PartialEq for Rgb

source§

fn eq(&self, other: &Rgb) -> 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 TryFrom<Rgb> for Dye

§

type Error = Dye

The closest match if there is no exact match.

source§

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);
source§

impl UpperHex for Rgb

source§

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

Formats self as a hex color.

§Examples
use chocodye::Rgb;

assert_eq!(format!("{:X}", Rgb::RED), "#FF0000");
source§

impl Copy for Rgb

source§

impl Eq for Rgb

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AnyEq for T
where T: Any + PartialEq,

source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

source§

fn as_any(&self) -> &(dyn Any + 'static)

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.