Skip to content

About

Rust CSS color parser library

Topics

Resources

Stars

80 stars

Watchers

0 watching

Forks

Latest commit

 

History

281 Commits

Folders and files

Repository files navigation

Rust CSS Color Parser Library

License crates.io Documentation Build Status Total Downloads

Documentation • Changelog • Features


Rust library for parsing CSS color string as defined in the W3C's CSS Color Module Level 4.

Supported Color Format

Absolute Color

  • Named colors
  • RGB hexadecimal (with and without # prefix)
    • Short format #rgb
    • Short format with alpha #rgba
    • Long format #rrggbb
    • Long format with alpha #rrggbbaa
  • rgb() and rgba()
  • hsl() and hsla()
  • 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.

Relative Color

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)

Usage

Add this to your Cargo.toml

csscolorparser = "0.9"

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().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");

Features

Default

  • std: Using the standard library.
  • named-colors: Enables parsing from named colors.

Default features can be disabled using default-features = false.

Optional

  • rust-rgb: Enables converting from rgb crate types into Color.
  • cint: Enables converting cint crate types to and from Color.
  • serde: Enables serializing (into HEX string) and deserializing (from any supported string color format) using serde framework.

Similar Projects

About

Rust CSS color parser library

Topics

Resources

Stars

80 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages