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.
# prefix)
#rgb#rgba#rrggbb#rrggbbaargb() and rgba()hsl() and hsla()hwb()lab()lch()oklab()oklch()color(srgb ...)color(srgb-linear ...)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)
Doesn't support percentage.
calc() only support the following expression:
[OPERAND] [OPERATOR] [OPERAND]
OPERAND can be a number, a variable (r, g, b, alpha etc. depends on color function) or another expression wrapped in parenthesis.
OPERATOR is one of +, -, * or /.
rgb(from #bad455 100% g b)
rgb(from #bad455 r g b / 50%)
rgb(from #bad455 calc(r+g-30) 90 b)
rgb(from #bad455 255 g b)
rgb(from #bad455 r g b / 0.5)
rgb(from #bad455 calc(r+15) 90 b)
rgb(from #bad455 calc((r+g)-30) 90 b)
hwb(from rgb(from rgb(100% 0% 50%) r g 75) calc(h+25) w b)
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");
Default features can be disabled using default-features = false.
rgb crate types into Color.cint crate types to and from Color.serde framework.Rust
99.8%
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.
# prefix)
#rgb#rgba#rrggbb#rrggbbaargb() and rgba()hsl() and hsla()hwb()lab()lch()oklab()oklch()color(srgb ...)color(srgb-linear ...)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)
Doesn't support percentage.
calc() only support the following expression:
[OPERAND] [OPERATOR] [OPERAND]
OPERAND can be a number, a variable (r, g, b, alpha etc. depends on color function) or another expression wrapped in parenthesis.
OPERATOR is one of +, -, * or /.
rgb(from #bad455 100% g b)
rgb(from #bad455 r g b / 50%)
rgb(from #bad455 calc(r+g-30) 90 b)
rgb(from #bad455 255 g b)
rgb(from #bad455 r g b / 0.5)
rgb(from #bad455 calc(r+15) 90 b)
rgb(from #bad455 calc((r+g)-30) 90 b)
hwb(from rgb(from rgb(100% 0% 50%) r g 75) calc(h+25) w b)
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");
Default features can be disabled using default-features = false.
rgb crate types into Color.cint crate types to and from Color.serde framework.Rust
99.8%