colr-types 0.3.1

Color model ZSTs and marker traits for colr.
Documentation
//! Chromatic adaptation transforms XYZ tristimulus values from one
//! illuminant to another.
//!
//! Each adaptation method defines a cone response matrix M. The adaptation
//! matrix for a given illuminant pair is computed at compile time via `colr::operations::adapt::adapt`.
//! Callers pass `WHITE_POINT_XYZ` values from the [`Illuminant`] trait directly
//! to ensure numerical consistency with specification-defined white points.
//!
//! Bradford is mandated by ICC, ACES, and CSS Color Level 4 and is the
//! correct default for all standard RGB space work.
//!
//! | Type        | Specification                                                    |
//! |-------------|------------------------------------------------------------------|
//! | [`Bradford`]| ICC.1:2022, Annex E                                              |
//! | [`Cat02`]   | CIE 159:2004                                                     |
//! | [`Cat16`]   | Li et al., Color Research and Application, 2017                  |
//! | [`VonKries`]| Süsstrunk et al., IS&T/SPIE Electronic Imaging, 1999             |
//!
//! [`Illuminant`]: crate::illuminant::Illuminant

/// A chromatic adaptation method defined by its cone response matrix.
///
/// Only `M` is required. The inverse is derived by `colr::operations::adapt::adapt` at compile time
/// via `Mat3::invert`, ensuring consistency and eliminating the possibility
/// of a mismatched inverse.
pub trait ChromaticAdaptation: 'static {
    /// Forward cone response matrix, row-major.
    const M: [[f32; 3]; 3];
}

/// Bradford chromatic adaptation.
///
/// A sharpened von Kries model using a non-diagonal cone response matrix.
/// More accurate than von Kries for large illuminant changes. Mandated by
/// ICC, ACES, and CSS Color Level 4.
///
/// Reference: ICC.1:2022, Annex E.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Bradford;

impl ChromaticAdaptation for Bradford {
    const M: [[f32; 3]; 3] = [
        [0.8951, 0.2664, -0.1614],
        [-0.7502, 1.7135, 0.0367],
        [0.0389, -0.0685, 1.0296],
    ];
}

/// CAT02 chromatic adaptation.
///
/// Used in CIECAM02 and ICC v4 appearance models. Provides better hue
/// constancy than Bradford in some gamut mapping contexts.
///
/// Reference: CIE 159:2004.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Cat02;

impl ChromaticAdaptation for Cat02 {
    const M: [[f32; 3]; 3] = [
        [0.7328, 0.4296, -0.1624],
        [-0.7036, 1.6975, 0.0061],
        [0.0030, 0.0136, 0.9834],
    ];
}

/// CAT16 chromatic adaptation.
///
/// Used in CAM16. Successor to CAT02 with improved accuracy for large
/// chromatic differences and reduced numerical instability.
///
/// Reference: Li et al., "Comprehensive colour appearance model (CAM16)",
/// Color Research and Application, 2017.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Cat16;

impl ChromaticAdaptation for Cat16 {
    const M: [[f32; 3]; 3] = [
        [0.401288, 0.650173, -0.051461],
        [-0.250268, 1.204414, 0.045854],
        [-0.002079, 0.048952, 0.953127],
    ];
}

/// Von Kries chromatic adaptation.
///
/// Diagonal-only scaling of cone channels using the Süsstrunk et al. matrix.
/// Less accurate than Bradford or CAT02 for large illuminant changes.
///
/// Reference: Süsstrunk et al., "A Standard Default Color Space for the
/// Internet — sRGB", IS&T/SPIE Electronic Imaging, 1999.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct VonKries;

impl ChromaticAdaptation for VonKries {
    const M: [[f32; 3]; 3] = [
        [0.40024, 0.70760, -0.08081],
        [-0.22630, 1.16532, 0.04570],
        [0.00000, 0.00000, 0.91822],
    ];
}