Skip to main content

ph_color_bake/
lib.rs

1//! Host-only baker for `ph-color` matrices and LUTs.
2//!
3//! All inversion, adaptation, and `f64` math live here. The target crate
4//! never depends on this package.
5
6pub mod emit;
7pub mod golden;
8pub mod lut;
9pub mod matrix;
10pub mod oklab;
11pub mod srgb;
12
13/// Host bake failure.
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum BakeError {
16    /// Primaries or white point produced a non-invertible matrix.
17    SingularMatrix,
18}
19
20/// CIE xy in ordinary units (not millionths).
21#[derive(Clone, Copy, Debug, PartialEq)]
22pub struct Xy {
23    /// CIE x.
24    pub x: f64,
25    /// CIE y.
26    pub y: f64,
27}
28
29impl Xy {
30    /// Construct from CIE xy.
31    #[must_use]
32    pub const fn new(x: f64, y: f64) -> Self {
33        Self { x, y }
34    }
35
36    /// Convert millionths used on the target crate into host `f64`.
37    #[must_use]
38    pub fn from_millionths(x_millionths: u32, y_millionths: u32) -> Self {
39        Self {
40            x: f64::from(x_millionths) / 1_000_000.0,
41            y: f64::from(y_millionths) / 1_000_000.0,
42        }
43    }
44}
45
46/// RGB primaries plus white point.
47#[derive(Clone, Copy, Debug, PartialEq)]
48pub struct Primaries {
49    /// Red primary.
50    pub r: Xy,
51    /// Green primary.
52    pub g: Xy,
53    /// Blue primary.
54    pub b: Xy,
55    /// White point.
56    pub white: Xy,
57}
58
59impl Primaries {
60    /// ITU-R BT.709 / sRGB primaries and D65.
61    #[must_use]
62    pub fn srgb() -> Self {
63        Self {
64            r: Xy::new(0.64, 0.33),
65            g: Xy::new(0.30, 0.60),
66            b: Xy::new(0.15, 0.06),
67            white: Xy::new(0.3127, 0.3290),
68        }
69    }
70}