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.
Quick Start
[]
= "0.4"
use ;
// Create an sRGB color from 8-bit values or hex
let coral = new;
let coral = try_from.unwrap;
// Convert to CIE XYZ and read components
let xyz: Xyz = coral.to_xyz;
let = xyz.components;
// Adjust luminance while preserving chromaticity
let brighter = xyz.with_luminance_scaled_by;
// Convert back to sRGB
let result: = brighter.to_rgb;
Color Spaces
Farg supports 50+ color spaces organized by family. Xyz, Lms, and Rgb<Srgb> are always available; all others
are enabled through feature flags.
| Family | Spaces | Feature Flags |
|---|---|---|
| CIE | XYZ, Lab, LCh, LCh(uv), Luv, xyY | space-lab, space-lch, space-lchuv, space-luv, space-xyy |
| Perceptual (Oklab) | Oklab, Oklch, Okhsl, Okhsv, Okhwb | space-oklab, space-oklch, space-okhsl, space-okhsv, space-okhwb |
| Perceptual (Luv) | HSLuv, HPLuv | space-hsluv, space-hpluv |
| Cylindrical | HSL, HSV/HSB, HWB, HSI | space-hsl, space-hsv, space-hwb, space-hsi |
| Subtractive | CMY, CMYK | space-cmy, space-cmyk |
| Physiological | LMS | (always available) |
| RGB | sRGB + 37 additional spaces | all-rgb-spaces or individual rgb-* flags |
RGB spaces include Display P3, Adobe RGB, Rec. 2020, Rec. 2100, ACES, ARRI Wide Gamut, and many more for display, broadcast, cinema, and legacy workflows.
Context-Aware Conversions
Colors exist within a viewing context: an illuminant, observer, and chromatic adaptation transform. The default context (D65, CIE 1931 2°, Bradford) matches the standard sRGB environment.
use ;
use Xyz;
// Adapt a D65 color to D50 (e.g., for print workflows)
let d50 = new
.with_illuminant
.with_cat;
let color = new;
let adapted = color.adapt_to;
Universal Property Access
Every color space implements the ColorSpace trait, providing access to any color property through automatic
conversion:
use ;
let color = new;
// Access properties from any color model
let luminance = color.luminance; // CIE Y
let chromaticity = color.chromaticity; // CIE xy
let hue = color.hue; // perceptual hue
let chroma = color.chroma; // perceptual chroma
// Mutate through any property
let shifted = color.with_hue;
let desaturated = color.with_chroma_scaled_by;
Alpha & Compositing
All color spaces support an alpha channel with a full mutation API:
use ;
let overlay = new.with_alpha;
let background = new;
// Flatten against a background
let composited = overlay.flatten_alpha_against;
Gamut Mapping
Four strategies for mapping out-of-gamut colors back into a target RGB gamut:
use ;
let wide = new;
let clipped: = wide.clip_to_gamut; // clamp to [0, 1]
let scaled: = wide.scale_to_gamut; // linear scaling
let perceptual: = wide.perceptually_map_to_gamut; // LMS scaling
Color Distance
Six algorithms for measuring perceptual or geometric distance between colors:
use ciede2000;
use ;
let coral = new;
let teal = new;
// CIEDE2000 color difference (ΔE*00)
let delta_e = calculate;
// Perceptual equivalence check (ΔE*00 < 1.0)
let same = coral.is_perceptually_equivalent;
// Find the closest match from a palette
let palette = ;
let closest = coral.closest_match;
Additional algorithms (CIE76, CIE94, CMC l:c, Euclidean, Manhattan) are available behind distance-* feature flags.
Contrast
Six algorithms for evaluating perceptual contrast between colors:
use ;
let white = new;
let text = new;
// WCAG 2.x contrast ratio with threshold checking
let ratio = white.contrast_ratio;
// APCA lightness contrast
let lc = white.lightness_contrast;
Additional algorithms (Michelson, Weber, RMS, AERT) are available behind contrast-* feature flags.
Color Harmonies
Generate harmonious color palettes using hue rotation and luminance scaling:
use ;
let coral = new;
// Hue-based harmonies
let complement = coral.complementary; // opposite on the color wheel
let = coral.analogous; // ±30° neighbors
let = coral.split_complementary; // flanking the complement
let = coral.triadic; // 120° intervals
let = coral.tetradic; // 90° intervals
// Luminance-based harmony (always available)
let = coral.monochromatic;
Hue-based methods require any cylindrical or perceptual color space feature (space-oklch, space-hsl, etc.).
Color Mixing & Gradients
Three interpolation strategies for mixing colors and generating gradients:
use ;
let coral = new;
let teal = new;
// Perceptually uniform mixing (Oklch/LCh)
let midpoint = coral.mix;
// Physically correct additive light mixing (linear sRGB)
let blended = coral.mix_linear;
// Generate a 5-step gradient
let gradient = coral.gradient;
Cylindrical and rectangular mixing variants are feature-gated behind their respective color spaces.
CSS Serialization
Convert any color to CSS Color Level 4 strings or hex notation:
use ;
let coral = new;
assert_eq!;
assert_eq!;
let oklch = new;
assert_eq!;
Types with a native CSS representation (Rgb<Srgb>, Rgb<DisplayP3>, Lab, Lch, Oklab, Oklch, Hsl<Srgb>,
Hwb<Srgb>, and others) output their space-specific format. All other color spaces fall back to sRGB rgb(...).
Serde Serialization
Enable the serde feature to serialize and deserialize all color spaces with any Serde-compatible format:
[]
= { = "0.4", = ["serde", "space-oklab"] }
use ;
let coral = new;
let json = to_string.unwrap;
let restored: = from_str.unwrap;
Alpha is omitted when opaque (1.0) and defaults to 1.0 when absent during deserialization.
Color Vision Deficiency Simulation
Three algorithms for simulating how colors appear to individuals with color blindness:
use ;
use ;
let coral = new;
// Dichromacy — complete cone loss (Brettel 1997)
let protanopia = protanopia;
let deuteranopia = deuteranopia;
// Anomalous trichromacy — reduced sensitivity (Machado 2009)
let mild_deutan = deuteranomaly;
let severe_deutan = deuteranomaly;
Brettel (most accurate dichromacy) and Machado (severity-parameterized anomalous trichromacy) are enabled by
default. Viénot is available as a faster dichromacy alternative behind cvd-vienot.
Correlated Color Temperature
Four algorithms for estimating the color temperature of a light source:
use ohno;
use Xyz;
// D65 white point (~6504 K)
let d65 = new;
let cct = calculate;
println!;
Additional algorithms (Robertson, Hernandez-Andres, McCamy) are available behind cct-* feature flags.
Spectral Data
Full spectral power distribution and color matching function data for all standard illuminants and observers:
use ;
let d65 = D65;
let spd = d65.spd;
let power_at_550nm = spd.at;
let observer = CIE_1931_2D;
let cmf = observer.cmf;
let xyz = cmf.spectral_power_distribution_to_xyz;
Build custom illuminants and observers from your own spectral data:
use ;
Feature Flags
Farg uses granular feature flags so you only compile what you need. The default feature enables Bradford CAT,
WCAG contrast, APCA contrast, CIEDE2000 color distance, and Brettel + Machado CVD simulation. D65, CIE 1931 2°,
sRGB, XYZ, and LMS are always available.
| Feature | Contents |
|---|---|
full |
Everything below |
all-cats |
All 9 chromatic adaptation transforms |
all-cct |
All 4 correlated color temperature algorithms |
all-chromaticity |
All chromaticity coordinate systems (Rg, Uv, u'v') |
all-contrast |
All 6 contrast algorithms |
all-cvd |
All 3 color vision deficiency simulation algorithms |
all-distance |
All 6 color distance algorithms |
all-illuminants |
All standard illuminants (44 total across daylight, fluorescent, LED, and more) |
all-observers |
All 7 additional observers (CIE 1964 10°, CIE 2006, Stockman-Sharpe, Judd, Judd-Vos) |
all-rgb-spaces |
All 37 additional RGB color spaces |
all-spaces |
All color spaces (CIE, cylindrical, perceptual, subtractive, and all RGB) |
serde |
Serialize and deserialize all color spaces via Serde |
Individual features follow the pattern {category}-{name}, e.g., space-oklab, cat-bradford, illuminant-d50,
rgb-display-p3. See the Feature Flags Reference for every flag with descriptions and
dependencies.
Enable everything:
[]
= { = "0.4", = ["full"] }
Or pick what you need:
[]
= { = "0.4", = ["space-oklab", "space-lab", "all-illuminants"] }
Documentation
- API Documentation
- Usage Guide
- Chromatic Adaptation -- CATs for adapting colors between illuminants
- Color Conversions -- Converting between color spaces
- Color Distance -- Measuring distance between colors
- Color Harmonies -- Generating harmonious color palettes
- Color Vision Deficiency -- Simulating color blindness
- Color Mixing -- Interpolating colors and generating gradients
- Contrast -- Measuring contrast between colors
- Correlated Color Temperature -- Estimating CCT
- Illuminants -- Standard, custom, and contextual illuminants
- Observers -- Standard, custom, and modified observers
- Feature Flags -- All feature flags, dependencies, and defaults
- Project Plan
- Contributing Guide
License
Licensed under the MIT License. See LICENSE for details.