#css-color #css #atomic

skribble_color

convert between different supported css color formats using the palette crate

1 unstable release

0.0.0 Apr 4, 2023

#20 in #css-color

24 downloads per month

Unlicense

49KB
1K SLoC

skribble_color

Parse supported css string into the supported color types.

Usage

Add the following to your Cargo.toml:

[dependencies]
skribble_color = "0.0.0"

Color Conversions

The following will convert a Color type into another Color type.

use skribble_color::Color;

let hex: Color = "#b1ffb0".parse().unwrap();
let rgb: Color = hex.into_rgb();
let hsl: Color = hex.into_hsl();
let hwb: Color = hex.into_hwb();
let lch: Color = hex.into_lch();
let lab: Color = hex.into_lab();
let oklch: Color = hex.into_oklch();
let oklab: Color = hex.into_oklab();

Examples

The following will parse a css string into a Color type automatically detecting the type of color.

Hex

use skribble_color::Color;
use skribble_color::Rgba;

let hex: Color = "#b1ffb0".parse().unwrap();
assert_eq!(hex.to_string(), "#b1ffb0");
assert_eq!(hex, Color::Hex(Rgba::new(0.69411767, 1.0, 0.6901961, 1.0)));

Rgb

use skribble_color::Color;
use skribble_color::Rgba;

let rgb: Color = "rgb(255, 0, 0)".parse().unwrap();
assert_eq!(rgb.to_string(), "rgb(255 0 0)");
assert_eq!(rgb, Color::Rgb(Rgba::new(1.0, 0.0, 0.0, 1.0)));

Hsl

use skribble_color::Color;
use skribble_color::Hsla;

let hsl: Color = "hsl(120, 100%, 50%)".parse().unwrap();
assert_eq!(hsl.to_string(), "hsl(120 100% 50%)");
assert_eq!(hsl, Color::Hsl(Hsla::new(120.0, 1.0, 0.5, 1.0)));

Hwb

use skribble_color::Color;
use skribble_color::Hwba;

let hwb: Color = "hwb(120 0% 0%)".parse().unwrap();
assert_eq!(hwb.to_string(), "hwb(120 0% 0%)");
assert_eq!(hwb, Color::Hwb(Hwba::new(120.0, 0.0, 0.0, 1.0)));

Dependencies

~4–6MB
~110K SLoC