This Rust crate, color-names, provides a comprehensive and efficiently searchable collection of color names. It's designed to help developers easily associate hexadecimal color values or RGB triplets with human-readable names, drawing from a vast, curated dataset.
Core Features
- Extensive Color Name Dataset: The crate leverages a meticulously assembled list of over 29,956 unique color names, sourced from various references and user contributions, provided by the
meodai/color-namesproject. - Multiple Color Sets: It organizes colors into different sets (e.g., "complete", "bestOf", "short"), allowing for focused searches and reduced bundle size if only specific sets are needed.
- Efficient Nearest Color Search (Implied): While the provided Rust code snippet doesn't explicitly detail the nearest color search algorithm, the reference to
meodai/color-namesandvycdev/ColorNamesSharpsuggests that efficient methods like K-D trees and CIELAB color space conversions are either implemented or intended for high accuracy and performance when finding the closest named color to a given input. - Flexible Color Representation: Colors can be represented and retrieved as:
- Hexadecimal strings (
#RRGGBB). - RGB tuples (
(r, g, b)). color::OpaqueColorfor integration with thecolorcrate.rgb::Rgbfor integration with thergbcrate.
- Hexadecimal strings (
- Parsing from Strings: The crate allows parsing color names directly from strings, making it convenient to convert user input or configuration values into
color-namesenums. - Serde Integration (Optional): With the
serdefeature enabled, color enums can be serialized and deserialized, facilitating their use in data structures and network communication. - Generated Code: The crate uses a
build.rsscript to generate Rust enums and associated implementations directly from the color data, ensuring up-to-date and type-safe access to the color names.
Key Components
colors.rs(Generated): This module contains all the generated Rust enums for each color set, along withimplblocks for retrieving hex values, color names, and converting tocolor::OpaqueColororrgb::Rgb. It also includesFromStrandTryFromimplementations for parsing color names from strings.ColorSetEnum: A master enum that enumerates all available color sets within the library.- Color Enums (e.g.,
Complete,BestOf,Short): Each color set has its own dedicated enum, where each variant represents a specific color by its sanitized name. HexParseErrorEnum: Defines the error types that can occur during color name parsing, such as invalid length or character in a hex string, or when a color name is not found.sanitize_identifierFunction: A utility function used during code generation to convert human-readable color names into valid Rust identifiers, handling special characters and leading digits.
Usage
Installation:
Add color-names to your Cargo.toml:
[]
= "0.1.0" # Use the latest version
= "0.8" # For RGB color representation
= "0.1" # For a more generalized color representation
= { = "1.0", = ["derive"], = true }
Basic Example:
use ;
use ToHex;