Rust CSS Color Parser Library

License crates.io Documentation Build Status codecov Total Downloads

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

Supported Color Format

Example Color Format

Click to expand!

```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 this to your Cargo.toml

toml csscolorparser = "0.6.2"

Examples

Using csscolorparser::parse() function.

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

asserteq!(c.toarray(), [1.0, 0.0, 0.0, 1.0]); asserteq!(c.torgba8(), [255, 0, 0, 255]); asserteq!(c.tohexstring(), "#ff0000"); asserteq!(c.torgbstring(), "rgb(255,0,0)"); ```

Using parse() method on &str.

```rust use csscolorparser::Color;

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

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

Default Feature

Optional Features

Similar Projects