| 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 |
fn to_xyy(self) -> XyY { |
| 46 |
self |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl XyY { |
| 51 |
#[inline] |
| 52 |
pub const fn new(x: f64, y: f64, yb: f64) -> Self { |
| 53 |
Self { x, y, yb } |
| 54 |
} |
| 55 |
|
| 56 |
#[inline] |
| 57 |
pub const fn to_xyz(self) -> Xyz { |
| 58 |
let reciprocal = if self.y != 0. { |
| 59 |
1. / self.y * self.yb |
| 60 |
} else { |
| 61 |
0. |
| 62 |
}; |
| 63 |
Xyz { |
| 64 |
x: (self.x * reciprocal) as f32, |
| 65 |
y: self.yb as f32, |
| 66 |
z: ((1. - self.x - self.y) * reciprocal) as f32, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
#[inline] |
| 71 |
pub const fn to_xyzd(self) -> Xyzd { |
| 72 |
let reciprocal = if self.y != 0. { |
| 73 |
1. / self.y * self.yb |
| 74 |
} else { |
| 75 |
0. |
| 76 |
}; |
| 77 |
Xyzd { |
| 78 |
x: self.x * reciprocal, |
| 79 |
y: self.yb, |
| 80 |
z: (1. - self.x - self.y) * reciprocal, |
| 81 |
} |
| 82 |
} |
| 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 |
} |