/rust/registry/src/index.crates.io-1949cf8c6b5b557f/moxcms-0.7.11/src/srlab2.rs
Line | Count | Source |
1 | | /* |
2 | | * // Copyright (c) Radzivon Bartoshyk 6/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; |
30 | | use crate::mlaf::mlaf; |
31 | | use pxfm::f_cbrtf; |
32 | | |
33 | | #[inline] |
34 | 0 | fn srlab2_gamma(x: f32) -> f32 { |
35 | 0 | if x <= 216. / 24389. { |
36 | 0 | x * (24389. / 2700.) |
37 | | } else { |
38 | 0 | 1.16 * f_cbrtf(x) - 0.16 |
39 | | } |
40 | 0 | } |
41 | | |
42 | | #[inline] |
43 | 0 | fn srlab2_linearize(x: f32) -> f32 { |
44 | 0 | if x <= 0.08 { |
45 | 0 | x * (2700.0 / 24389.0) |
46 | | } else { |
47 | 0 | let zx = (x + 0.16) / 1.16; |
48 | 0 | zx * zx * zx |
49 | | } |
50 | 0 | } |
51 | | |
52 | | #[derive(Copy, Clone, Debug, Default, PartialOrd, PartialEq)] |
53 | | pub struct Srlab2 { |
54 | | pub l: f32, |
55 | | pub a: f32, |
56 | | pub b: f32, |
57 | | } |
58 | | |
59 | | impl Srlab2 { |
60 | | #[inline] |
61 | 0 | pub const fn new(l: f32, a: f32, b: f32) -> Srlab2 { |
62 | 0 | Srlab2 { l, a, b } |
63 | 0 | } |
64 | | |
65 | | #[inline] |
66 | 0 | pub fn from_xyz(xyz: Xyz) -> Srlab2 { |
67 | 0 | let lx = srlab2_gamma(xyz.x); |
68 | 0 | let ly = srlab2_gamma(xyz.y); |
69 | 0 | let lz = srlab2_gamma(xyz.z); |
70 | | |
71 | 0 | let l = mlaf(mlaf(0.629054 * ly, -0.000008, lz), 0.37095, lx); |
72 | 0 | let a = mlaf(mlaf(6.634684 * lx, -7.505078, ly), 0.870328, lz); |
73 | 0 | let b = mlaf(mlaf(0.639569 * lx, 1.084576, ly), -1.724152, lz); |
74 | 0 | Srlab2 { l, a, b } |
75 | 0 | } |
76 | | |
77 | | #[inline] |
78 | 0 | pub fn to_xyz(&self) -> Xyz { |
79 | 0 | let x = mlaf(mlaf(self.l, 0.09041272, self.a), 0.045634452, self.b); |
80 | 0 | let y = mlaf(mlaf(self.l, -0.05331593, self.a), -0.026917785, self.b); |
81 | 0 | let z = mlaf(self.l, -0.58, self.b); |
82 | 0 | let lx = srlab2_linearize(x); |
83 | 0 | let ly = srlab2_linearize(y); |
84 | 0 | let lz = srlab2_linearize(z); |
85 | 0 | Xyz::new(lx, ly, lz) |
86 | 0 | } |
87 | | } |
88 | | |
89 | | #[cfg(test)] |
90 | | mod tests { |
91 | | use super::*; |
92 | | |
93 | | #[test] |
94 | | fn test_srlab2() { |
95 | | let xyz = Xyz::new(0.3, 0.65, 0.66); |
96 | | let srlab2 = Srlab2::from_xyz(xyz); |
97 | | let r_xyz = srlab2.to_xyz(); |
98 | | assert!((r_xyz.x - xyz.x).abs() < 1e-5); |
99 | | assert!((r_xyz.y - xyz.y).abs() < 1e-5); |
100 | | assert!((r_xyz.z - xyz.z).abs() < 1e-5); |
101 | | } |
102 | | } |