Skip to main content

Crate farg

Crate farg 

Source
Expand description

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

Create colors, convert between spaces, and manipulate components:

use farg::space::{ColorSpace, Rgb, Srgb, Xyz};

// Create an sRGB color from 8-bit values
let color = Rgb::<Srgb>::new(255, 87, 51);

// Convert to CIE XYZ
let xyz: Xyz = color.to_xyz();
let [x, y, z] = xyz.components();

// Adjust luminance while preserving chromaticity
let brighter = xyz.with_luminance_scaled_by(1.2);

§Architecture

Colors in farg exist within a viewing context consisting of:

By default, colors use the D65 illuminant with the CIE 1931 2° observer and the Bradford chromatic adaptation transform. This matches the standard sRGB viewing environment.

Conversions flow through hub color spaces to minimize implementation complexity:

  • Xyz — universal hub for device-independent spaces
  • Lms — cone response space used for chromatic adaptation
  • Linear RGB — intermediate hub for the RGB family (gamma decode → linear → XYZ)

§Color Spaces

The space module contains all color space types and the ColorSpace trait. Xyz, Lms, and Srgb are always available. Additional RGB spaces are enabled through feature flags:

[dependencies]
farg = { version = "0.1", features = ["rgb-display-p3", "rgb-adobe-rgb"] }

All color spaces implement the ColorSpace trait, providing a common interface for conversions, luminance operations, and component access.

§Chromatic Adaptation

Adapt colors between different illuminants using a ChromaticAdaptationTransform:

use farg::{Cat, ColorimetricContext, Illuminant};
use farg::space::Xyz;

let d50_context = ColorimetricContext::new()
    .with_illuminant(Illuminant::D50)
    .with_cat(Cat::BRADFORD);

let color = Xyz::new(0.95047, 1.0, 1.08883);
let adapted = color.adapt_to(d50_context);

§Spectral Data

Farg includes spectral power distribution (SPD) data for all standard illuminants and color matching function (CMF) data for all standard observers. Access spectral data through the SpectralTable trait:

use farg::{Illuminant, Observer, SpectralTable};

let d65 = Illuminant::D65;
let spd = d65.spd();
let power_at_550nm = spd.at(550);

let observer = Observer::CIE_1931_2D;
let cmf = observer.cmf();
let xyz = cmf.spectral_power_distribution_to_xyz(&spd);

§Chromaticity

The chromaticity module provides coordinate systems for representing color independent of luminance. Xy (CIE 1931) is always available; additional systems like Uv and Upvp are feature-gated.

§Feature Flags

Farg uses granular feature flags so you only compile what you need. The default feature enables cat-bradford. The D65 illuminant, CIE 1931 2° observer, sRGB, and XYZ/LMS spaces are always available regardless of feature selection.

FeatureContents
fullEverything
all-catsAll chromatic adaptation transforms
all-chromaticityAll chromaticity coordinate systems
all-illuminantsAll standard illuminants
all-observersAll standard observers
all-rgb-spacesAll RGB color spaces

Modules§

chromaticity
contrast
correlated_color_temperature
Correlated Color Temperature (CCT) estimation.
distance
space

Structs§

ChromaticAdaptationTransform
A 3x3 matrix transform for adapting colors between different illuminant white points.
ChromaticityCoordinates
Spectral locus chromaticity coordinates derived from color matching functions.
ColorMatchingFunction
CIE color matching functions mapping wavelengths to XYZ tristimulus responses.
ColorimetricContext
Defines the viewing conditions for colorimetric calculations.
ConeFundamentals
Spectral cone sensitivity functions mapping wavelengths to LMS cone responses.
ConeResponse
An LMS cone response at a single wavelength.
FairchildModifier
Modifier for deriving new Observer instances from existing ones.
Illuminant
A standard or custom illuminant (light source) defined by its spectral power distribution.
IlluminantBuilder
Builder for constructing custom Illuminant instances.
Observer
A standard or custom observer defined by color matching functions.
ObserverBuilder
Builder for constructing custom Observer instances.
SpectralPowerDistribution
Spectral power distribution — the power of a light source at each wavelength.
TristimulusResponse
An XYZ tristimulus response at a single wavelength.

Enums§

Error
Errors that can occur during color operations.
IlluminantType
The category of an illuminant.

Traits§

SpectralTable
Common interface for wavelength-indexed spectral data.

Type Aliases§

Cat
Shorthand alias for ChromaticAdaptationTransform.
Cmf
Shorthand alias for ColorMatchingFunction.
Spd
Shorthand alias for SpectralPowerDistribution.