pub struct Color { /* private fields */ }Expand description
Color is a struct that represents a color.
Implementations§
Source§impl Color
Color channel extraction methods.
impl Color
Color channel extraction methods.
§Examples
use color_art::{Color, color};
use std::str::FromStr;
let color = color!(rgba(10, 20, 30, 0.8));
assert_eq!(color.red(), 10);
assert_eq!(color.green(), 20);
assert_eq!(color.blue(), 30);
assert_eq!(color.alpha(), 0.8);
let color = Color::from_str("hsl(90, 100%, 50%)").unwrap();
assert_eq!(color.hue(), 90.0);
assert_eq!(color.saturation(), 1.0);
assert_eq!(color.lightness(), 0.5);Sourcepub fn alpha(&self) -> f64
pub fn alpha(&self) -> f64
Extracts the alpha channel of color as a number between 0.0 and 1.0.
Sourcepub fn saturation(&self) -> f64
pub fn saturation(&self) -> f64
Extracts the HSL saturation of color as a number between 0.0 and 1.0.
Sourcepub fn lightness(&self) -> f64
pub fn lightness(&self) -> f64
Extracts the HSL lightness of color as a number between 0.0 and 1.0.
Sourcepub fn whiteness(&self) -> f64
pub fn whiteness(&self) -> f64
Extracts the HWB whiteness of color as a number between 0.0 and 1.0.
Sourcepub fn blackness(&self) -> f64
pub fn blackness(&self) -> f64
Extracts the HWB blackness of color as a number between 0.0 and 1.0.
Sourcepub fn luma(&self) -> f64
pub fn luma(&self) -> f64
Calculates the relative luminance of color.
the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest white.
same as luminance()
Sourcepub fn luminance(&self) -> f64
pub fn luminance(&self) -> f64
Calculates the relative luminance of color.
the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest white.
same as luma()
Sourcepub fn hsv_saturation(&self) -> f64
pub fn hsv_saturation(&self) -> f64
Extracts the saturation channel of color in the HSV color space.
Source§impl Color
impl Color
Sourcepub fn from_hsl(h: f64, s: f64, l: f64) -> Result<Self, Error>
pub fn from_hsl(h: f64, s: f64, l: f64) -> Result<Self, Error>
Create a color from HSL values.
§Examples
use color_art::Color;
let color = Color::from_hsl(330.0, 1.0, 0.6).unwrap();
assert_eq!(color.hex(), "#f39");Sourcepub fn from_hsv(h: f64, s: f64, v: f64) -> Result<Self, Error>
pub fn from_hsv(h: f64, s: f64, v: f64) -> Result<Self, Error>
Create a color from HSV values.
§Examples
use color_art::Color;
let color = Color::from_hsv(38.82, 1.0, 1.0).unwrap();
assert_eq!(color.hex(), "#ffa500");Sourcepub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Result<Self, Error>
pub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Result<Self, Error>
Create a color from CMYK values.
§Examples
use color_art::Color;
let color = Color::from_cmyk(0.0, 0.8, 0.4, 0.0).unwrap();
assert_eq!(color.hex(), "#f39");Sourcepub fn from_hex(hex_str: &str) -> Result<Self, Error>
pub fn from_hex(hex_str: &str) -> Result<Self, Error>
Create a color from a hex string.
§Examples
use color_art::Color;
let color = Color::from_hex("#ff3399").unwrap();
assert_eq!(color.hex(), "#f39");
let color = Color::from_hex("#ff339933").unwrap();
assert_eq!(color.hex(), "#f393");Sourcepub fn from_name(name: &str) -> Result<Self, Error>
pub fn from_name(name: &str) -> Result<Self, Error>
Create a color from a color name.
Currently supported color names are:
- English color names from X11_color_names
- 中国传统色 (Chinese traditional colors)
§Examples
use color_art::Color;
let color = Color::from_name("yellow").unwrap();
assert_eq!(color.hex(), "#ff0");
let color = Color::from_name("水绿").unwrap();
assert_eq!(color.hex(), "#8cc269");Source§impl Color
Stringify a color to a string.
impl Color
Stringify a color to a string.
Sourcepub fn hex(self) -> String
pub fn hex(self) -> String
hex string of the color
The hex string is simplified to a short hex string if possible.
For example:
#ff00ff->#f0f#ffffff88->#fff8
§Examples
use color_art::Color;
let color = Color::new(255, 0, 255, 1.0);
assert_eq!(color.hex(), "#f0f");
let color = Color::new(255, 255, 255, 0.5);
assert_eq!(color.hex(), "#ffffff80");Sourcepub fn hex_full(self) -> String
pub fn hex_full(self) -> String
hex string of the color with the full length.
§Examples
use color_art::Color;
let color = Color::new(255, 0, 255, 1.0);
assert_eq!(color.hex_full(), "#ff00ff");Sourcepub fn rgb(self) -> String
pub fn rgb(self) -> String
rgb string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 255.0, 1.0);
assert_eq!(color.rgb(), "rgb(255, 255, 255)");Sourcepub fn rgba(self) -> String
pub fn rgba(self) -> String
rgba string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 255.0, 0.5);
assert_eq!(color.rgba(), "rgba(255, 255, 255, 0.5)");Sourcepub fn hsl(self) -> String
pub fn hsl(self) -> String
hsl string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 255.0, 1.0);
assert_eq!(color.hsl(), "hsl(0, 0%, 100%)");Sourcepub fn hsla(self) -> String
pub fn hsla(self) -> String
hsla string of the color
§Examples
use color_art::Color;
let color = Color::new(255, 255, 255, 0.3);
assert_eq!(color.hsla(), "hsla(0, 0%, 100%, 0.3)");Sourcepub fn hsv(self) -> String
pub fn hsv(self) -> String
hsv string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 255.0, 1.0);
assert_eq!(color.hsv(), "hsv(0, 0%, 100%)");Sourcepub fn hsi(self) -> String
pub fn hsi(self) -> String
hsi string of the color
§Examples
use color_art::Color;
let color = Color::new(255, 255, 255, 1.0);
assert_eq!(color.hsi(), "hsi(0, 0%, 100%)");Sourcepub fn hwb(self) -> String
pub fn hwb(self) -> String
hwb string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 255.0, 1.0);
assert_eq!(color.hwb(), "hwb(0, 100%, 0%)");Sourcepub fn cmyk(self) -> String
pub fn cmyk(self) -> String
cmyk string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 255.0, 1.0);
assert_eq!(color.cmyk(), "cmyk(0%, 0%, 0%, 0%)");Sourcepub fn xyz(self) -> String
pub fn xyz(self) -> String
xyz string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 0.0, 0.0, 1.0);
assert_eq!(color.xyz(), "xyz(0.412391, 0.212639, 0.019331)");Sourcepub fn yiq(self) -> String
pub fn yiq(self) -> String
yiq string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 0.0, 0.0, 1.0);
assert_eq!(color.yiq(), "yiq(0.299, 0.59572, 0.21146)");Sourcepub fn yuv(self) -> String
pub fn yuv(self) -> String
yuv string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 0.0, 0.0, 1.0);
assert_eq!(color.yuv(), "yuv(0.299, -0.1471, 0.6148)");Sourcepub fn lab(self) -> String
pub fn lab(self) -> String
lab string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 0.0, 1.0);
assert_eq!(color.lab(), "lab(97.61, -15.75, 93.39)");Sourcepub fn ycbcr(self) -> String
pub fn ycbcr(self) -> String
YCbCr string of the color
§Examples
use color_art::Color;
let color = Color::new(255.0, 255.0, 0.0, 1.0);
assert_eq!(color.ycbcr(), "YCbCr(225.93, 0.5755, 148.7269)");Sourcepub fn name(self) -> String
pub fn name(self) -> String
name of the color
The color name is based on the CSS3 color name or 中国传统色彩.
If the color is not named, the hex string will be returned.
§Examples
use color_art::{Color, color};
let color = color!(#ffffff);
assert_eq!(color.name(), "white");
let color = color!(#f8df72);
assert_eq!(color.name(), "茉莉黄");
let color = Color::new(42, 42, 42, 1.0);
assert_eq!(color.name(), "#2a2a2a");Source§impl Color
impl Color
Sourcepub fn vec_of(&self, color_space: impl Into<ColorSpace>) -> Vec<f64>
pub fn vec_of(&self, color_space: impl Into<ColorSpace>) -> Vec<f64>
Get the color space vector of the color instance.
⚗️ Experimental: This method is experimental and may change frequently in the future.
§Examples
use color_art::{color, ColorSpace};
let color = color!(rgb(255, 51, 153));
let vec = color.vec_of(ColorSpace::RGB);
assert_eq!(vec, vec![255.0, 51.0, 153.0]);
let vec = color.vec_of(ColorSpace::HSV);
assert_eq!(vec, vec![330.0, 0.8, 1.0]);Source§impl Color
impl Color
Sourcepub fn average(colors: &[Color]) -> Color
pub fn average(colors: &[Color]) -> Color
Average a list of colors.
This function will return a new color that is the average of the colors in the list. It will calculate the average of the RGB channels and alpha values of the colors. If the list length is 0, it will return a black color.
§Examples
use color_art::Color;
use std::str::FromStr;
let colors = vec![
Color::from_str("#ff6600").unwrap(),
Color::from_str("rgba(0, 0, 0, 0.5)").unwrap(),
];
let averaged_color = Color::average(&colors);
assert_eq!(averaged_color.rgba(), "rgba(128, 51, 0, 0.75)");Source§impl Color
impl Color
Sourcepub fn mix(color1: &Color, color2: &Color, weight: f64) -> Result<Self, Error>
pub fn mix(color1: &Color, color2: &Color, weight: f64) -> Result<Self, Error>
Mix two colors with a weight.
§Arguments
color1- The first color.color2- The second color.weight- The weight of the first color. Must be between 0.0 and 1.0.
§Examples
use color_art::{Color, color};
let color1 = color!(#998099);
let color2 = color!(#191970);
let color3 = Color::mix(&color1, &color2, 0.5).unwrap();
assert_eq!(color3.hex(), "#594d85");Source§impl Color
impl Color
Source§impl Color
impl Color
Sourcepub fn fade(&self, amount: f64) -> Self
pub fn fade(&self, amount: f64) -> Self
Set the absolute opacity of a color.
Can be applied to colors whether they already have an opacity value or not.
§Examples
use color_art::color;
let color = color!(rgba(0, 255, 0, 0.8));
assert_eq!(color.alpha(), 0.8);
let color = color.fade(0.5);
assert_eq!(color.alpha(), 0.5);Sourcepub fn fade_in(&self, amount: f64) -> Self
pub fn fade_in(&self, amount: f64) -> Self
Decrease the transparency (or increase the opacity) of a color, making it more opaque.
§Examples
use color_art::color;
let color = color!(rgba(0, 255, 0, 0.8));
assert_eq!(color.alpha(), 0.8);
let color = color.fade_in(0.1);
assert_eq!(color.alpha(), 0.9);Sourcepub fn fade_out(&self, amount: f64) -> Self
pub fn fade_out(&self, amount: f64) -> Self
Increase the transparency (or decrease the opacity) of a color, making it less opaque.
§Examples
use color_art::color;
let color = color!(rgba(0, 255, 0, 0.8));
assert_eq!(color.alpha(), 0.8);
let color = color.fade_out(0.2);
assert_eq!(color.alpha(), 0.6);Source§impl Color
impl Color
Sourcepub fn mix_with(&self, new_color: &Color, weight: f64) -> Self
pub fn mix_with(&self, new_color: &Color, weight: f64) -> Self
Mix two colors with a weight.
§Arguments
color- The color to mix with.weight- The weight of the new color to mix with. 0.0 is all the original color, 1.0 is all the new color.
§Examples
use color_art::color;
let color1 = color!(#998099);
let color2 = color!(#d2e1dd);
let color3 = color1.mix_with(&color2, 0.5);
assert_eq!(color3.hex(), "#b6b1bb");Source§impl Color
impl Color
Sourcepub fn saturate(&self, amount: f64) -> Self
pub fn saturate(&self, amount: f64) -> Self
Increase the saturation of a color in the HSL color space by an absolute amount.
§Arguments
amount - The amount to increase the saturation by. Must be between 0.0 and 1.0.
§Examples
use color_art::color;
let color = color!(#80e619);
let color = color.saturate(0.2);
assert_eq!(color.hex(), "#80ff00");Sourcepub fn desaturate(&self, amount: f64) -> Self
pub fn desaturate(&self, amount: f64) -> Self
Decrease the saturation of a color in the HSL color space by an absolute amount.
§Arguments
amount - The amount to decrease the saturation by. Must be between 0.0 and 1.0.
§Examples
use color_art::color;
let color = color!(#80e619);
let color = color.desaturate(0.2);
assert_eq!(color.hex(), "#80cd32");Source§impl Color
impl Color
Sourcepub fn spin(&self, angle: f64) -> Self
pub fn spin(&self, angle: f64) -> Self
Rotate the hue angle of a color in either direction.
§Arguments
angle- The angle to rotate the hue by. Positive values rotate clockwise, negative values rotate counter-clockwise.
§Examples
use color_art::Color;
use std::str::FromStr;
let color = Color::from_str("hsl(10, 90%, 50%)").unwrap();
let color = color.spin(30.0);
assert_eq!(color.hsl(), "hsl(40, 90%, 50%)");
let color = Color::from_str("hsl(10, 90%, 50%)").unwrap();
let color = color.spin(-30.0);
assert_eq!(color.hsl(), "hsl(340, 90%, 50%)");Sourcepub fn complement(&self) -> Self
pub fn complement(&self) -> Self
Returns the complement of color.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Color
impl<'de> Deserialize<'de> for Color
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromStr for Color
impl FromStr for Color
Source§fn from_str(s: &str) -> Result<Self, Error>
fn from_str(s: &str) -> Result<Self, Error>
Creates a new Color from a string.
§Examples
use color_art::Color;
use std::str::FromStr;
let s = "rgb(255, 255, 255)";
let color = Color::from_str(s).unwrap();
assert_eq!(color, Color::new(255, 255, 255, 1.0));
let s = "rgba(255, 255, 255, 0.5)";
let color = Color::from_str(s).unwrap();
assert_eq!(color, Color::new(255, 255, 255, 0.5));
let s = "#ffffff";
let color = Color::from_str(s).unwrap();
assert_eq!(color, Color::new(255, 255, 255, 1.0));
let s = "hsl(0, 0%, 100%)";
let color = Color::from_str(s).unwrap();
assert_eq!(color, Color::new(255, 255, 255, 1.0));
let s = "hsv(0, 0%, 100%)";
let color = Color::from_str(s).unwrap();
assert_eq!(color, Color::new(255, 255, 255, 1.0));
let s = "deeppink";
let color = Color::from_str(s).unwrap();
assert_eq!(color, Color::new(255, 20, 147, 1.0));
let s = "水绿";
let color = Color::from_str(s).unwrap();
assert_eq!(color, Color::new(140, 194, 105, 1.0));