use crate::illuminant::{AcesWhitePoint, D50, D65, DciWhite, Illuminant};
use crate::math::Mat3;
use crate::observer::Cie1931;
pub const fn derive_rgb_to_xyz(r: [f32; 2], g: [f32; 2], b: [f32; 2], w_xyz: [f32; 3]) -> Mat3 {
let [xr, yr] = r;
let zr = 1.0 - xr - yr;
let [xg, yg] = g;
let zg = 1.0 - xg - yg;
let [xb, yb] = b;
let zb = 1.0 - xb - yb;
let det = xr * (yg * zb - yb * zg) - xg * (yr * zb - yb * zr) + xb * (yr * zg - yg * zr);
let inv00 = (yg * zb - yb * zg) / det;
let inv01 = (xb * zg - xg * zb) / det;
let inv02 = (xg * yb - xb * yg) / det;
let inv10 = (yb * zr - yr * zb) / det;
let inv11 = (xr * zb - xb * zr) / det;
let inv12 = (xb * yr - xr * yb) / det;
let inv20 = (yr * zg - yg * zr) / det;
let inv21 = (xg * zr - xr * zg) / det;
let inv22 = (xr * yg - xg * yr) / det;
let [xw, yw, zw] = w_xyz;
let sr = inv00 * xw + inv01 * yw + inv02 * zw;
let sg = inv10 * xw + inv11 * yw + inv12 * zw;
let sb = inv20 * xw + inv21 * yw + inv22 * zw;
Mat3 {
col0: [sr * xr, sr * yr, sr * zr, 0.0],
col1: [sg * xg, sg * yg, sg * zg, 0.0],
col2: [sb * xb, sb * yb, sb * zb, 0.0],
}
}
pub trait Primaries: 'static {
type Native: Illuminant<Observer = Cie1931>;
const R: [f32; 2];
const G: [f32; 2];
const B: [f32; 2];
const TO_XYZ_NATIVE: Mat3;
const FROM_XYZ_NATIVE: Mat3;
const LUMA_WEIGHTS: [f32; 3] = [
Self::TO_XYZ_NATIVE.col0[1],
Self::TO_XYZ_NATIVE.col1[1],
Self::TO_XYZ_NATIVE.col2[1],
];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Srgb;
const SRGB_TO_XYZ: Mat3 = derive_rgb_to_xyz(
[0.6400, 0.3300],
[0.3000, 0.6000],
[0.1500, 0.0600],
<D65<Cie1931> as Illuminant>::WHITE_POINT_XYZ,
);
const SRGB_FROM_XYZ: Mat3 = Mat3::invert(&SRGB_TO_XYZ);
impl Primaries for Srgb {
type Native = D65;
const R: [f32; 2] = [0.6400, 0.3300];
const G: [f32; 2] = [0.3000, 0.6000];
const B: [f32; 2] = [0.1500, 0.0600];
const TO_XYZ_NATIVE: Mat3 = SRGB_TO_XYZ;
const FROM_XYZ_NATIVE: Mat3 = SRGB_FROM_XYZ;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct P3;
const P3_TO_XYZ: Mat3 = derive_rgb_to_xyz(
[0.6800, 0.3200],
[0.2650, 0.6900],
[0.1500, 0.0600],
<D65<Cie1931> as Illuminant>::WHITE_POINT_XYZ,
);
const P3_FROM_XYZ: Mat3 = Mat3::invert(&P3_TO_XYZ);
impl Primaries for P3 {
type Native = D65;
const R: [f32; 2] = [0.6800, 0.3200];
const G: [f32; 2] = [0.2650, 0.6900];
const B: [f32; 2] = [0.1500, 0.0600];
const TO_XYZ_NATIVE: Mat3 = P3_TO_XYZ;
const FROM_XYZ_NATIVE: Mat3 = P3_FROM_XYZ;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Rec2020;
const REC2020_TO_XYZ: Mat3 = derive_rgb_to_xyz(
[0.7080, 0.2920],
[0.1700, 0.7970],
[0.1310, 0.0460],
<D65<Cie1931> as Illuminant>::WHITE_POINT_XYZ,
);
const REC2020_FROM_XYZ: Mat3 = Mat3::invert(&REC2020_TO_XYZ);
impl Primaries for Rec2020 {
type Native = D65;
const R: [f32; 2] = [0.7080, 0.2920];
const G: [f32; 2] = [0.1700, 0.7970];
const B: [f32; 2] = [0.1310, 0.0460];
const TO_XYZ_NATIVE: Mat3 = REC2020_TO_XYZ;
const FROM_XYZ_NATIVE: Mat3 = REC2020_FROM_XYZ;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AcesAp0;
const AP0_TO_XYZ: Mat3 = derive_rgb_to_xyz(
[0.73470, 0.26530],
[0.00000, 1.00000],
[0.00010, -0.07700],
AcesWhitePoint::WHITE_POINT_XYZ,
);
const AP0_FROM_XYZ: Mat3 = Mat3::invert(&AP0_TO_XYZ);
impl Primaries for AcesAp0 {
type Native = AcesWhitePoint;
const R: [f32; 2] = [0.73470, 0.26530];
const G: [f32; 2] = [0.00000, 1.00000];
const B: [f32; 2] = [0.00010, -0.07700];
const TO_XYZ_NATIVE: Mat3 = AP0_TO_XYZ;
const FROM_XYZ_NATIVE: Mat3 = AP0_FROM_XYZ;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AcesAp1;
const AP1_TO_XYZ: Mat3 = derive_rgb_to_xyz(
[0.71300, 0.29300],
[0.16500, 0.83000],
[0.12800, 0.04400],
AcesWhitePoint::WHITE_POINT_XYZ,
);
const AP1_FROM_XYZ: Mat3 = Mat3::invert(&AP1_TO_XYZ);
impl Primaries for AcesAp1 {
type Native = AcesWhitePoint;
const R: [f32; 2] = [0.71300, 0.29300];
const G: [f32; 2] = [0.16500, 0.83000];
const B: [f32; 2] = [0.12800, 0.04400];
const TO_XYZ_NATIVE: Mat3 = AP1_TO_XYZ;
const FROM_XYZ_NATIVE: Mat3 = AP1_FROM_XYZ;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ProPhoto;
const PRO_PHOTO_TO_XYZ: Mat3 = derive_rgb_to_xyz(
[0.7347, 0.2653],
[0.1596, 0.8404],
[0.0366, 0.0001],
<D50<Cie1931> as Illuminant>::WHITE_POINT_XYZ,
);
const PRO_PHOTO_FROM_XYZ: Mat3 = Mat3::invert(&PRO_PHOTO_TO_XYZ);
impl Primaries for ProPhoto {
type Native = D50;
const R: [f32; 2] = [0.7347, 0.2653];
const G: [f32; 2] = [0.1596, 0.8404];
const B: [f32; 2] = [0.0366, 0.0001];
const TO_XYZ_NATIVE: Mat3 = PRO_PHOTO_TO_XYZ;
const FROM_XYZ_NATIVE: Mat3 = PRO_PHOTO_FROM_XYZ;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct DciP3;
const DCI_P3_TO_XYZ: Mat3 = derive_rgb_to_xyz(
[0.6800, 0.3200],
[0.2650, 0.6900],
[0.1500, 0.0600],
DciWhite::WHITE_POINT_XYZ,
);
const DCI_P3_FROM_XYZ: Mat3 = Mat3::invert(&DCI_P3_TO_XYZ);
impl Primaries for DciP3 {
type Native = DciWhite;
const R: [f32; 2] = [0.6800, 0.3200];
const G: [f32; 2] = [0.2650, 0.6900];
const B: [f32; 2] = [0.1500, 0.0600];
const TO_XYZ_NATIVE: Mat3 = DCI_P3_TO_XYZ;
const FROM_XYZ_NATIVE: Mat3 = DCI_P3_FROM_XYZ;
}