Expand description
§Overview
Rust library for parsing 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()
lab()
lch()
oklab()
oklch()
hwba()
,hsv()
,hsva()
- not in CSS standard.
§Usage
Add this to your Cargo.toml
csscolorparser = "0.7"
§Examples
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(), "#ff0000");
assert_eq!(c.to_css_rgb(), "rgb(255 0 0)");
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(), "#ff00007f");
§Default Feature
named-colors
: Enables parsing from named colors. Requiresphf
.
§Optional Features
lab
: Enables parsinglab()
andlch()
color format.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.
Structs§
- Color
- The color
Enums§
- Parse
Color Error - An error which can be returned when parsing a CSS color string.
Statics§
- NAMED_
COLORS - Named colors defined in https://www.w3.org/TR/css-color-4/#named-colors.
Functions§
- parse
- Parse CSS color string