Skip to main content

Color

Enum Color 

Source
pub enum Color {
Show 17 variants A98Rgb(A98Rgb), DisplayP3(DisplayP3), Hsv(Hsv), Hsl(Hsl), Hex(Hex), Hwb(Hwb), Lab(Lab), Lch(Lch), LinearRgb(LinearRgb), Named(Named), Oklab(Oklab), Oklch(Oklch), ProphotoRgb(ProphotoRgb), Rec2020(Rec2020), Srgb(Srgb), XyzD50(XyzD50), XyzD65(XyzD65),
}

Variants§

§

A98Rgb(A98Rgb)

§

DisplayP3(DisplayP3)

§

Hsv(Hsv)

§

Hsl(Hsl)

§

Hex(Hex)

§

Hwb(Hwb)

§

Lab(Lab)

§

Lch(Lch)

§

LinearRgb(LinearRgb)

§

Named(Named)

§

Oklab(Oklab)

§

Oklch(Oklch)

§

ProphotoRgb(ProphotoRgb)

§

Rec2020(Rec2020)

§

Srgb(Srgb)

§

XyzD50(XyzD50)

§

XyzD65(XyzD65)

Implementations§

Source§

impl Color

Source

pub fn color_space(&self) -> Option<ColorSpace>

Returns the ColorSpace of this colour, if it maps to a bounded RGB gamut.

Perceptual and CIE spaces (Lab, Lch, Oklab, Oklch, XyzD50, XyzD65) return None — they can represent colours outside any single RGB gamut.

Source

pub fn in_gamut_of(&self, space: ColorSpace) -> bool

Returns true if this colour can be represented in space without clamping.

If the colour’s own space is a subset of space and the colour is in gamut of its own space, this returns true without conversion. Otherwise the colour is converted to the target space (via XyzD65) and the RGB channels are checked against [0,1].

Source§

impl Color

Source

pub fn with_alpha(self, alpha: f32) -> Self

Returns a copy of this colour with a new alpha value (0.0–100.0).

For Named colours (which are always fully opaque), setting a non-100 alpha converts to Srgb. For Hex, the colour is round-tripped through Srgb to apply the new alpha.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Color

Source§

impl Debug for Color

Source§

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

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

impl Display for Color

Source§

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

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

impl From<A98Rgb> for Color

Source§

fn from(c: A98Rgb) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for A98Rgb

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for DisplayP3

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Hsv

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Hex

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Hsl

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Hwb

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Lab

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Lch

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for LinearRgb

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Oklab

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Oklch

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for ProphotoRgb

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Rec2020

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Srgb

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for XyzD50

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for XyzD65

Source§

fn from(value: Color) -> Self

Converts to this type from the input type.
Source§

impl From<DisplayP3> for Color

Source§

fn from(c: DisplayP3) -> Self

Converts to this type from the input type.
Source§

impl From<Hsl> for Color

Source§

fn from(c: Hsl) -> Self

Converts to this type from the input type.
Source§

impl From<Hwb> for Color

Source§

fn from(c: Hwb) -> Self

Converts to this type from the input type.
Source§

impl From<Lab> for Color

Source§

fn from(c: Lab) -> Self

Converts to this type from the input type.
Source§

impl From<Lch> for Color

Source§

fn from(c: Lch) -> Self

Converts to this type from the input type.
Source§

impl From<LinearRgb> for Color

Source§

fn from(c: LinearRgb) -> Self

Converts to this type from the input type.
Source§

impl From<Oklab> for Color

Source§

fn from(c: Oklab) -> Self

Converts to this type from the input type.
Source§

impl From<Oklch> for Color

Source§

fn from(c: Oklch) -> Self

Converts to this type from the input type.
Source§

impl From<ProphotoRgb> for Color

Source§

fn from(c: ProphotoRgb) -> Self

Converts to this type from the input type.
Source§

impl From<Rec2020> for Color

Source§

fn from(c: Rec2020) -> Self

Converts to this type from the input type.
Source§

impl From<Srgb> for Color

Source§

fn from(c: Srgb) -> Self

Converts to this type from the input type.
Source§

impl From<XyzD50> for Color

Source§

fn from(c: XyzD50) -> Self

Converts to this type from the input type.
Source§

impl From<XyzD65> for Color

Source§

fn from(c: XyzD65) -> Self

Converts to this type from the input type.
Source§

impl Gamut for Color

Source§

fn in_gamut(&self) -> bool

Returns true if all colour channels are within the natural bounds of this colour space. Alpha is not considered — it is always clamped on construction.
Source§

fn clamp_to_gamut(&self) -> Self

Returns a copy with all colour channels naively clamped to the natural bounds. Read more
Source§

fn map_to_gamut(self) -> Self

Perceptually maps this colour into gamut. Read more
Source§

impl PartialEq for Color

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PerceptualRound for Color

Source§

fn round(self) -> Self

Source§

impl StructuralPartialEq for Color

Source§

impl ToAlpha for Color

Source§

fn to_alpha(&self) -> f32

Returns a number between 0.0 (fully transparent) to 100.0 (fully opaque).
Source§

fn fully_opaque(&self) -> bool

Returns true if the alpha of this colour is 100.0
Source§

fn fully_transparent(&self) -> bool

Returns true if the alpha of this colour is 0.0
Source§

impl TryFrom<Color> for Named

Source§

type Error = ToNamedError

The type returned in the event of a conversion error.
Source§

fn try_from(value: Color) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnsafeUnpin for Color

§

impl UnwindSafe for Color

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<C, T> ColorDistance<T> for C
where C: Copy, Lab: From<C> + From<T>,

Source§

fn close_to(&self, other: T, tolerance: f64) -> bool

Compares self to other via delta_e, checking that the result is less than or equal to tolerance. Read more
Source§

fn far_from(&self, other: T, tolerance: f64) -> bool

Compares self to other via delta_e, checking that the result is greater than or equal to tolerance. Read more
Source§

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

This uses the CIEDE2000 Delta E formula difference between self and other. 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<C, T> WcagColorContrast<T> for C
where C: Copy, T: Copy, Srgb: From<C> + From<T>,

Source§

fn wcag_contrast_ratio(&self, other: T) -> f64

Calculate the contrast ratio between self and other. colors according to WCAG 2.1. Returns a value between 1:1 and 21:1 Read more
Source§

fn wcag_level(&self, other: T) -> WcagLevel

Get the WCAG level for the contrast between self and other. Read more
Source§

fn find_minimum_contrast<Space>( &self, other: T, level: WcagLevel, ) -> Option<Space>
where Self: WcagColorContrast<Space>, Space: ColorMix<Self, T> + From<Self> + From<T> + Copy + PartialEq + Debug, Srgb: From<Space>, Hex: From<Space>,

Find the closest color that meets a specific WCAG level against a background. Read more