csscolorparser

Crates.iocsscolorparser
lib.rscsscolorparser
version0.7.2
created_at2020-12-13 17:07:25.830121+00
updated_at2025-06-03 11:53:27.862981+00
descriptionCSS color parser library
homepage
repositoryhttps://github.com/mazznoer/csscolorparser-rs
max_upload_size
id322452
size150,737
Nor Khasyatillah (mazznoer)

documentation

https://docs.rs/csscolorparser/

README

Rust CSS Color Parser Library

License crates.io Documentation Build Status Total Downloads

DocumentationChangelogFeatures


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
  • rgb() and rgba()
  • hsl() and hsla()
  • 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)");
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(), "#ff00007f");

Features

Default

  • named-colors: Enables parsing from named colors. Requires phf. Can be disabled using default-features = false.

Optional

  • lab: Enables parsing lab() and lch() color format.
  • 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

Commit count: 161

cargo fmt