Color

Struct Color 

Source
pub struct Color<T, W = D65>
where T: FloatNumber,
{ /* private fields */ }
Expand description

The color representation.

ยงType Parameters

  • T - The floating point type.
  • W - The white point type.

ยงExamples

use std::str::FromStr;

use auto_palette::color::Color;

let color: Color<f32> = Color::from_str("#2c7de7").unwrap();
assert!(color.is_light());
assert!(color.lightness() - 52.917 < 1e-3);
assert!(color.chroma() - 61.981 < 1e-3);
assert!(color.hue().to_degrees() - 282.662 < 1e-3);

let rgb = color.to_rgb();
assert_eq!(format!("{}", rgb), "RGB(44, 125, 231)");

let hsl = color.to_hsl();
assert_eq!(format!("{}", hsl), "HSL(214.01, 0.80, 0.54)");

let lab = color.to_lab();
assert_eq!(format!("{}", lab), "Lab(52.92, 13.59, -60.47)");

Implementationsยง

Sourceยง

impl<T, W> Color<T, W>
where T: FloatNumber, W: WhitePoint,

Source

pub fn is_light(&self) -> bool

Returns whether this color is light.

ยงReturns

true if the color is light, otherwise false.

Source

pub fn is_dark(&self) -> bool

Returns whether this color is dark.

ยงReturns

true if the color is dark, otherwise false.

Source

pub fn lightness(&self) -> T

Returns the lightness of this color.

ยงReturns

The lightness of this color.

Source

pub fn chroma(&self) -> T

Returns the chroma of this color.

ยงReturns

The chroma of this color.

Source

pub fn delta_e(&self, other: &Self) -> T

Calculates the delta E76 value between this color and another color.

ยงArguments
  • other - The other color to compare against.
ยงReturns

The delta E76 value, which is a measure of the difference between two colors.

ยงNote

This method uses the CIE76 formula, which is a simple Euclidean distance in the Lab* color space. Color difference CIE76 - Wikipedia

Source

pub fn hue(&self) -> Hue<T>

Returns the hue of this color. The hue is the angle of the vector in the ab plane.

ยงReturns

The hue of this color.

Source

pub fn to_hex_string(&self) -> String

Converts this color to a hexadecimal string.

ยงReturns

The hexadecimal string representation of this color.

Source

pub fn to_rgb(&self) -> RGB

Converts this color to the RGB color space.

ยงReturns

The converted RGB color.

Source

pub fn to_cmyk(&self) -> CMYK<T>

Converts this color to the CMYK color space.

ยงReturns

The converted CMYK color.

Source

pub fn to_hsl(&self) -> HSL<T>

Converts this color to the HSL color space.

ยงReturns

The converted HSL color.

Source

pub fn to_hsv(&self) -> HSV<T>

Converts this color to the HSV color space.

ยงReturns

The converted HSV color.

Source

pub fn to_xyz(&self) -> XYZ<T>

Converts this color to the CIE XYZ color space.

ยงReturns

The converted XYZ color.

Source

pub fn to_luv(&self) -> Luv<T, W>

Converts this color to the CIE Luv* color space.

ยงReturns

The converted Luv color.

Source

pub fn to_lchuv(&self) -> LCHuv<T, W>

Converts this color to the CIE LCH(uv) color space.

ยงReturns

The converted LCHuv color.

Source

pub fn to_lab(&self) -> Lab<T, W>

Converts this color to the CIE Lab* color space.

ยงReturns

The converted Lab color.

Source

pub fn to_lchab(&self) -> LCHab<T, W>

Converts this color to the CIE LCH(ab) color space.

ยงReturns

The converted LCHab color.

Source

pub fn to_oklab(&self) -> Oklab<T>

Converts this color to the CIE Oklab color space.

ยงReturns

The converted Oklab color.

Source

pub fn to_oklch(&self) -> Oklch<T>

Converts this color to the CIE Oklch color space.

ยงReturns

The converted Oklch color.

Source

pub fn to_ansi16(&self) -> Ansi16

Converts this color to the 4-bit ANSI 16 color space.

ยงReturns

The converted Ansi16 color.

Source

pub fn to_ansi256(&self) -> Ansi256

Converts this color to the 8-bit ANSI 256 color space.

ยงReturns

The converted Ansi256 color.

Source

pub fn to_rgb_int(&self) -> u32

Converts this color to a 32-bit integer representation in RGB.

ยงReturns

The converted RGB integer representation.

Source

pub fn to_rgba_int(&self, alpha: u8) -> u32

Converts this color to a 32-bit integer representation in RGBA.

ยงArguments
  • alpha - The alpha value (0-255).
ยงReturns

The converted RGBA integer representation.

Source

pub fn mix(&self, other: &Self, fraction: T) -> Self

Mixes this color with another color by a given fraction.

ยงArguments
  • other - The other color to mix with.
  • fraction - The fraction to mix the two colors (0.0 to 1.0).
ยงReturns

A new Color instance that is the result of mixing the two colors.

Trait Implementationsยง

Sourceยง

impl<T, W: Clone> Clone for Color<T, W>
where T: FloatNumber + Clone,

Sourceยง

fn clone(&self) -> Color<T, W>

Returns a duplicate 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<T, W: Debug> Debug for Color<T, W>
where T: FloatNumber + Debug,

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl<T> Default for Color<T>
where T: FloatNumber,

Sourceยง

fn default() -> Self

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl<T> Display for Color<T>
where T: FloatNumber,

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl<T> From<u32> for Color<T>
where T: FloatNumber,

Sourceยง

fn from(value: u32) -> Self

Converts to this type from the input type.
Sourceยง

impl<T> FromStr for Color<T>
where T: FloatNumber,

Sourceยง

type Err = ColorError

The associated error which can be returned from parsing.
Sourceยง

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Sourceยง

impl<T, W: PartialEq> PartialEq for Color<T, W>

Sourceยง

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl<T, W: Copy> Copy for Color<T, W>
where T: FloatNumber + Copy,

Sourceยง

impl<T, W: Eq> Eq for Color<T, W>
where T: FloatNumber + Eq,

Sourceยง

impl<T, W> StructuralPartialEq for Color<T, W>
where T: FloatNumber,

Auto Trait Implementationsยง

ยง

impl<T, W> Freeze for Color<T, W>
where T: Freeze,

ยง

impl<T, W> RefUnwindSafe for Color<T, W>

ยง

impl<T, W> Send for Color<T, W>
where T: Send, W: Send,

ยง

impl<T, W> Sync for Color<T, W>
where T: Sync, W: Sync,

ยง

impl<T, W> Unpin for Color<T, W>
where T: Unpin, W: Unpin,

ยง

impl<T, W> UnwindSafe for Color<T, W>
where T: UnwindSafe, W: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Sourceยง

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> ToString for T
where T: Display + ?Sized,

Sourceยง

fn to_string(&self) -> String

Converts the given value to a String. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

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

Sourceยง

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.