| Crates.io | colorsnamed |
| lib.rs | colorsnamed |
| version | 0.2.1 |
| created_at | 2025-11-02 16:50:36.947713+00 |
| updated_at | 2025-11-07 17:18:37.247918+00 |
| description | Names for a large set of widely understood color sets, like HTML/CSS or xkcd |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1913319 |
| size | 3,772,020 |
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.
meodai/color-names project.meodai/color-names and vycdev/ColorNamesSharp suggests 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.#RRGGBB).(r, g, b)).color::OpaqueColor for integration with the color crate.rgb::Rgb for integration with the rgb crate.color-names enums.serde feature enabled, color enums can be serialized and deserialized, facilitating their use in data structures and network communication.build.rs script to generate Rust enums and associated implementations directly from the color data, ensuring up-to-date and type-safe access to the color names.colors.rs (Generated): This module contains all the generated Rust enums for each color set, along with impl blocks for retrieving hex values, color names, and converting to color::OpaqueColor or rgb::Rgb. It also includes FromStr and TryFrom implementations for parsing color names from strings.ColorSet Enum: A master enum that enumerates all available color sets within the library.Complete, BestOf, Short): Each color set has its own dedicated enum, where each variant represents a specific color by its sanitized name.HexParseError Enum: 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_identifier Function: A utility function used during code generation to convert human-readable color names into valid Rust identifiers, handling special characters and leading digits.Installation:
Add color-names to your Cargo.toml:
[dependencies]
color-names = "0.1.0" # Use the latest version
rgb = "0.8" # For RGB color representation
color = "0.1" # For a more generalized color representation
serde = { version = "1.0", features = ["derive"], optional = true }
Basic Example:
use color_names::{Complete, HexParseError};
use hex::ToHex;
fn main() -> Result<(), HexParseError> {
// Get hex value
let white_hex = Complete::White.hex();
println!("White hex: {}", white_hex); // => #ffffff
// Get color name
let classic_rose_name = Complete::ClassicRose.color_name();
println!("Classic Rose name: {}", classic_rose_name); // => Classic Rose
// Parse from string
let parsed_color: Complete = "Eigengrau".parse()?;
println!("Parsed color hex: {}", parsed_color.hex()); // => #16161d
// Convert to RGB
let fairy_tale_rgb = Complete::FairyTale.rgb();
println!("Fairy Tale RGB: R:{}, G:{}, B:{}", fairy_tale_rgb.r, fairy_tale_rgb.g, fairy_tale_rgb.b); // => R:241, G:193, B:209
// Convert to a generic color type (using the 'color' crate)
let stoic_white_color: color::OpaqueColor<color::Srgb> = Complete::StoicWhite.color();
println!("Stoic White Color (normalized components): {:?}", stoic_white_color.components); // => [0.8784314, 0.8784314, 1.0]
// Get uppercase hex
let silky_pink_hex_upper: String = Complete::SilkyPink.encode_hex_upper();
println!("Silky Pink uppercase hex: {}", silky_pink_hex_upper); // => FFDBF0
Ok(())
}