#color #css-color #css #no-alloc

no-std css-named-colors

CSS Named Colors

2 stable releases

new 1.0.1 Feb 16, 2025

#1290 in Web programming

Download history 149/week @ 2025-02-11

149 downloads per month

MIT license

47KB
1K SLoC

CSS Named Colors

Basically what it says on the tin. Conversion to/from names, hex codes, and rgb triples.
No dependencies, no allocations, mostly const.

// parsing
let color = NamedColor::from_name("rebeccapurple").expect("invalid name");
// conversion
assert_eq!("#663399", &color.hex());
assert_eq!(Some((102, 51, 153)), &color.rgb());

// `transparent` is handled reasonably
assert_eq!("transparent", NamedColor::TRANSPARENT.hex());
assert!(NamedColor::TRANSPARENT.rgb().is_none());

// aliases/collisions are handled alphabetically
assert_eq!(NamedColor::AQUA.hex(), NamedColor::CYAN.hex());
assert_eq!(NamedColor::from_hex("#00ffff").unwrap(), NamedColor::AQUA); // _not_ CYAN

lib.rs:

CSS Named Colors

Adapted from the official documentation

Notes:

  1. transparent is a valid name that occasionally requires special handling
  2. When color names collide, I have deferred to alphabetic order ("aqua" instead of "cyan", "fuchsia" instead of "magenta, "gray" instead of "grey")

No runtime deps