colorutils_rs/
xyb.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * // Copyright 2024 (c) the Radzivon Bartoshyk. All rights reserved.
 * //
 * // Use of this source code is governed by a BSD-style
 * // license that can be found in the LICENSE file.
 */
use crate::utils::mlaf;
use crate::{EuclideanDistance, Rgb, TaxicabDistance, TransferFunction};
use num_traits::Pow;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// XYB is a color space that was designed for use with the JPEG XL Image Coding System.
///
/// It is an LMS-based color model inspired by the human visual system, facilitating perceptually uniform quantization.
/// It uses a gamma of 3 for computationally efficient decoding.
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq)]
pub struct Xyb {
    pub x: f32,
    pub y: f32,
    pub b: f32,
}

impl Xyb {
    #[inline]
    pub fn new(x: f32, y: f32, b: f32) -> Xyb {
        Xyb { x, y, b }
    }

    #[inline]
    /// Converts [Rgb] to [Xyb] using provided [TransferFunction]
    pub fn from_rgb(rgb: Rgb<u8>, transfer_function: TransferFunction) -> Xyb {
        let linear_rgb = rgb.to_linear(transfer_function);
        Self::from_linear_rgb(linear_rgb)
    }

    #[inline]
    /// Converts linear [Rgb] to [Xyb]
    pub fn from_linear_rgb(rgb: Rgb<f32>) -> Xyb {
        const BIAS_CBRT: f32 = 0.155954200549248620f32;
        const BIAS: f32 = 0.00379307325527544933;
        let lgamma = mlaf(
            0.3f32,
            rgb.r,
            mlaf(0.622f32, rgb.g, mlaf(0.078f32, rgb.b, BIAS)),
        )
        .cbrt()
            - BIAS_CBRT;
        let mgamma = mlaf(
            0.23f32,
            rgb.r,
            mlaf(0.692f32, rgb.g, mlaf(0.078f32, rgb.b, BIAS)),
        )
        .cbrt()
            - BIAS_CBRT;
        let sgamma = mlaf(
            0.24342268924547819f32,
            rgb.r,
            mlaf(
                0.20476744424496821f32,
                rgb.g,
                mlaf(0.55180986650955360f32, rgb.b, BIAS),
            ),
        )
        .cbrt()
            - BIAS_CBRT;
        let x = (lgamma - mgamma) * 0.5f32;
        let y = (lgamma + mgamma) * 0.5f32;
        let b = sgamma - mgamma;
        Xyb::new(x, y, b)
    }

    #[inline]
    /// Converts [Xyb] to linear [Rgb]
    pub fn to_linear_rgb(&self) -> Rgb<f32> {
        const BIAS_CBRT: f32 = 0.155954200549248620f32;
        const BIAS: f32 = 0.00379307325527544933;
        let x_lms = (self.x + self.y) + BIAS_CBRT;
        let y_lms = (-self.x + self.y) + BIAS_CBRT;
        let b_lms = (-self.x + self.y + self.b) + BIAS_CBRT;
        let x_c_lms = (x_lms * x_lms * x_lms) - BIAS;
        let y_c_lms = (y_lms * y_lms * y_lms) - BIAS;
        let b_c_lms = (b_lms * b_lms * b_lms) - BIAS;
        let r = mlaf(
            11.031566901960783,
            x_c_lms,
            mlaf(-9.866943921568629, y_c_lms, -0.16462299647058826 * b_c_lms),
        );
        let g = mlaf(
            -3.254147380392157,
            x_c_lms,
            mlaf(4.418770392156863, y_c_lms, -0.16462299647058826 * b_c_lms),
        );
        let b = mlaf(
            -3.6588512862745097,
            x_c_lms,
            mlaf(2.7129230470588235, y_c_lms, 1.9459282392156863 * b_c_lms),
        );
        Rgb::new(r, g, b)
    }

    #[inline]
    /// Converts [Xyb] to [Rgb] using provided [TransferFunction]
    pub fn to_rgb(&self, transfer_function: TransferFunction) -> Rgb<u8> {
        let linear_rgb = self.to_linear_rgb();
        linear_rgb.gamma(transfer_function).to_u8()
    }
}

impl Add<f32> for Xyb {
    type Output = Xyb;

    #[inline]
    fn add(self, rhs: f32) -> Self::Output {
        Xyb::new(self.x + rhs, self.y + rhs, self.b + rhs)
    }
}

impl Add<Xyb> for Xyb {
    type Output = Xyb;

    #[inline]
    fn add(self, rhs: Xyb) -> Self::Output {
        Xyb::new(self.x + rhs.x, self.y + rhs.y, self.b + rhs.b)
    }
}

impl Sub<f32> for Xyb {
    type Output = Xyb;

    #[inline]
    fn sub(self, rhs: f32) -> Self::Output {
        Xyb::new(self.x - rhs, self.y - rhs, self.b - rhs)
    }
}

impl Sub<Xyb> for Xyb {
    type Output = Xyb;

    #[inline]
    fn sub(self, rhs: Xyb) -> Self::Output {
        Xyb::new(self.x - rhs.x, self.y - rhs.y, self.b - rhs.b)
    }
}

impl Mul<f32> for Xyb {
    type Output = Xyb;

    #[inline]
    fn mul(self, rhs: f32) -> Self::Output {
        Xyb::new(self.x * rhs, self.y * rhs, self.b * rhs)
    }
}

impl Mul<Xyb> for Xyb {
    type Output = Xyb;

    #[inline]
    fn mul(self, rhs: Xyb) -> Self::Output {
        Xyb::new(self.x * rhs.x, self.y * rhs.y, self.b * rhs.b)
    }
}

impl Div<f32> for Xyb {
    type Output = Xyb;

    #[inline]
    fn div(self, rhs: f32) -> Self::Output {
        Xyb::new(self.x / rhs, self.y / rhs, self.b / rhs)
    }
}

impl Div<Xyb> for Xyb {
    type Output = Xyb;

    #[inline]
    fn div(self, rhs: Xyb) -> Self::Output {
        Xyb::new(self.x / rhs.x, self.y / rhs.y, self.b / rhs.b)
    }
}

impl Neg for Xyb {
    type Output = Xyb;

    #[inline]
    fn neg(self) -> Self::Output {
        Xyb::new(-self.x, -self.y, -self.b)
    }
}

impl Pow<f32> for Xyb {
    type Output = Xyb;

    #[inline]
    fn pow(self, rhs: f32) -> Self::Output {
        Xyb::new(self.x.powf(rhs), self.y.powf(rhs), self.b.powf(rhs))
    }
}

impl Pow<Xyb> for Xyb {
    type Output = Xyb;

    #[inline]
    fn pow(self, rhs: Xyb) -> Self::Output {
        Xyb::new(self.x.powf(rhs.x), self.y.powf(rhs.y), self.b.powf(rhs.b))
    }
}

impl MulAssign<f32> for Xyb {
    #[inline]
    fn mul_assign(&mut self, rhs: f32) {
        self.x *= rhs;
        self.y *= rhs;
        self.b *= rhs;
    }
}

impl MulAssign<Xyb> for Xyb {
    #[inline]
    fn mul_assign(&mut self, rhs: Xyb) {
        self.x *= rhs.x;
        self.y *= rhs.y;
        self.b *= rhs.b;
    }
}

impl AddAssign<f32> for Xyb {
    #[inline]
    fn add_assign(&mut self, rhs: f32) {
        self.x += rhs;
        self.y += rhs;
        self.b += rhs;
    }
}

impl AddAssign<Xyb> for Xyb {
    #[inline]
    fn add_assign(&mut self, rhs: Xyb) {
        self.x += rhs.x;
        self.y += rhs.y;
        self.b += rhs.b;
    }
}

impl SubAssign<f32> for Xyb {
    #[inline]
    fn sub_assign(&mut self, rhs: f32) {
        self.x -= rhs;
        self.y -= rhs;
        self.b -= rhs;
    }
}

impl SubAssign<Xyb> for Xyb {
    #[inline]
    fn sub_assign(&mut self, rhs: Xyb) {
        self.x -= rhs.x;
        self.y -= rhs.y;
        self.b -= rhs.b;
    }
}

impl DivAssign<f32> for Xyb {
    #[inline]
    fn div_assign(&mut self, rhs: f32) {
        self.x /= rhs;
        self.y /= rhs;
        self.b /= rhs;
    }
}

impl DivAssign<Xyb> for Xyb {
    #[inline]
    fn div_assign(&mut self, rhs: Xyb) {
        self.x /= rhs.x;
        self.y /= rhs.y;
        self.b /= rhs.b;
    }
}

impl Xyb {
    #[inline]
    pub fn sqrt(&self) -> Xyb {
        Xyb::new(
            if self.x < 0. { 0. } else { self.x.sqrt() },
            if self.y < 0. { 0. } else { self.y.sqrt() },
            if self.b < 0. { 0. } else { self.b.sqrt() },
        )
    }

    #[inline]
    pub fn cbrt(&self) -> Xyb {
        Xyb::new(self.x.cbrt(), self.y.cbrt(), self.b.cbrt())
    }
}

impl EuclideanDistance for Xyb {
    fn euclidean_distance(&self, other: Self) -> f32 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        let db = self.b - other.b;
        (dx * dx + dy * dy + db * db).sqrt()
    }
}

impl TaxicabDistance for Xyb {
    fn taxicab_distance(&self, other: Self) -> f32 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        let db = self.b - other.b;
        dx.abs() + dy.abs() + db.abs()
    }
}