[][src]Crate colorspace

Build Status


A crate for colorimetry in Rust. This crate contains types and functions for working with color. The intended use is to support rendering applications (I use it to manage color in a spectral pathtracer), but if you want to be able to convert between spectral, XYZ, L'a'b' and RGB spaces of various flavors such as sRGB, ACES, DCI P3 and ALEXA Wide Gamut then this is the crate for you.

This crate is still WIP but is fairly stable. Some minor refactoring may occur, but the core types are complete and stable. Future development will be mostly about adding functionality and SIMD-ifying as many operations as possible. I also intend to integrate OCIO at some point.



The library contains two main types for working with color values. XYZf and RGBf can be either single- or double-precision and have all the expected mathematical operations defined.

The RGBu8 and RGBu16 types are for storage only (for writing to images or passing to e.g. OpenGL for display) and do not define any operations. To do maths with them you must convert to RGBf first

Spectral Power Distribution

SPDs are defined as a pair of Vecs of wavelengths and associated values. The library supplies spectral data for CIE illuminants in the illuminant module, and for the color checker chart in the color_checker module.


Convert a slice of 32-bit sRGB colors to 8-bit DCI-P3

use colorspace::*;
let srgb = &color_space_rgb::model_f32::SRGB;
let dci_p3 = &color_space_rgb::model_f32::DCI_P3;
let mut px_dci_p3_u8 = vec![rgbu8(0, 0, 0); px_srgb.len()];
rgb_to_rgb(srgb, dci_p3, &px_srgb, &mut px_dci_p3_u8);

Spectral to 8-bit, gamma-encoded sRGB conversion

use colorspace::*;

// Convert the spectral data for a measured MacBeth chart swatch to XYZ
// using the CIE 1931 2-degree CMFs and a D65 illuminant
let xyz = colorchecker::SPECTRAL["dark_skin"]
.to_xyz(&illuminant::spd::D65, &cmf::CIE_1931_2_DEGREE);

// Convert the XYZ value to scene-referred (i.e. linear) sRGB by first creating
// the conversion matrix and then applying it
let model_srgb = &color_space_rgb::model_f64::SRGB;
let xf_xyz_to_srgb = xyz_to_rgb_matrix(model_srgb.white, model_srgb);
let rgb = xyz_to_rgb(&xf_xyz_to_srgb, xyz);

// Convert the scene-referred sRGB value to an 8-bit, display-referred
// value by applying the opto-electrical transfer function and using RGBu8's
// From<RGBf32> impl
let rgb: RGBu8 = model_srgb.encode(rgb).into();

assert_eq!(rgb, rgbu8(115, 82, 68));

# Licence
Copyright [2018-2020] [Anders Langlands]
colorspace is licensed under Apache License, Version 2.0

This crate contains some data taken from the excellent colour-science python
library by Mansencal et al.: <https://www.colour-science.org>
Copyright (c) 2013-2018, Colour Developers

Most of the conversion algorithms are based on those published at
Bruce Lindbloom's site: <http://www.brucelindbloom.com>
Copyright © 2001 - 2018 Bruce Justin Lindbloom.

BabelColor color-checker data is copyright © 2004‐2012 Danny Pascale (www.babelcolor.com); used with permission.

Contains data from https://github.com/imallett/simple-spectral accompanying the EGSR 2019 paper "Mallett & Yuksel - Spectral Primary Decomposition for Rendering with sRGB Reflectance".


pub use cmf::CMF;
pub use interpolation::InterpolatorLinear;
pub use interpolation::InterpolatorSprague;
pub use xyz::XYZf32;
pub use xyz::XYZf64;
pub use rgb::rgbf32;
pub use rgb::rgbf64;
pub use rgb::rgbu16;
pub use rgb::rgbu8;
pub use rgb::RGBAf32;
pub use rgb::RGBf32;
pub use rgb::RGBf64;
pub use rgb::RGBu16;
pub use rgb::RGBu8;
pub use math::M3f32;
pub use math::M3f64;
pub use math::Matrix33;
pub use color_space_rgb::decode;
pub use color_space_rgb::encode;
pub use color_space_rgb::model_f64::*;
pub use color_space_rgb::ColorSpaceRGB;
pub use chromaticity::*;
pub use vspd::SpdElement;
pub use vspd::SpdShape;
pub use vspd::VSPD;
pub use spd::SPD;
pub use transform::*;
pub use lab::delta_E_2000 as delta_E;
pub use lab::lab;
pub use lab::xyz_to_lab;
pub use lab::Lab;
pub use photometry::spd_to_lumens;



xyY coordinates and conversion to and from XYZ


Tabulated data for Color Matching Functions.


Defining RGB color spaces from primaries, whitepoint and OETF


Lab color space and difference calculations.


RGB color types


XYZ color type