A rust crate for working with colors and color spaces.
See Color Art.
toml
[dependencies]
color-art = "0.2"
You can use the from_str
method to construct a color from a string.
Currently supported color formats:
rgb
rgba
hex
hsl
hsv
hwb
cmyk
xyz
yuv
YCbCr
named color
from w3cx11For example:
```rust use color_art::Color; use std::str::FromStr;
let color = Color::fromstr("rgb(255, 255, 0)").unwrap(); let color = Color::fromstr("rgba(255, 255, 0, 0.5)").unwrap(); let color = Color::fromstr("#ffff00").unwrap(); let color = Color::fromstr("hsl(60, 100%, 50%)").unwrap(); let color = Color::fromstr("hsv(60, 100%, 100%)").unwrap(); let color = Color::fromstr("hwb(60, 0%, 0%)").unwrap(); let color = Color::fromstr("cmyk(0%, 0%, 100%, 0%)").unwrap(); let color = Color::fromstr("xyz(0.932231, 0.975339, 0.502949)").unwrap(); let color = Color::fromstr("yuv(0.886, -0.4359, 0.1)").unwrap(); let color = Color::fromstr("YCbCr(225.93, 0.5755, 148.7269)").unwrap(); let color = Color::from_str("yellow").unwrap(); ```
You can use the from_num
method to construct a color from a number.
For example:
```rust use color_art::Color;
let color = Color::fromnum(16776960).unwrap(); let color = Color::fromnum(0xffff00).unwrap(); ```
You can use the from_<color_space>
method to construct a color from a color space.
Currently supported color spaces:
rgb
rgba
hsl
hsv
cmyk
hex
For example:
```rust use color_art::Color;
let color = Color::fromrgb(255, 255, 0).unwrap(); let color = Color::fromrgba(255, 255, 0, 0.5).unwrap(); let color = Color::fromhsl(60.0, 1.0, 0.5).unwrap(); let color = Color::fromhsv(60.0, 1.0, 1.0).unwrap(); let color = Color::fromcmyk(0.0, 0.0, 1.0, 0.0).unwrap(); let color = Color::fromhex("#ffff00").unwrap(); ```
Stringify a color to a string.
You can use the hex
, rgb
, rgba
, hsl
, hsv
, hwb
, cmyk
, xyz
, yuv
, YCbCr
, lab
, name
method to stringify a color to a string.
For example:
```rust use color_art::Color; use std::str::FromStr;
let color = Color::from_str("#ffff00").unwrap(); color.hex(); // "#ffff00" color.rgb(); // "rgb(255, 255, 0)" color.rgba(); // "rgba(255, 255, 0, 1)" color.hsl(); // "hsl(60, 100%, 50%)" color.hsv(); // "hsv(60, 100%, 100%)" color.hwb(); // "hwb(60, 0%, 0%)" color.cmyk(); // "cmyk(0%, 0%, 100%, 0%)" color.xyz(); // "xyz(0.932231, 0.975339, 0.502949)" color.yuv(); // "yuv(0.886, -0.4359, 0.1)" color.ycbcr(); // "YCbCr(225.93, 0.5755, 148.7269)" color.name(); // "yellow" color.lab(); // "lab(97.14, -21.55, 94.48)" ```
You can use the red
, green
, blue
, alpha
, hue
, saturation
, lightness
, hsv_hue
, hsv_saturation
, hsv_value
, luma
, luminance
, gray
method to extract the color channel.
```rust use color_art::Color; use std::str::FromStr;
let color = Color::from_str("#abcdef").unwrap();
color.red(); // 171 color.green(); // 205 color.blue(); // 239 color.alpha(); // 1.0
color.hue(); // 210.0 color.saturation(); // 0.68 color.lightness(); // 0.8
color.hsvhue(); // 210.0 color.hsvsaturation(); // 0.28 color.hsv_value(); // 0.94
color.luma(); // 0.59 color.luminance(); // 0.79
color.gray(); // 198.71 ```
More details, please refer to Color Operation.
Made with ❤️ by JiatLn.