Rust CSS Color Parser Library
Documentation • Changelog • Features
Rust library for parsing CSS color string as defined in the W3C's CSS Color Module Level 4.
- Named colors
- RGB hexadecimal (with and without
#prefix)- Short format
#rgb - Short format with alpha
#rgba - Long format
#rrggbb - Long format with alpha
#rrggbbaa
- Short format
rgb()andrgba()hsl()andhsla()hwb()lab()lch()oklab()oklch()color(srgb ...)color(srgb-linear ...)color(xyz ...)color(xyz-d50 ...)color(xyz-d65 ...)hwba(),hsv(),hsva()- not in CSS standard.
Example:
rgb(from red r g calc(b + 20))
rgb(from gold calc((r + g + b) / 3) 127 127)
hwb(from #bad455 calc(h + 35) w b)
hsl(from purple h s l / 0.5)
Add this to your Cargo.toml
csscolorparser = "0.9"Using csscolorparser::parse() function.
let c = csscolorparser::parse("rgb(100%,0%,0%)")?;
assert_eq!(c.to_array(), [1.0, 0.0, 0.0, 1.0]);
assert_eq!(c.to_rgba8(), [255, 0, 0, 255]);
assert_eq!(c.to_css_hex().to_string(), "#ff0000");
assert_eq!(c.to_css_rgb().to_string(), "rgb(255 0 0)");
assert_eq!(c.name(), Some("red"));Using parse() method on &str.
use csscolorparser::Color;
let c: Color = "#ff00007f".parse()?;
assert_eq!(c.to_rgba8(), [255, 0, 0, 127]);
assert_eq!(c.to_css_hex().to_string(), "#ff00007f");- std: Using the standard library.
- named-colors: Enables parsing from named colors.
Default features can be disabled using default-features = false.
- rust-rgb: Enables converting from
rgbcrate types intoColor. - cint: Enables converting
cintcrate types to and fromColor. - serde: Enables serializing (into HEX string) and deserializing (from any supported string color format) using
serdeframework.
- csscolorparser (Go)
- csscolorparser (Javascript)