Rust CSS Color Parser Library
Rust library to parse CSS color string as defined in the W3C's CSS Color Module Level 4.
Supported Color Format
- 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()
hwba()
,hsv()
,hsva()
- not in CSS standard.
Not yet supported: lab()
, lch()
.
Example Color Format
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
= "0.5.0"
Examples
Using csscolorparser::parse()
function.
let c = parse?;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Using parse()
method on &str
.
use Color;
let c = "#ff00007f".?;
assert_eq!;
assert_eq!;
Default Feature
named-colors
: Enables parsing from named colors. Requiresphf
.
Optional Features
rust-rgb
: Enables converting fromrgb
crate types intoColor
.cint
: Enables convertingcint
crate types to and fromColor
.serde
: Enables serializing (into HEX string) and deserializing (from any supported string color format) usingserde
framework.
Similar Projects
- csscolorparser (Go)
- csscolorparser (Javascript)