/rust/registry/src/index.crates.io-1949cf8c6b5b557f/moxcms-0.7.7/src/xyy.rs
Line | Count | Source |
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 | | */ |
29 | | use crate::{Xyz, Xyzd}; |
30 | | |
31 | | /// Holds CIE XyY representation |
32 | | #[derive(Clone, Debug, Copy, Default)] |
33 | | pub struct XyY { |
34 | | pub x: f64, |
35 | | pub y: f64, |
36 | | pub yb: f64, |
37 | | } |
38 | | |
39 | | pub trait XyYRepresentable { |
40 | | fn to_xyy(self) -> XyY; |
41 | | } |
42 | | |
43 | | impl XyYRepresentable for XyY { |
44 | | #[inline] |
45 | 0 | fn to_xyy(self) -> XyY { |
46 | 0 | self |
47 | 0 | } |
48 | | } |
49 | | |
50 | | impl XyY { |
51 | | #[inline] |
52 | 0 | pub const fn new(x: f64, y: f64, yb: f64) -> Self { |
53 | 0 | Self { x, y, yb } |
54 | 0 | } |
55 | | |
56 | | #[inline] |
57 | 0 | pub const fn to_xyz(self) -> Xyz { |
58 | 0 | let reciprocal = if self.y != 0. { |
59 | 0 | 1. / self.y * self.yb |
60 | | } else { |
61 | 0 | 0. |
62 | | }; |
63 | 0 | Xyz { |
64 | 0 | x: (self.x * reciprocal) as f32, |
65 | 0 | y: self.yb as f32, |
66 | 0 | z: ((1. - self.x - self.y) * reciprocal) as f32, |
67 | 0 | } |
68 | 0 | } Unexecuted instantiation: <moxcms::xyy::XyY>::to_xyz Unexecuted instantiation: <moxcms::xyy::XyY>::to_xyz |
69 | | |
70 | | #[inline] |
71 | 0 | pub const fn to_xyzd(self) -> Xyzd { |
72 | 0 | let reciprocal = if self.y != 0. { |
73 | 0 | 1. / self.y * self.yb |
74 | | } else { |
75 | 0 | 0. |
76 | | }; |
77 | 0 | Xyzd { |
78 | 0 | x: self.x * reciprocal, |
79 | 0 | y: self.yb, |
80 | 0 | z: (1. - self.x - self.y) * reciprocal, |
81 | 0 | } |
82 | 0 | } |
83 | | } |
84 | | |
85 | | #[cfg(test)] |
86 | | mod tests { |
87 | | use super::*; |
88 | | |
89 | | #[test] |
90 | | fn test_xyzd_xyy() { |
91 | | let xyy = XyY::new(0.2, 0.4, 0.5); |
92 | | let xyy = xyy.to_xyzd(); |
93 | | let r_xyy = xyy.to_xyzd(); |
94 | | assert!((r_xyy.x - xyy.x).abs() < 1e-5); |
95 | | assert!((r_xyy.y - xyy.y).abs() < 1e-5); |
96 | | assert!((r_xyy.z - xyy.z).abs() < 1e-5); |
97 | | } |
98 | | } |