moxcms/
chromaticity.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 8/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1.  Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3.  Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::{CmsError, XyY, XyYRepresentable, Xyz, Xyzd};
30
31#[derive(Clone, Debug, Copy)]
32#[repr(C)]
33pub struct Chromaticity {
34    pub x: f32,
35    pub y: f32,
36}
37
38impl Chromaticity {
39    #[inline]
40    pub const fn new(x: f32, y: f32) -> Self {
41        Self { x, y }
42    }
43
44    /// Converts this chromaticity (`x`, `y`) to a tristimulus [`Xyz`] value,
45    /// normalized such that `y = 1.0`.
46    #[inline]
47    pub const fn to_xyz(&self) -> Xyz {
48        let reciprocal = if self.y != 0. { 1. / self.y } else { 0. };
49        Xyz {
50            x: self.x * reciprocal,
51            y: 1f32,
52            z: (1f32 - self.x - self.y) * reciprocal,
53        }
54    }
55
56    /// Get the color representation with component sum `1`.
57    ///
58    /// In contrast to the XYZ representation defined through setting `Y` to a known
59    /// value (such as `1` in [`Self::to_xyz`]) this representation can be uniquely
60    /// derived from the `xy` coordinates with no ambiguities. It is scaled from the
61    /// original XYZ color by diving by `X + Y + Z`. Note that, in particular, this
62    /// method is well-defined even if the original color had pure chromamatic
63    /// information with no luminance (Y = `0`) and will preserve that information,
64    /// whereas [`Self::to_xyz`] is ill-defined and returns an incorrect value.
65    #[inline]
66    pub const fn to_scaled_xyzd(&self) -> Xyzd {
67        let z = 1.0 - self.x as f64 - self.y as f64;
68        Xyzd::new(self.x as f64, self.y as f64, z)
69    }
70
71    /// Get the color representation with component sum `1`.
72    ///
73    /// In contrast to the XYZ representation defined through setting `Y` to a known
74    /// value (such as `1` in [`Self::to_xyz`]) this representation can be uniquely
75    /// derived from the `xy` coordinates with no ambiguities. It is scaled from the
76    /// original XYZ color by diving by `X + Y + Z`. Note that, in particular, this
77    /// method is well-defined even if the original color had pure chromamatic
78    /// information with no luminance (Y = `0`) and will preserve that information,
79    /// whereas [`Self::to_xyz`] is ill-defined and returns an incorrect value.
80    #[inline]
81    pub const fn to_scaled_xyz(&self) -> Xyz {
82        let z = 1.0 - self.x - self.y;
83        Xyz::new(self.x, self.y, z)
84    }
85
86    #[inline]
87    pub const fn to_xyzd(&self) -> Xyzd {
88        let reciprocal = if self.y != 0. { 1. / self.y } else { 0. };
89        Xyzd {
90            x: self.x as f64 * reciprocal as f64,
91            y: 1f64,
92            z: (1f64 - self.x as f64 - self.y as f64) * reciprocal as f64,
93        }
94    }
95
96    #[inline]
97    pub const fn to_xyyb(&self) -> XyY {
98        XyY {
99            x: self.x as f64,
100            y: self.y as f64,
101            yb: 1.,
102        }
103    }
104
105    pub const D65: Chromaticity = Chromaticity {
106        x: 0.31272,
107        y: 0.32903,
108    };
109
110    pub const D50: Chromaticity = Chromaticity {
111        x: 0.34567,
112        y: 0.35850,
113    };
114}
115
116impl XyYRepresentable for Chromaticity {
117    fn to_xyy(self) -> XyY {
118        self.to_xyyb()
119    }
120}
121
122impl TryFrom<Xyz> for Chromaticity {
123    type Error = CmsError;
124
125    #[inline]
126    fn try_from(xyz: Xyz) -> Result<Self, Self::Error> {
127        let sum = xyz.x + xyz.y + xyz.z;
128
129        // Avoid division by zero or invalid XYZ values
130        if sum == 0.0 {
131            return Err(CmsError::DivisionByZero);
132        }
133        let rec = 1f32 / sum;
134
135        let chromaticity_x = xyz.x * rec;
136        let chromaticity_y = xyz.y * rec;
137
138        Ok(Chromaticity {
139            x: chromaticity_x,
140            y: chromaticity_y,
141        })
142    }
143}