chroma-forge
A comprehensive color conversion library for Rust that provides seamless conversion between various color formats including RGB, Hex, HSL, HSV, CMYK, and Minecraft color codes.
Features
- Multiple Color Formats: RGB/RGBA, Hex, HSL, HSV, CMYK
- Minecraft Color Codes: Legacy codes (§, &) and modern hex formats
- Color Utilities: Luminance calculation, dark/light detection, contrasting colors
- Color Manipulation: Blending, darkening, lightening
- Zero Dependencies: Pure Rust implementation
- Comprehensive Error Handling: Custom error types with detailed messages
- Extensive Testing: Full test coverage for all conversion methods
Installation
Add this to your Cargo.toml:
[]
= "1.0.0"
Quick Start
use Color;
Color Format Support
RGB/RGBA
let color = from_rgb;
let color_with_alpha = from_rgba;
let rgb = color.to_rgb;
Hex Colors
Supports multiple hex formats:
let color1 = from_hex?; // Full format
let color2 = from_hex?; // Without #
let color3 = from_hex?; // Short format
let color4 = from_hex?; // With alpha
let hex = color.to_hex; // #FF5733
let hex_alpha = color.to_hex_alpha; // #FF5733FF
HSL (Hue, Saturation, Lightness)
let color = from_hsl?; // Blue
let hsl = color.to_hsl;
HSV (Hue, Saturation, Value)
let color = from_hsv?; // Blue
let hsv = color.to_hsv;
CMYK (Cyan, Magenta, Yellow, Key/Black)
let color = from_cmyk?; // Cyan
let cmyk = color.to_cmyk;
Minecraft Color Codes
chroma-forge provides comprehensive support for Minecraft's color system, including both legacy and modern formats.
Legacy Color Codes
Supports all 16 standard Minecraft colors with both § and & prefixes:
// Legacy format parsing
let red = from_minecraft_code?; // Red
let blue = from_minecraft_code?; // Dark Blue
let yellow = from_minecraft_code?; // Yellow (no prefix)
// Convert to legacy format (finds closest match)
let mc_code = color.to_minecraft_code; // "§c"
Modern Hex Formats
Supports Minecraft's modern hex color formats:
// Modern hex format
let color1 = from_minecraft_code?;
// Alternate hex format
let color2 = from_minecraft_code?;
// Convert to modern formats
let mc_hex = color.to_minecraft_hex; // "&#FF5733"
let mc_alt = color.to_minecraft_hex_alt; // "&x&F&F&5&7&3&3"
Minecraft Color Constants
Pre-defined constants for all Minecraft colors:
let colors = ;
Color Utilities
Luminance and Brightness
let color = from_hex?;
let luminance = color.luminance; // 0.0 to 1.0
let is_dark = color.is_dark; // true/false
let is_light = color.is_light; // true/false
// Get contrasting color for text overlay
let contrast = color.contrasting_text_color; // Black or white
Color Manipulation
let color = from_hex?;
// Darken/lighten by percentage
let darker = color.darken; // 30% darker
let lighter = color.lighten; // 30% lighter
// Blend two colors
let red = RED;
let blue = BLUE;
let purple = red.blend; // 50% blend
Color Constants
Common colors are available as constants:
let colors = ;
Error Handling
chroma-forge uses a comprehensive error system:
use ;
match from_hex
Error types include:
InvalidHexFormatInvalidHexLengthInvalidRgbValueInvalidHslValueInvalidHsvValueInvalidCmykValueInvalidMinecraftCode
Examples
Basic Color Conversion
use Color;
Minecraft Integration
use Color;
Color Analysis
use Color;
Performance
chroma-forge is designed for performance with:
- Zero-copy string parsing where possible
- Efficient mathematical operations
- Minimal memory allocations
- No external dependencies
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. Make sure to:
- Add tests for new functionality
- Update documentation
- Follow Rust best practices
- Ensure all tests pass
License
This project is licensed under MIT License
Changelog
1.0.0
- Initial release
- RGB, Hex, HSL, HSV, CMYK support
- Minecraft color code support
- Color manipulation utilities
- Comprehensive error handling
- Full test coverage