Farg
A Rust library for colorimetry, color space conversions, and color manipulation.
Farg provides context-aware color conversions with f64 precision, spectral data processing, and chromatic adaptation. It's designed to serve web developers with sensible defaults while giving colorimetrists full control over illuminants, observers, and adaptation transforms.
Features
- 40+ RGB color spaces — sRGB, Display P3, Adobe RGB, Rec. 2020, ACES, and many more
- Cylindrical color spaces — HSL, HSV/HSB, HWB
- Subtractive color spaces — CMY, CMYK
- CIE XYZ and LMS — Device-independent color spaces as conversion hubs
- Chromatic adaptation — Bradford, CAT02, CAT16, Von Kries, and other transforms
- Spectral data — SPD and color matching function support with wavelength-level access
- Standard illuminants — D65, D50, A, E, fluorescent, LED, and others
- Standard observers — CIE 1931 2°, CIE 1964 10°, CIE 2006, Stockman-Sharpe
- Chromaticity coordinates — CIE xy, uv, u'v', and rg systems
- Feature-gated — Include only what you need for minimal binary size
- f64 precision — All internal calculations use 64-bit floating point
Installation
Add to your Cargo.toml:
[]
= "0.2"
By default, farg includes the Bradford chromatic adaptation transform and the D65 illuminant with CIE 1931 2° observer. Enable additional features as needed:
[]
= { = "0.2", = ["all-rgb-spaces", "all-illuminants"] }
Or enable everything:
[]
= { = "0.2", = ["full"] }
Quick Start
Create and convert colors
use ;
// Create an sRGB color from 8-bit values
let color = new;
// Convert to CIE XYZ
let xyz: Xyz = color.to_xyz;
// Access components
let = xyz.components;
Parse hex codes
use Srgb;
let color = from_hexcode.unwrap;
let also_red = from_hexcode.unwrap;
Work with chromaticity
use Xy;
use Xyz;
// Extract chromaticity from a color
let xyz = new;
let xy: Xy = xyz.chromaticity;
// Reconstruct XYZ from chromaticity + luminance
let reconstructed = xy.to_xyz;
Convert to cylindrical and subtractive spaces
use ;
let color = new;
// Convert to cylindrical spaces
let hsl: = color.to_hsl;
let hsv: = color.to_hsv;
let hwb: = color.to_hwb;
// Convert to subtractive spaces
let cmy: = color.to_cmy;
let cmyk: = color.to_cmyk;
Chromatic adaptation
use ;
use Xyz;
// Adapt a color from D65 to D50 illuminant
let d50_context = new
.with_illuminant
.with_cat;
let color = new;
let adapted = color.adapt_to;
Spectral data
use ;
use Table;
// Access illuminant spectral power distribution
let d65 = D65;
let spd = d65.spd;
let power_at_550nm = spd.at;
// Use observer color matching functions
let observer = CIE_1931_2D;
let cmf = observer.cmf;
let xyz = cmf.spectral_power_distribution_to_xyz;
Feature Flags
Farg uses granular feature flags so you only compile what you need.
Meta Features
| Feature | Contents |
|---|---|
full |
Everything below |
all-cats |
All chromatic adaptation transforms |
all-chromaticity |
All chromaticity coordinate systems |
all-illuminants |
All standard illuminants |
all-observers |
All standard observers |
all-spaces |
All color spaces (RGB, cylindrical, subtractive) |
all-rgb-spaces |
All RGB color spaces |
Individual Features
| Prefix | Examples |
|---|---|
cat-* |
cat-bradford (default), cat-cat02, cat-cat16, cat-von-kries |
chromaticity-* |
chromaticity-rg, chromaticity-uv, chromaticity-upvp |
illuminant-* |
illuminant-d50, illuminant-daylight, illuminant-led |
observer-* |
observer-cie-1964-10d, observer-cie-2006-2d |
rgb-* |
rgb-display-p3, rgb-adobe-rgb, rgb-rec-2020, rgb-aces-cg |
space-* |
space-hsl, space-hsv, space-hwb, space-cmy, space-cmyk |
The D65 illuminant, CIE 1931 2° observer, sRGB, and XYZ/LMS spaces are always available.
Documentation
License
Licensed under the MIT License. See LICENSE for details.