| 1 | <p align="center"> |
| 2 | <strong>Rust CSS Color Parser Library</strong> |
| 3 | </p> |
| 4 | |
| 5 | <p align="center"> |
| 6 | <a href="https://github.com/mazznoer/csscolorparser-rs"><img alt="License" src="https://img.shields.io/crates/l/csscolorparser"></a> |
| 7 | <a href="https://crates.io/crates/csscolorparser"><img alt="crates.io" src="https://img.shields.io/crates/v/csscolorparser.svg"></a> |
| 8 | <a href="https://docs.rs/csscolorparser"><img alt="Documentation" src="https://docs.rs/csscolorparser/badge.svg"></a> |
| 9 | <a href="https://github.com/mazznoer/csscolorparser-rs/actions"><img alt="Build Status" src="https://github.com/mazznoer/csscolorparser-rs/actions/workflows/rust.yml/badge.svg"></a> |
| 10 | <a href="https://crates.io/crates/csscolorparser"><img alt="Total Downloads" src="https://img.shields.io/crates/d/csscolorparser.svg"></a> |
| 11 | </p> |
| 12 | |
| 13 | <p align="center"> |
| 14 | <strong> |
| 15 | <a href="https://docs.rs/csscolorparser">Documentation</a> • <a href="CHANGELOG.md">Changelog</a> • <a href="#features">Features</a> |
| 16 | </strong> |
| 17 | </p> |
| 18 | |
| 19 | <hr> |
| 20 | |
| 21 | [Rust](https://www.rust-lang.org/) library for parsing CSS color string as defined in the W3C's [CSS Color Module Level 4](https://www.w3.org/TR/css-color-4/). |
| 22 | |
| 23 | ## Supported Color Format |
| 24 | |
| 25 | ### Absolute Color |
| 26 | |
| 27 | * [Named colors](https://www.w3.org/TR/css-color-4/#named-colors) |
| 28 | * RGB hexadecimal (with and without `#` prefix) |
| 29 | + Short format `#rgb` |
| 30 | + Short format with alpha `#rgba` |
| 31 | + Long format `#rrggbb` |
| 32 | + Long format with alpha `#rrggbbaa` |
| 33 | * `rgb()` and `rgba()` |
| 34 | * `hsl()` and `hsla()` |
| 35 | * `hwb()` |
| 36 | * `lab()` |
| 37 | * `lch()` |
| 38 | * `oklab()` |
| 39 | * `oklch()` |
| 40 | * `hwba()`, `hsv()`, `hsva()` - not in CSS standard. |
| 41 | |
| 42 | ### Relative Color |
| 43 | |
| 44 | Example: |
| 45 | |
| 46 | ```text |
| 47 | rgb(from red r g calc(b + 20)) |
| 48 | rgb(from gold calc(((r + g) + b) / 3) 127 127) |
| 49 | hwb(from #bad455 calc(h + 35) w b) |
| 50 | hsl(from purple h s l / 0.5) |
| 51 | ``` |
| 52 | |
| 53 | #### Relative Color Format Limitations |
| 54 | |
| 55 | Doesn't support percentage. |
| 56 | |
| 57 | `calc()` only support the following expression: |
| 58 | |
| 59 | ```[OPERAND] [OPERATOR] [OPERAND]``` |
| 60 | |
| 61 | `OPERAND` can be a number, a variable (`r`, `g`, `b`, `alpha` etc. depends on color function) or another expression wrapped in parenthesis. |
| 62 | `OPERATOR` is one of `+`, `-`, `*` or `/`. |
| 63 | |
| 64 | ##### Not Supported |
| 65 | |
| 66 | ``` |
| 67 | rgb(from #bad455 100% g b) |
| 68 | rgb(from #bad455 r g b / 50%) |
| 69 | rgb(from #bad455 calc(r+g-30) 90 b) |
| 70 | ``` |
| 71 | |
| 72 | ##### OK |
| 73 | |
| 74 | ``` |
| 75 | rgb(from #bad455 255 g b) |
| 76 | rgb(from #bad455 r g b / 0.5) |
| 77 | rgb(from #bad455 calc(r+15) 90 b) |
| 78 | rgb(from #bad455 calc((r+g)-30) 90 b) |
| 79 | hwb(from rgb(from rgb(100% 0% 50%) r g 75) calc(h+25) w b) |
| 80 | ``` |
| 81 | |
| 82 | ## Usage |
| 83 | |
| 84 | Add this to your `Cargo.toml` |
| 85 | |
| 86 | ```toml |
| 87 | csscolorparser = "0.8" |
| 88 | ``` |
| 89 | |
| 90 | ## Examples |
| 91 | |
| 92 | Using `csscolorparser::parse()` function. |
| 93 | |
| 94 | ```rust |
| 95 | let c = csscolorparser::parse("rgb(100%,0%,0%)")?; |
| 96 | |
| 97 | assert_eq!(c.to_array(), [1.0, 0.0, 0.0, 1.0]); |
| 98 | assert_eq!(c.to_rgba8(), [255, 0, 0, 255]); |
| 99 | assert_eq!(c.to_css_hex(), "#ff0000"); |
| 100 | assert_eq!(c.to_css_rgb(), "rgb(255 0 0)"); |
| 101 | assert_eq!(c.name(), Some("red")); |
| 102 | ``` |
| 103 | |
| 104 | Using `parse()` method on `&str`. |
| 105 | |
| 106 | ```rust |
| 107 | use csscolorparser::Color; |
| 108 | |
| 109 | let c: Color = "#ff00007f".parse()?; |
| 110 | |
| 111 | assert_eq!(c.to_rgba8(), [255, 0, 0, 127]); |
| 112 | assert_eq!(c.to_css_hex(), "#ff00007f"); |
| 113 | ``` |
| 114 | |
| 115 | ## Features |
| 116 | |
| 117 | ### Default |
| 118 | |
| 119 | * __std__: Using the standard library. |
| 120 | * __named-colors__: Enables parsing from [named colors](https://www.w3.org/TR/css-color-4/#named-colors). |
| 121 | |
| 122 | Default features can be disabled using `default-features = false`. |
| 123 | |
| 124 | ### Optional |
| 125 | |
| 126 | * __rust-rgb__: Enables converting from [`rgb`](https://crates.io/crates/rgb) crate types into `Color`. |
| 127 | * __cint__: Enables converting [`cint`](https://crates.io/crates/cint) crate types to and from `Color`. |
| 128 | * __serde__: Enables serializing (into HEX string) and deserializing (from any supported string color format) using [`serde`](https://serde.rs/) framework. |
| 129 | |
| 130 | ## Similar Projects |
| 131 | |
| 132 | * [csscolorparser](https://github.com/mazznoer/csscolorparser) (Go) |
| 133 | * [csscolorparser](https://github.com/deanm/css-color-parser-js) (Javascript) |
| 134 |