Struct let_engine::Color
source · pub struct Color { /* private fields */ }
Expand description
Color is a struct that represents a color.
Implementations§
source§impl Color
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_rgb<T>(r: T, g: T, b: T) -> Result<Color, Error>where
T: Into<f64>,
pub fn from_rgb<T>(r: T, g: T, b: T) -> Result<Color, Error>where T: Into<f64>,
Create a color from RGB values.
Parameters
r
: Red value (0-255)g
: Green value (0-255)b
: Blue value (0-255)
Examples
use color_art::Color;
let color = Color::from_rgb(255, 51, 153).unwrap();
assert_eq!(color.hex(), "#f39");
sourcepub fn from_rgba<T>(r: T, g: T, b: T, a: f64) -> Result<Color, Error>where
T: Into<f64>,
pub fn from_rgba<T>(r: T, g: T, b: T, a: f64) -> Result<Color, Error>where T: Into<f64>,
Create a color from RGBA values.
Parameters
r
: Red value (0-255)g
: Green value (0-255)b
: Blue value (0-255)a
: Alpha value (0-1)
Examples
use color_art::Color;
let color = Color::from_rgba(255, 51, 153, 0.5).unwrap();
assert_eq!(color.rgba(), "rgba(255, 51, 153, 0.5)");
sourcepub fn from_hsl(h: f64, s: f64, l: f64) -> Result<Color, Error>
pub fn from_hsl(h: f64, s: f64, l: f64) -> Result<Color, 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<Color, Error>
pub fn from_hsv(h: f64, s: f64, v: f64) -> Result<Color, 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<Color, Error>
pub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Result<Color, 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<Color, Error>
pub fn from_hex(hex_str: &str) -> Result<Color, 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<Color, Error>
pub fn from_name(name: &str) -> Result<Color, 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
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<Color, Error>
pub fn mix(color1: &Color, color2: &Color, weight: f64) -> Result<Color, 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) -> Color
pub fn fade(&self, amount: f64) -> Color
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) -> Color
pub fn fade_in(&self, amount: f64) -> Color
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) -> Color
pub fn fade_out(&self, amount: f64) -> Color
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) -> Color
pub fn mix_with(&self, new_color: &Color, weight: f64) -> Color
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 desaturate(&self, amount: f64) -> Color
pub fn desaturate(&self, amount: f64) -> Color
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) -> Color
pub fn spin(&self, angle: f64) -> Color
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) -> Color
pub fn complement(&self) -> Color
Returns the complement of color.
Trait Implementations§
source§impl FromStr for Color
impl FromStr for Color
source§fn from_str(s: &str) -> Result<Color, Error>
fn from_str(s: &str) -> Result<Color, 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));
source§impl PartialEq for Color
impl PartialEq for Color
impl Copy for Color
impl StructuralPartialEq for Color
Auto Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<R, P> ReadPrimitive<R> for Pwhere
R: Read + ReadEndian<P>,
P: Default,
impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,
source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.