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) -> Color

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_rgba8(r: u8, g: u8, b: u8, a: u8) -> Color

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) -> Color

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) -> Color

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_hsva(h: f32, s: f32, v: f32, a: f32) -> Color

Arguments:

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

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

Arguments:

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

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

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) -> Color

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) -> Color

Arguments:

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

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

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 fn clamp(&self) -> Color

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 fn to_array(&self) -> [f32; 4]

Returns: [r, g, b, a]

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

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

Returns: [r, g, b, a]

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

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

Returns: [r, g, b, a]

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

pub 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 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 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_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 interpolate_rgb(&self, other: &Color, t: f32) -> Color

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) -> Color

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

Source

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

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) -> Color

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

Source§

impl Color

Source

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

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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) -> Color

👎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 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 Debug for Color

Source§

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

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

impl Default for Color

Source§

fn default() -> Color

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

impl Display for Color

Source§

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

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

impl From<[f32; 3]> for Color

Source§

fn from(_: [f32; 3]) -> Color

Converts to this type from the input type.
Source§

impl From<[f32; 4]> for Color

Source§

fn from(_: [f32; 4]) -> Color

Converts to this type from the input type.
Source§

impl From<[f64; 3]> for Color

Source§

fn from(_: [f64; 3]) -> Color

Converts to this type from the input type.
Source§

impl From<[f64; 4]> for Color

Source§

fn from(_: [f64; 4]) -> Color

Converts to this type from the input type.
Source§

impl From<[u8; 3]> for Color

Source§

fn from(_: [u8; 3]) -> Color

Converts to this type from the input type.
Source§

impl From<[u8; 4]> for Color

Source§

fn from(_: [u8; 4]) -> Color

Converts to this type from the input type.
Source§

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

Source§

fn from(_: (f32, f32, f32)) -> Color

Converts to this type from the input type.
Source§

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

Source§

fn from(_: (f32, f32, f32, f32)) -> Color

Converts to this type from the input type.
Source§

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

Source§

fn from(_: (u8, u8, u8)) -> Color

Converts to this type from the input type.
Source§

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

Source§

fn from(_: (u8, u8, u8, u8)) -> Color

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<Color, <Color as FromStr>::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 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<Color, <Color as TryFrom<&str>>::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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,