Color

Struct Color 

Source
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}
Expand description

The color

Fields§

§r: f32

Red

§g: f32

Green

§b: f32

Blue

§a: f32

Alpha

Implementations§

Source§

impl Color

Source

pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self

Arguments:

  • r: Red value [0..1]
  • g: Green value [0..1]
  • b: Blue value [0..1]
  • a: Alpha value [0..1]
Source

pub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self

Arguments:

  • r: Red value [0..255]
  • g: Green value [0..255]
  • b: Blue value [0..255]
  • a: Alpha value [0..255]
Source

pub fn from_linear_rgba(r: f32, g: f32, b: f32, a: f32) -> Self

Arguments:

  • r: Red value [0..1]
  • g: Green value [0..1]
  • b: Blue value [0..1]
  • a: Alpha value [0..1]
Source

pub fn from_linear_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self

Arguments:

  • r: Red value [0..255]
  • g: Green value [0..255]
  • b: Blue value [0..255]
  • a: Alpha value [0..255]
Source

pub const fn from_hsva(h: f32, s: f32, v: f32, a: f32) -> Self

Arguments:

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • v: Value [0..1]
  • a: Alpha [0..1]
Source

pub const fn from_hsla(h: f32, s: f32, l: f32, a: f32) -> Self

Arguments:

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • l: Lightness [0..1]
  • a: Alpha [0..1]
Source

pub const fn from_hwba(h: f32, w: f32, b: f32, a: f32) -> Self

Arguments:

  • h: Hue angle [0..360]
  • w: Whiteness [0..1]
  • b: Blackness [0..1]
  • a: Alpha [0..1]
Source

pub fn from_oklaba(l: f32, a: f32, b: f32, alpha: f32) -> Self

Arguments:

  • l: Perceived lightness
  • a: How green/red the color is
  • b: How blue/yellow the color is
  • alpha: Alpha [0..1]
Source

pub fn from_oklcha(l: f32, c: f32, h: f32, alpha: f32) -> Self

Arguments:

  • l: Perceived lightness
  • c: Chroma
  • h: Hue angle in radians
  • alpha: Alpha [0..1]
Source

pub fn from_laba(l: f32, a: f32, b: f32, alpha: f32) -> Self

Arguments:

  • l: Lightness
  • a: Distance along the a axis
  • b: Distance along the b axis
  • alpha: Alpha [0..1]
Source

pub fn from_lcha(l: f32, c: f32, h: f32, alpha: f32) -> Self

Arguments:

  • l: Lightness
  • c: Chroma
  • h: Hue angle in radians
  • alpha: Alpha [0..1]
Source

pub fn from_html<S: AsRef<str>>(s: S) -> Result<Self, ParseColorError>

Create color from CSS color string.

§Examples
use csscolorparser::Color;

let c = Color::from_html("rgb(255,0,0)")?;

assert_eq!(c.to_array(), [1.0, 0.0, 0.0, 1.0]);
assert_eq!(c.to_rgba8(), [255, 0, 0, 255]);
assert_eq!(c.to_css_hex(), "#ff0000");
assert_eq!(c.to_css_rgb(), "rgb(255 0 0)");
Source

pub const fn clamp(&self) -> Self

Restricts R, G, B, A values to the range [0..1].

Source

pub fn name(&self) -> Option<&'static str>

Returns name if there is a name for this color.

Note: It ignores transparency (alpha value).

use csscolorparser::Color;

assert_eq!(Color::from_rgba8(255, 0, 0, 255).name(), Some("red"));
assert_eq!(Color::from_rgba8(238, 130, 238, 255).name(), Some("violet"));
assert_eq!(Color::from_rgba8(90, 150, 200, 255).name(), None);
Source

pub const fn to_array(&self) -> [f32; 4]

Returns: [r, g, b, a]

  • Red, green, blue and alpha in the range [0..1]
Source

pub const fn to_rgba8(&self) -> [u8; 4]

Returns: [r, g, b, a]

  • Red, green, blue and alpha in the range [0..255]
Source

pub const fn to_rgba16(&self) -> [u16; 4]

Returns: [r, g, b, a]

  • Red, green, blue and alpha in the range [0..65535]
Source

pub const fn to_hsva(&self) -> [f32; 4]

Returns: [h, s, v, a]

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • v: Value [0..1]
  • a: Alpha [0..1]
Source

pub const fn to_hsla(&self) -> [f32; 4]

Returns: [h, s, l, a]

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • l: Lightness [0..1]
  • a: Alpha [0..1]
Source

pub const fn to_hwba(&self) -> [f32; 4]

Returns: [h, w, b, a]

  • h: Hue angle [0..360]
  • w: Whiteness [0..1]
  • b: Blackness [0..1]
  • a: Alpha [0..1]
Source

pub fn to_linear_rgba(&self) -> [f32; 4]

Returns: [r, g, b, a]

  • Red, green, blue and alpha in the range [0..1]
Source

pub fn to_linear_rgba_u8(&self) -> [u8; 4]

Returns: [r, g, b, a]

  • Red, green, blue and alpha in the range [0..255]
Source

pub fn to_oklaba(&self) -> [f32; 4]

Returns: [l, a, b, alpha]

Source

pub fn to_oklcha(&self) -> [f32; 4]

Returns: [l, c, h, alpha]

Source

pub fn to_laba(&self) -> [f32; 4]

Returns: [l, a, b, alpha]

Source

pub fn to_lcha(&self) -> [f32; 4]

Returns: [l, c, h, alpha]

Source

pub fn to_css_hex(&self) -> String

Get CSS RGB hexadecimal color representation

Source

pub fn to_css_rgb(&self) -> String

Get CSS rgb() color representation

Source

pub fn to_css_hsl(&self) -> String

Get CSS hsl() color representation

Source

pub fn to_css_hwb(&self) -> String

Get CSS hwb() color representation

Source

pub fn to_css_oklab(&self) -> String

Get CSS oklab() color representation

Source

pub fn to_css_oklch(&self) -> String

Get CSS oklch() color representation

Source

pub fn to_css_lab(&self) -> String

Get CSS lab() color representation

Source

pub fn to_css_lch(&self) -> String

Get CSS lch() color representation

Source

pub const fn interpolate_rgb(&self, other: &Color, t: f32) -> Self

Blend this color with the other one, in the RGB color-space. t in the range [0..1].

Source

pub fn interpolate_linear_rgb(&self, other: &Color, t: f32) -> Self

Blend this color with the other one, in the linear RGB color-space. t in the range [0..1].

Source

pub const fn interpolate_hsv(&self, other: &Color, t: f32) -> Self

Blend this color with the other one, in the HSV color-space. t in the range [0..1].

Source

pub fn interpolate_oklab(&self, other: &Color, t: f32) -> Self

Blend this color with the other one, in the Oklab color-space. t in the range [0..1].

Source

pub fn interpolate_lab(&self, other: &Color, t: f32) -> Self

Blend this color with the other one, in the Lab color-space. t in the range [0..1].

Source

pub fn interpolate_lch(&self, other: &Color, t: f32) -> Self

Blend this color with the other one, in the LCH color-space. t in the range [0..1].

Source§

impl Color

Source

pub fn from_rgb(r: f32, g: f32, b: f32) -> Self

👎Deprecated: Use new instead.

Arguments:

  • r: Red value [0..1]
  • g: Green value [0..1]
  • b: Blue value [0..1]
Source

pub fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Self

👎Deprecated: Use new instead.

Arguments:

  • r: Red value [0..1]
  • g: Green value [0..1]
  • b: Blue value [0..1]
  • a: Alpha value [0..1]
Source

pub fn from_rgb_u8(r: u8, g: u8, b: u8) -> Self

👎Deprecated: Use from_rgba8 instead.

Arguments:

  • r: Red value [0..255]
  • g: Green value [0..255]
  • b: Blue value [0..255]
Source

pub fn from_rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Self

👎Deprecated: Use from_rgba8 instead.

Arguments:

  • r: Red value [0..255]
  • g: Green value [0..255]
  • b: Blue value [0..255]
  • a: Alpha value [0..255]
Source

pub fn from_linear_rgb(r: f32, g: f32, b: f32) -> Self

👎Deprecated: Use from_linear_rgba instead.

Arguments:

  • r: Red value [0..1]
  • g: Green value [0..1]
  • b: Blue value [0..1]
Source

pub fn from_linear_rgb_u8(r: u8, g: u8, b: u8) -> Self

👎Deprecated: Use from_linear_rgba8 instead.

Arguments:

  • r: Red value [0..255]
  • g: Green value [0..255]
  • b: Blue value [0..255]
Source

pub fn from_linear_rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Self

👎Deprecated: Use from_linear_rgba8 instead.

Arguments:

  • r: Red value [0..255]
  • g: Green value [0..255]
  • b: Blue value [0..255]
  • a: Alpha value [0..255]
Source

pub fn from_hsv(h: f32, s: f32, v: f32) -> Self

👎Deprecated: Use from_hsva instead.

Arguments:

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • v: Value [0..1]
Source

pub fn from_hsl(h: f32, s: f32, l: f32) -> Self

👎Deprecated: Use from_hsla instead.

Arguments:

  • h: Hue angle [0..360]
  • s: Saturation [0..1]
  • l: Lightness [0..1]
Source

pub fn from_hwb(h: f32, w: f32, b: f32) -> Self

👎Deprecated: Use from_hwba instead.

Arguments:

  • h: Hue angle [0..360]
  • w: Whiteness [0..1]
  • b: Blackness [0..1]
Source

pub fn from_oklab(l: f32, a: f32, b: f32) -> Self

👎Deprecated: Use from_oklaba instead.

Arguments:

  • l: Perceived lightness
  • a: How green/red the color is
  • b: How blue/yellow the color is
Source

pub fn from_lab(l: f32, a: f32, b: f32, alpha: f32) -> Self

👎Deprecated: Use from_laba instead.

Arguments:

  • l: Lightness
  • a: Distance along the a axis
  • b: Distance along the b axis
  • alpha: Alpha [0..1]
Source

pub fn to_lab(&self) -> [f32; 4]

👎Deprecated: Use to_laba instead.

Returns: [l, a, b, alpha]

Source

pub fn from_lch(l: f32, c: f32, h: f32, alpha: f32) -> Self

👎Deprecated: Use from_lcha instead.

Arguments:

  • l: Lightness
  • c: Chroma
  • h: Hue angle in radians
  • alpha: Alpha [0..1]
Source

pub fn to_lch(&self) -> [f32; 4]

👎Deprecated: Use to_lcha instead.

Returns: [l, c, h, alpha]

Source

pub fn rgba(&self) -> (f32, f32, f32, f32)

👎Deprecated

Returns: (r, g, b, a)

  • Red, green, blue and alpha in the range [0..1]
Source

pub fn rgba_u8(&self) -> (u8, u8, u8, u8)

👎Deprecated: Use to_rgba8 instead.

Returns: (r, g, b, a)

  • Red, green, blue and alpha in the range [0..255]
Source

pub fn to_hex_string(&self) -> String

👎Deprecated: Use to_css_hex instead.

Get the RGB hexadecimal color string.

Source

pub fn to_rgb_string(&self) -> String

👎Deprecated: Use to_css_rgb instead.

Get the CSS rgb() format string.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

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 ColorInterop for Color

Source§

type CintTy = Alpha<EncodedSrgb<f32>>

Source§

fn from_cint(col: Self::CintTy) -> Self

Convert self into its canonical cint type.
Source§

fn into_cint(self) -> Self::CintTy

Create a Self from its canonical cint type.
Source§

impl Debug for Color

Source§

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

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

impl Default for Color

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Color

Available on crate feature serde only.

Implement Serde deserialization from string

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. 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<[f32; 3]> for Color

Source§

fn from([r, g, b]: [f32; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<[f32; 4]> for Color

Source§

fn from([r, g, b, a]: [f32; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<[f64; 3]> for Color

Source§

fn from([r, g, b]: [f64; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<[f64; 4]> for Color

Source§

fn from([r, g, b, a]: [f64; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<[u8; 3]> for Color

Source§

fn from([r, g, b]: [u8; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<[u8; 4]> for Color

Source§

fn from([r, g, b, a]: [u8; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<(f32, f32, f32)> for Color

Source§

fn from((r, g, b): (f32, f32, f32)) -> Self

Converts to this type from the input type.
Source§

impl From<(f32, f32, f32, f32)> for Color

Source§

fn from((r, g, b, a): (f32, f32, f32, f32)) -> Self

Converts to this type from the input type.
Source§

impl From<(u8, u8, u8)> for Color

Source§

fn from((r, g, b): (u8, u8, u8)) -> Self

Converts to this type from the input type.
Source§

impl From<(u8, u8, u8, u8)> for Color

Source§

fn from((r, g, b, a): (u8, u8, u8, u8)) -> Self

Converts to this type from the input type.
Source§

impl From<Alpha<EncodedSrgb<f32>>> for Color

Source§

fn from(c: Alpha<EncodedSrgb<f32>>) -> Self

Converts to this type from the input type.
Source§

impl From<Alpha<EncodedSrgb<f64>>> for Color

Source§

fn from(c: Alpha<EncodedSrgb<f64>>) -> Self

Converts to this type from the input type.
Source§

impl From<Alpha<EncodedSrgb>> for Color

Source§

fn from(c: Alpha<EncodedSrgb<u8>>) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Alpha<EncodedSrgb<f32>>

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Alpha<EncodedSrgb<f64>>

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Alpha<EncodedSrgb<u8>>

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for EncodedSrgb<u8>

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for EncodedSrgb<f32>

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for EncodedSrgb<f64>

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl From<EncodedSrgb<f32>> for Color

Source§

fn from(c: EncodedSrgb<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<EncodedSrgb<f64>> for Color

Source§

fn from(c: EncodedSrgb<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<EncodedSrgb> for Color

Source§

fn from(c: EncodedSrgb<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<Rgb<f32>> for Color

Available on crate feature rust-rgb only.

Convert rust-rgb’s RGB<f32> type into Color.

Source§

fn from(item: RGB<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<Rgba<f32>> for Color

Available on crate feature rust-rgb only.

Convert rust-rgb’s RGBA<f32> type into Color.

Source§

fn from(item: RGBA<f32>) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Color

Source§

type Err = ParseColorError

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 PartialEq for Color

Source§

fn eq(&self, other: &Color) -> 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 PartialOrd for Color

Source§

fn partial_cmp(&self, other: &Color) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Color

Available on crate feature serde only.

Implement Serde serialization into HEX string

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<&str> for Color

Source§

type Error = ParseColorError

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

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for Color

Source§

type Error = ParseColorError

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

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,