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:
- An
Illuminant— the light source (e.g., D65 daylight, D50 for print) - An
Observer— the human visual system model (e.g., CIE 1931 2°) - A
ChromaticAdaptationTransform— how the eye adjusts to different lighting
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 spacesLms— 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.
| Feature | Contents |
|---|---|
full | Everything |
all-cats | All chromatic adaptation transforms |
all-chromaticity | All chromaticity coordinate systems |
all-illuminants | All standard illuminants |
all-observers | All standard observers |
all-rgb-spaces | All RGB color spaces |
Modules§
- chromaticity
- color_
vision_ deficiency - Color Vision Deficiency (CVD) simulation.
- contrast
- correlated_
color_ temperature - Correlated Color Temperature (CCT) estimation.
- distance
- space
Structs§
- Chromatic
Adaptation Transform - A 3x3 matrix transform for adapting colors between different illuminant white points.
- Chromaticity
Coordinates - Spectral locus chromaticity coordinates derived from color matching functions.
- Color
Matching Function - CIE color matching functions mapping wavelengths to XYZ tristimulus responses.
- Colorimetric
Context - Defines the viewing conditions for colorimetric calculations.
- Cone
Fundamentals - Spectral cone sensitivity functions mapping wavelengths to LMS cone responses.
- Cone
Response - An LMS cone response at a single wavelength.
- Fairchild
Modifier - Modifier for deriving new
Observerinstances from existing ones. - Illuminant
- A standard or custom illuminant (light source) defined by its spectral power distribution.
- Illuminant
Builder - Builder for constructing custom
Illuminantinstances. - Observer
- A standard or custom observer defined by color matching functions.
- Observer
Builder - Builder for constructing custom
Observerinstances. - Spectral
Power Distribution - Spectral power distribution — the power of a light source at each wavelength.
- Tristimulus
Response - An XYZ tristimulus response at a single wavelength.
Enums§
- Error
- Errors that can occur during color operations.
- Illuminant
Type - The category of an illuminant.
Traits§
- Spectral
Table - Common interface for wavelength-indexed spectral data.
Type Aliases§
- Cat
- Shorthand alias for
ChromaticAdaptationTransform. - Cmf
- Shorthand alias for
ColorMatchingFunction. - Spd
- Shorthand alias for
SpectralPowerDistribution.