Chromatic
Chromatic is a Rust library for building and sampling colour maps with perceptually uniform interpolation. It provides a flexible, type-safe way to work with various colour representations and create smooth transitions between colours.
Features
- Multiple colour space representations:
- Basic colour types:
Rgb,Rgba,Grey,GreyAlpha - Perceptually uniform colour types:
LabRgb,LabRgba(using CIE Lab colour space)
- Basic colour types:
- Flexible
ColourMapfor interpolation between multiple colours - Perceptually uniform colour mixing with Lab colour space
- Type-safe conversions between colour spaces
- String parsing and formatting (hex notation and comma-separated values)
- Generic implementation with
Floattrait support
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Examples
Creating and Using Colors
use ;
// Create RGB colours
let red = new;
let blue = new;
// Create with alpha
let transparent_green = new;
// Parse from string (hex notation)
let purple = from_str.unwrap;
// Convert between colour spaces
let grey = red.to_grey;
let lab_red = red.to_lab_rgb; // Better for interpolation
Creating a Color Map
use ;
// Create colours
let red = new;
let green = new;
let blue = new;
// Create a colour map with positions
let colours = vec!;
let positions = vec!;
let colour_map = new;
// Sample the colour map
let middle = colour_map.sample; // Returns a colour between red and green
Using Perceptually Uniform Interpolation
use ;
// Create colours in Lab space for better interpolation
let yellow = new;
let blue = new;
// Interpolate in Lab colour space (perceptually uniform)
let mixed = lerp;