csscolorparser/lib.rs
1//! # Overview
2//!
3//! Rust 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/).
4//!
5//! ## Supported Color Format
6//!
7//! * [Named colors](https://www.w3.org/TR/css-color-4/#named-colors)
8//! * RGB hexadecimal (with and without `#` prefix)
9//! + Short format `#rgb`
10//! + Short format with alpha `#rgba`
11//! + Long format `#rrggbb`
12//! + Long format with alpha `#rrggbbaa`
13//! * `rgb()` and `rgba()`
14//! * `hsl()` and `hsla()`
15//! * `hwb()`
16//! * `lab()`
17//! * `lch()`
18//! * `oklab()`
19//! * `oklch()`
20//! * `hwba()`, `hsv()`, `hsva()` - not in CSS standard.
21//!
22//! ## Usage
23//!
24//! Add this to your `Cargo.toml`
25//!
26//! ```toml
27//! csscolorparser = "0.7"
28//! ```
29//!
30//! ## Examples
31//!
32//! Using [`csscolorparser::parse()`](fn.parse.html) function.
33//!
34//! ```rust
35//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
36//! let c = csscolorparser::parse("rgb(100%,0%,0%)")?;
37//!
38//! assert_eq!(c.to_array(), [1.0, 0.0, 0.0, 1.0]);
39//! assert_eq!(c.to_rgba8(), [255, 0, 0, 255]);
40//! assert_eq!(c.to_css_hex(), "#ff0000");
41//! assert_eq!(c.to_css_rgb(), "rgb(255 0 0)");
42//! # Ok(())
43//! # }
44//! ```
45//!
46//! Using `parse()` method on `&str`.
47//!
48//! ```rust
49//! use csscolorparser::Color;
50//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
51//!
52//! let c: Color = "#ff00007f".parse()?;
53//!
54//! assert_eq!(c.to_rgba8(), [255, 0, 0, 127]);
55//! assert_eq!(c.to_css_hex(), "#ff00007f");
56//! # Ok(())
57//! # }
58//! ```
59//!
60//! ## Default Feature
61//!
62//! * `named-colors`: Enables parsing from [named colors](https://www.w3.org/TR/css-color-4/#named-colors). Requires [`phf`](https://crates.io/crates/phf).
63//!
64//! ## Optional Features
65//!
66//! * `lab`: Enables parsing `lab()` and `lch()` color format.
67//! * `rust-rgb`: Enables converting from [`rgb`](https://crates.io/crates/rgb) crate types into `Color`.
68//! * `cint`: Enables converting [`cint`](https://crates.io/crates/cint) crate types to and from `Color`.
69//! * `serde`: Enables serializing (into HEX string) and deserializing (from any supported string color format) using [`serde`](https://serde.rs/) framework.
70
71#![forbid(unsafe_code)]
72#![warn(missing_docs)]
73
74mod color;
75mod color2;
76pub use color::Color;
77
78mod parser;
79pub use parser::{parse, ParseColorError};
80
81#[cfg(feature = "named-colors")]
82mod named_colors;
83#[cfg(feature = "named-colors")]
84pub use named_colors::NAMED_COLORS;
85
86#[cfg(feature = "cint")]
87mod cint;
88
89#[cfg(feature = "lab")]
90mod lab;
91
92mod utils;