hex_color
Lightweight Rust crate for RGB and RGBA hexadecimal colors
Repository Health
Technical Analysis
hex_color is a small, focused Rust library for working with RGB and RGBA colors expressed in hexadecimal notation. Its central HexColor type parses shorthand and full hex strings, exposes named color constants, and provides ergonomic constructors and mutators for building and adjusting colors.
The crate supports both strict and flexible parsing (RGB-only, RGBA-only, or either), conversions to and from packed u24/u32 integers, and optional serde serialization, rand generation, and no_std builds, making it equally suitable for graphics tools, config parsing, and embedded environments.
What You Get
- A HexColor type with parse, parse_rgb, and parse_rgba for strict or flexible input
- Named color constants (CYAN, WHITE, GRAY, and many more)
- Constructors from channels or packed integers via rgb, rgba, from_u24, and from_u32
- Channel mutators like with_a for immutable, fluent adjustments
- Optional serde, rand, and no_std feature flags for serialization, random colors, and embedded use
Common Use Cases
- Parsing hex color strings from configuration or user input
- Converting between hex strings and packed integer color values
- Generating or manipulating colors in graphics and design tooling
- Serializing colors to and from JSON with serde
Under The Hood
Architecture - The crate centers on a single HexColor struct holding r, g, b, and a byte channels. Parsing routines interpret 3/4/6/8-digit hex forms and expand shorthand, dispatching through parse, parse_rgb, and parse_rgba to enforce the desired channel set, and returning a dedicated error type on malformed input. Constructors and with_* methods build or clone-and-modify values without mutation surprises.
Tech Stack - Pure Rust with all extra capabilities gated behind optional features: serde (with arrayvec) for serialization, rand for random color generation, and a no_std-compatible core enabled by disabling the default std feature. Dependencies are optional and default-features-off to keep the baseline minimal.
Code Quality - The library is a single well-documented lib.rs whose module docs double as an example-rich specification of the supported hex syntax. CI runs via GitHub Actions, docs.rs builds all features with doc_cfg annotations, and dev-dependencies exercise serde_json round-trips.
API Design - Method names map directly to intent: parse for flexible input, parse_rgb/parse_rgba for strict variants, from_u24/from_u32 for integer conversion, and with_a for fluent immutable edits. Named constants and Result-based parsing make the common cases concise while keeping failure explicit.