pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}Expand description
The color
Fields§
§r: f32Red
g: f32Green
b: f32Blue
a: f32Alpha
Implementations§
Source§impl Color
impl Color
Sourcepub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self
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]
Sourcepub const fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self
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]
Sourcepub fn from_linear_rgba(r: f32, g: f32, b: f32, a: f32) -> Self
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]
Sourcepub fn from_linear_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self
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]
Sourcepub const fn from_hsva(h: f32, s: f32, v: f32, a: f32) -> Self
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]
Sourcepub const fn from_hsla(h: f32, s: f32, l: f32, a: f32) -> Self
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]
Sourcepub const fn from_hwba(h: f32, w: f32, b: f32, a: f32) -> Self
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]
Sourcepub fn from_oklaba(l: f32, a: f32, b: f32, alpha: f32) -> Self
pub fn from_oklaba(l: f32, a: f32, b: f32, alpha: f32) -> Self
Arguments:
l: Perceived lightnessa: How green/red the color isb: How blue/yellow the color isalpha: Alpha [0..1]
Sourcepub fn from_oklcha(l: f32, c: f32, h: f32, alpha: f32) -> Self
pub fn from_oklcha(l: f32, c: f32, h: f32, alpha: f32) -> Self
Arguments:
l: Perceived lightnessc: Chromah: Hue angle in radiansalpha: Alpha [0..1]
Sourcepub fn from_laba(l: f32, a: f32, b: f32, alpha: f32) -> Self
pub fn from_laba(l: f32, a: f32, b: f32, alpha: f32) -> Self
Arguments:
l: Lightnessa: Distance along theaaxisb: Distance along thebaxisalpha: Alpha [0..1]
Sourcepub fn from_lcha(l: f32, c: f32, h: f32, alpha: f32) -> Self
pub fn from_lcha(l: f32, c: f32, h: f32, alpha: f32) -> Self
Arguments:
l: Lightnessc: Chromah: Hue angle in radiansalpha: Alpha [0..1]
Sourcepub fn from_html<S: AsRef<str>>(s: S) -> Result<Self, ParseColorError>
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)");Sourcepub fn name(&self) -> Option<&'static str>
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);Sourcepub const fn to_array(&self) -> [f32; 4]
pub const fn to_array(&self) -> [f32; 4]
Returns: [r, g, b, a]
- Red, green, blue and alpha in the range [0..1]
Sourcepub const fn to_rgba8(&self) -> [u8; 4]
pub const fn to_rgba8(&self) -> [u8; 4]
Returns: [r, g, b, a]
- Red, green, blue and alpha in the range [0..255]
Sourcepub const fn to_rgba16(&self) -> [u16; 4]
pub const fn to_rgba16(&self) -> [u16; 4]
Returns: [r, g, b, a]
- Red, green, blue and alpha in the range [0..65535]
Sourcepub const fn to_hsva(&self) -> [f32; 4]
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]
Sourcepub const fn to_hsla(&self) -> [f32; 4]
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]
Sourcepub const fn to_hwba(&self) -> [f32; 4]
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]
Sourcepub fn to_linear_rgba(&self) -> [f32; 4]
pub fn to_linear_rgba(&self) -> [f32; 4]
Returns: [r, g, b, a]
- Red, green, blue and alpha in the range [0..1]
Sourcepub fn to_linear_rgba_u8(&self) -> [u8; 4]
pub fn to_linear_rgba_u8(&self) -> [u8; 4]
Returns: [r, g, b, a]
- Red, green, blue and alpha in the range [0..255]
Sourcepub fn to_css_hex(&self) -> String
pub fn to_css_hex(&self) -> String
Get CSS RGB hexadecimal color representation
Sourcepub fn to_css_rgb(&self) -> String
pub fn to_css_rgb(&self) -> String
Get CSS rgb() color representation
Sourcepub fn to_css_hsl(&self) -> String
pub fn to_css_hsl(&self) -> String
Get CSS hsl() color representation
Sourcepub fn to_css_hwb(&self) -> String
pub fn to_css_hwb(&self) -> String
Get CSS hwb() color representation
Sourcepub fn to_css_oklab(&self) -> String
pub fn to_css_oklab(&self) -> String
Get CSS oklab() color representation
Sourcepub fn to_css_oklch(&self) -> String
pub fn to_css_oklch(&self) -> String
Get CSS oklch() color representation
Sourcepub fn to_css_lab(&self) -> String
pub fn to_css_lab(&self) -> String
Get CSS lab() color representation
Sourcepub fn to_css_lch(&self) -> String
pub fn to_css_lch(&self) -> String
Get CSS lch() color representation
Sourcepub const fn interpolate_rgb(&self, other: &Color, t: f32) -> Self
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].
Sourcepub fn interpolate_linear_rgb(&self, other: &Color, t: f32) -> Self
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].
Sourcepub const fn interpolate_hsv(&self, other: &Color, t: f32) -> Self
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].
Sourcepub fn interpolate_oklab(&self, other: &Color, t: f32) -> Self
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].
Sourcepub fn interpolate_lab(&self, other: &Color, t: f32) -> Self
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].
Sourcepub fn interpolate_lch(&self, other: &Color, t: f32) -> Self
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
impl Color
Sourcepub fn from_rgb(r: f32, g: f32, b: f32) -> Self
👎Deprecated: Use new instead.
pub fn from_rgb(r: f32, g: f32, b: f32) -> Self
Arguments:
r: Red value [0..1]g: Green value [0..1]b: Blue value [0..1]
Sourcepub fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Self
👎Deprecated: Use new instead.
pub fn from_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]
Sourcepub fn from_rgb_u8(r: u8, g: u8, b: u8) -> Self
👎Deprecated: Use from_rgba8 instead.
pub fn from_rgb_u8(r: u8, g: u8, b: u8) -> Self
Arguments:
r: Red value [0..255]g: Green value [0..255]b: Blue value [0..255]
Sourcepub fn from_rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Self
👎Deprecated: Use from_rgba8 instead.
pub fn from_rgba_u8(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]
Sourcepub fn from_linear_rgb(r: f32, g: f32, b: f32) -> Self
👎Deprecated: Use from_linear_rgba instead.
pub fn from_linear_rgb(r: f32, g: f32, b: f32) -> Self
Arguments:
r: Red value [0..1]g: Green value [0..1]b: Blue value [0..1]
Sourcepub fn from_linear_rgb_u8(r: u8, g: u8, b: u8) -> Self
👎Deprecated: Use from_linear_rgba8 instead.
pub fn from_linear_rgb_u8(r: u8, g: u8, b: u8) -> Self
Arguments:
r: Red value [0..255]g: Green value [0..255]b: Blue value [0..255]
Sourcepub fn from_linear_rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Self
👎Deprecated: Use from_linear_rgba8 instead.
pub fn from_linear_rgba_u8(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]
Sourcepub fn from_hsv(h: f32, s: f32, v: f32) -> Self
👎Deprecated: Use from_hsva instead.
pub fn from_hsv(h: f32, s: f32, v: f32) -> Self
Arguments:
h: Hue angle [0..360]s: Saturation [0..1]v: Value [0..1]
Sourcepub fn from_hsl(h: f32, s: f32, l: f32) -> Self
👎Deprecated: Use from_hsla instead.
pub fn from_hsl(h: f32, s: f32, l: f32) -> Self
Arguments:
h: Hue angle [0..360]s: Saturation [0..1]l: Lightness [0..1]
Sourcepub fn from_hwb(h: f32, w: f32, b: f32) -> Self
👎Deprecated: Use from_hwba instead.
pub fn from_hwb(h: f32, w: f32, b: f32) -> Self
Arguments:
h: Hue angle [0..360]w: Whiteness [0..1]b: Blackness [0..1]
Sourcepub fn from_oklab(l: f32, a: f32, b: f32) -> Self
👎Deprecated: Use from_oklaba instead.
pub fn from_oklab(l: f32, a: f32, b: f32) -> Self
Arguments:
l: Perceived lightnessa: How green/red the color isb: How blue/yellow the color is
Sourcepub fn from_lab(l: f32, a: f32, b: f32, alpha: f32) -> Self
👎Deprecated: Use from_laba instead.
pub fn from_lab(l: f32, a: f32, b: f32, alpha: f32) -> Self
Arguments:
l: Lightnessa: Distance along theaaxisb: Distance along thebaxisalpha: Alpha [0..1]
Sourcepub fn from_lch(l: f32, c: f32, h: f32, alpha: f32) -> Self
👎Deprecated: Use from_lcha instead.
pub fn from_lch(l: f32, c: f32, h: f32, alpha: f32) -> Self
Arguments:
l: Lightnessc: Chromah: Hue angle in radiansalpha: Alpha [0..1]
Sourcepub fn rgba(&self) -> (f32, f32, f32, f32)
👎Deprecated
pub fn rgba(&self) -> (f32, f32, f32, f32)
Returns: (r, g, b, a)
- Red, green, blue and alpha in the range [0..1]
Sourcepub fn rgba_u8(&self) -> (u8, u8, u8, u8)
👎Deprecated: Use to_rgba8 instead.
pub fn rgba_u8(&self) -> (u8, u8, u8, u8)
Returns: (r, g, b, a)
- Red, green, blue and alpha in the range [0..255]
Sourcepub fn to_hex_string(&self) -> String
👎Deprecated: Use to_css_hex instead.
pub fn to_hex_string(&self) -> String
Get the RGB hexadecimal color string.
Sourcepub fn to_rgb_string(&self) -> String
👎Deprecated: Use to_css_rgb instead.
pub fn to_rgb_string(&self) -> String
Get the CSS rgb() format string.
Trait Implementations§
Source§impl ColorInterop for Color
impl ColorInterop for Color
Source§impl<'de> Deserialize<'de> for Color
Available on crate feature serde only.Implement Serde deserialization from string
impl<'de> Deserialize<'de> for Color
serde only.Implement Serde deserialization from string
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl From<EncodedSrgb<f32>> for Color
impl From<EncodedSrgb<f32>> for Color
Source§fn from(c: EncodedSrgb<f32>) -> Self
fn from(c: EncodedSrgb<f32>) -> Self
Source§impl From<EncodedSrgb<f64>> for Color
impl From<EncodedSrgb<f64>> for Color
Source§fn from(c: EncodedSrgb<f64>) -> Self
fn from(c: EncodedSrgb<f64>) -> Self
Source§impl From<EncodedSrgb> for Color
impl From<EncodedSrgb> for Color
Source§fn from(c: EncodedSrgb<u8>) -> Self
fn from(c: EncodedSrgb<u8>) -> Self
Source§impl From<Rgb<f32>> for Color
Available on crate feature rust-rgb only.Convert rust-rgb’s RGB<f32> type into Color.
impl From<Rgb<f32>> for Color
rust-rgb only.Convert rust-rgb’s RGB<f32> type into Color.
Source§impl From<Rgba<f32>> for Color
Available on crate feature rust-rgb only.Convert rust-rgb’s RGBA<f32> type into Color.
impl From<Rgba<f32>> for Color
rust-rgb only.Convert rust-rgb’s RGBA<f32> type into Color.
Source§impl PartialOrd for Color
impl PartialOrd for Color
Source§impl Serialize for Color
Available on crate feature serde only.Implement Serde serialization into HEX string
impl Serialize for Color
serde only.Implement Serde serialization into HEX string