Rust CSS Color Parser

crates.io Documentation Build Status Build Status codecov

Rust library to parse CSS color string as defined in the W3C's CSS Color Module Level 4.

Supported Color Format

Not yet supported: lab(), lch().

Example Color Format

```text transparent gold rebeccapurple lime

0f0

0f0f

00ff00

00ff00ff

rgb(0,255,0) rgb(0% 100% 0%) rgb(0 255 0 / 100%) rgba(0,255,0,1) hsl(120,100%,50%) hsl(120deg 100% 50%) hsl(-240 100% 50%) hsl(-240deg 100% 50%) hsl(0.3333turn 100% 50%) hsl(133.333grad 100% 50%) hsl(2.0944rad 100% 50%) hsla(120,100%,50%,100%) hwb(120 0% 0%) hwb(480deg 0% 0% / 100%) hsv(120,100%,100%) hsv(120deg 100% 100% / 100%) ```

Usage

Add csscolorparser to your Cargo.toml

text [dependencies] csscolorparser = "0.3.0"

Examples

Using csscolorparser::parse() function.

```rust let c = csscolorparser::parse("rgb(100%,0%,0%)").unwrap();

asserteq!(c.rgba(), (1., 0., 0., 1.)); asserteq!(c.rgbau8(), (255, 0, 0, 255)); asserteq!(c.tohexstring(), "#ff0000"); asserteq!(c.torgb_string(), "rgb(255,0,0)"); ```

Using parse() method on string.

```rust use csscolorparser::Color;

let c = "#ff00007f".parse::().unwrap();

asserteq!(c.rgbau8(), (255, 0, 0, 127)); asserteq!(c.tohex_string(), "#ff00007f"); ```

Using Color::from_html().

```rust use csscolorparser::Color;

let c = Color::from_html("skyblue").unwrap();

asserteq!(c.rgbau8(), (135, 206, 235, 255)); asserteq!(c.tohexstring(), "#87ceeb"); asserteq!(c.torgbstring(), "rgb(135,206,235)"); ```

Links