Chromatic
A comprehensive Rust library with a straightforward API for working with multiple colour spaces.
Features
- No Standard Library: Works in
#![no_std]environments - Extensive colour space support: RGB, sRGB, HSL, HSV, Lab, XYZ, and greyscale
- Alpha channel variants: All colour spaces have alpha channel counterparts (e.g., RGBA, HSLA)
- Generic type representation: Use any floating-point type as the underlying representation (f32, f64, etc.)
- Flexible colourmap creation: Build and sample from colour gradients with any colour space
- Rich conversion system: Convert between any supported colour spaces
- CLI friendly: Display impls print ANSI-coloured blocks so you can eyeball gradients right in your terminal.
- Format flexibility: Convert to/from hex strings and byte arrays
- Complete colour mixing: Linear interpolation, perceptual colour differences, proper gamma handling for sRGB
Installation
Add chromatic to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use ;
use Float;
// 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
let colours = ;
let positions = ;
let cmap = new;
// Sample colours from the map
let orange = cmap.sample; // Between red and green
let teal = cmap.sample; // Between green and blue
// Display colours in terminal
println!;
println!;
println!;
println!;
println!;
println!;
Colour Spaces
Chromatic provides the following colour spaces:
| Space | Description | Components |
|---|---|---|
| Grey | Greyscale | Monochromaic intensity |
| Rgb | Linear RGB | Red, Green, Blue |
| Srgb | sRGB (gamma-corrected) | Red, Green, Blue |
| Hsl | HSL | Hue, Saturation, Lightness |
| Hsv | HSV | Hue, Saturation, Value |
| Lab | CIE Lab* | Lightness, a*, b* |
| Xyz | CIE XYZ | X, Y, Z |
Each colour space also has an alpha variant (e.g., GreyAlpha, RgbAlpha, etc.).
Working with Colour Maps
Colour maps allow you to create gradients between multiple colours:
use ;
// Create a rainbow colour map in HSV space
let rainbow = ;
// Create a uniformly spaced colour map
let cmap = new_uniform;
// Print the colour map to see the gradient
println!;
// Sample a colour from the map
let sampled_colour = cmap.sample;
Colour Conversions
All colours can be converted between spaces using the Convert trait:
use ;
let red = new;
// Convert to other spaces
let red_hsv = red.to_hsv;
let red_lab = red.to_lab;
// Convert to string representation
let hex = red.to_hex;
println!; // #FF0000
// Parse colour from hex
let parsed_red = from_hex.unwrap;
Perceptual Colour Differences
Chromatic includes methods to calculate perceptual colour differences:
use ;
let colour1 = new;
let colour2 = new;
// Convert to Lab for perceptual difference calculation
let lab1 = colour1.to_lab;
let lab2 = colour2.to_lab;
// Calculate perceptual colour difference using Delta E
let basic_delta_e = lab1.delta_e;
let improved_delta_e = lab1.delta_e94;
println!;
println!;
Terminal Visualization
All colour types implement Display and can be directly printed in terminals that support 24-bit colour:
use ;
// Create a simple gradient from black to white
let black = new;
let white = new;
let gradient = ;
let cmap = new_uniform;
// Print the entire colour map to visualize the gradient
println!;
Generic Type Support
Chromatic supports any floating-point type that implements the Float trait from num_traits:
use ;
use Float;
// Use f32 for memory efficiency
let red_f32 = new;
// Use f64 for higher precision
let red_f64 = new;
// Function that works with any float type
License
This project is licensed under the MIT License - see the LICENSE file for details.