Chromatic
A comprehensive Rust library for working with colours across multiple colour spaces, featuring robust conversions, colour maps, and terminal visualization.
✨ Features
- 🎨 Complete colour space support: RGB, sRGB, HSL, HSV, Lab, XYZ, and greyscale
- 🔍 Alpha channel variants: All colour spaces include transparency support (RGBA, HSLA, etc.)
- 🧮 Generic numeric types: Use any floating-point type (f32, f64, custom) as the underlying representation
- 🌈 Powerful colour maps: Create and sample gradients with custom positioning and interpolation
- 🔄 Universal conversions: Convert seamlessly between any supported colour spaces
- 🖥️ Terminal visualization: Rich ANSI display support for visualizing colours and gradients
- 📐 Perceptual accuracy: Delta E colour difference calculations and proper gamma handling
- 🛡️ Memory safe: Zero unsafe code with comprehensive error handling
- ⚡ Performance focused: Efficient algorithms with minimal allocations
📦 Installation
Add chromatic to your Cargo.toml:
[]
= "0.1.0"
🚀 Quick Start
use ;
// Create colours in different spaces
let red = new?;
let green = new?;
let blue = new?;
// Convert between colour spaces
let red_hsv = red.to_hsv?;
let red_lab = red.to_lab?;
// Create a colour map and sample from it
let colours = ;
let cmap = new?;
let orange = cmap.sample?; // Between red and green
let teal = cmap.sample?; // Between green and blue
// Display colours in terminal (24-bit colour support)
println!;
println!;
println!;
println!;
println!;
🎨 Supported Colour Spaces
| Space | Description | Components | Range |
|---|---|---|---|
| Grey | Greyscale | Intensity | [0, 1] |
| Rgb | Linear RGB | Red, Green, Blue | [0, 1] each |
| Srgb | Standard RGB (gamma-corrected) | Red, Green, Blue | [0, 1] each |
| Hsl | Hue, Saturation, Lightness | H: [0°, 360°), S,L: [0, 1] | Cylindrical |
| Hsv | Hue, Saturation, Value | H: [0°, 360°), S,V: [0, 1] | Cylindrical |
| Lab | CIE L*a*b* (perceptually uniform) | L*: [0, 100], a*,b*: [-128, 127] | Perceptual |
| Xyz | CIE XYZ (device-independent) | X,Y,Z: [0, 1] | Linear tristimulus |
Each colour space has an alpha variant (e.g., RgbAlpha, HslAlpha) for transparency support.
🌈 Advanced Colour Maps
Create sophisticated gradients with custom positioning:
use ;
// Create a sunset gradient with custom positions
let sunset_colours = ;
let positions = ;
let sunset_map = from_positions?;
// Sample multiple colours for smooth transitions
let gradient_samples = sunset_map.sample_n?;
// Create gradients with custom interpolation
let smooth_gradient = sunset_map.sample_with?;
🔄 Colour Space Conversions
Seamless conversions between all supported colour spaces:
use ;
let color = new?;
// Convert to different spaces
let hsv = color.to_hsv?; // For hue-based operations
let lab = color.to_lab?; // For perceptual calculations
let srgb = color.to_srgb?; // For display/web use
let xyz = color.to_xyz?; // For device-independent work
// String representations
let hex = color.to_hex?; // "#CC4D99"
let bytes = color.to_bytes?; // [204, 77, 153]
// Parse from various formats
let from_hex = from_hex?;
let from_bytes = from_bytes?;
🎯 Perceptual Colour Operations
Calculate perceptually accurate colour differences and perform intelligent mixing:
use ;
let color1 = new?;
let color2 = new?;
// Convert to Lab for perceptual accuracy
let lab1 = color1.to_lab?;
let lab2 = color2.to_lab?;
// Calculate colour differences
let delta_e76 = lab1.delta_e; // Basic Delta E
let delta_e94 = lab1.delta_e94?; // Improved CIE94 formula
println!;
// < 1.0: Not perceptible
// 1-2: Perceptible with close observation
// 2-10: Perceptible at a glance
// > 10: Very different colours
// Advanced colour mixing
let mixed = mix?; // Weighted mix
let blended = lerp?; // 50/50 blend
// Create smooth gradients
let gradient = gradient?; // 10-step gradient
🖥️ Terminal Visualization
Rich terminal output with automatic colour detection:
use ;
// Create a vibrant rainbow
let rainbow = .map.?;
let rainbow_map = new?;
// Print colour map - automatically adapts to terminal width
println!;
// Individual colour blocks
for in rainbow.iter.enumerate
// Create data visualizations
let data = vec!;
for in data.iter.enumerate
🧮 Generic Type Support
Work with any floating-point precision:
use ;
use Float;
// Memory-efficient f32
let color_f32 = new?;
// High-precision f64
let color_f64 = new?;
// Generic functions work with any float type
// Custom float types (requires Float trait implementation)
// let custom_color = Rgb::<YourCustomFloat>::new(...);
🎨 Practical Examples
Creating a Heat Map
use ;
// Create a temperature color map
let heat_colors = ;
let heat_map = new?;
// Map temperature data to colors
let temperatures = vec!;
let max_temp = 40.0;
for temp in temperatures
Web-Safe Color Palette
use ;
// Generate web-safe colors
let base_color = from_hex?;
// Create variations
let lighter = ;
let darker = ;
println!;
println!;
println!;
🔧 Error Handling
Chromatic uses a comprehensive error system for robust applications:
use ;
match new
// Parsing errors
match from_hex
🚀 Performance Tips
- Use
f32for memory-constrained applications - Use
f64for high-precision scientific applications - Prefer Lab space for perceptual operations
- Use RGB/sRGB for display and web applications
- Cache ColourMap instances for repeated sampling
- Use
sample_n()for bulk gradient generation
📚 API Documentation
For detailed API documentation, visit docs.rs/chromatic.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.