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
/*
 * // 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.
 */

//! # Luv
/// Struct representing a color in CIE LUV, a.k.a. L\*u\*v\*, color space
#[derive(Debug, Copy, Clone, Default, PartialOrd)]
pub struct Luv {
    /// The L\* value (achromatic luminance) of the colour in 0–100 range.
    pub l: f32,
    /// The u\* value of the colour.
    ///
    /// Together with v\* value, it defines chromaticity of the colour.  The u\*
    /// coordinate represents colour’s position on red-green axis with negative
    /// values indicating more red and positive more green colour.  Typical
    /// values are in -134–220 range (but exact range for ‘valid’ colours
    /// depends on luminance and v\* value).
    pub u: f32,
    /// The u\* value of the colour.
    ///
    /// Together with u\* value, it defines chromaticity of the colour.  The v\*
    /// coordinate represents colour’s position on blue-yellow axis with
    /// negative values indicating more blue and positive more yellow colour.
    /// Typical values are in -140–122 range (but exact range for ‘valid’
    /// colours depends on luminance and u\* value).
    pub v: f32,
}

/// Representing a color in cylindrical CIE LCh(uv) color space
#[derive(Debug, Copy, Clone, Default, PartialOrd)]
pub struct LCh {
    /// The L\* value (achromatic luminance) of the colour in 0–100 range.
    ///
    /// This is the same value as in the [`Luv`] object.
    pub l: f32,
    /// The C\*_uv value (chroma) of the colour.
    ///
    /// Together with h_uv, it defines chromaticity of the colour.  The typical
    /// values of the coordinate go from zero up to around 150 (but exact range
    /// for ‘valid’ colours depends on luminance and hue).  Zero represents
    /// shade of grey.
    pub c: f32,
    /// The h_uv value (hue) of the colour measured in radians.
    ///
    /// Together with C\*_uv, it defines chromaticity of the colour.  The value
    /// represents an angle thus it wraps around τ.  Typically, the value will
    /// be in the -π–π range.  The value is undefined if C\*_uv is zero.
    pub h: f32,
}

const D65_XYZ: [f32; 3] = [95.047f32, 100.0f32, 108.883f32];

use crate::rgb::Rgb;
use crate::rgba::Rgba;
use crate::xyz::Xyz;
use crate::EuclideanDistance;
use erydanos::Euclidean3DDistance;

pub(crate) const LUV_WHITE_U_PRIME: f32 =
    4.0f32 * D65_XYZ[1] / (D65_XYZ[0] + 15.0 * D65_XYZ[1] + 3.0 * D65_XYZ[2]);
pub(crate) const LUV_WHITE_V_PRIME: f32 =
    9.0f32 * D65_XYZ[1] / (D65_XYZ[0] + 15.0 * D65_XYZ[1] + 3.0 * D65_XYZ[2]);

pub(crate) const LUV_CUTOFF_FORWARD_Y: f32 = (6f32 / 29f32) * (6f32 / 29f32) * (6f32 / 29f32);
pub(crate) const LUV_MULTIPLIER_FORWARD_Y: f32 = (29f32 / 3f32) * (29f32 / 3f32) * (29f32 / 3f32);
pub(crate) const LUV_MULTIPLIER_INVERSE_Y: f32 = (3f32 / 29f32) * (3f32 / 29f32) * (3f32 / 29f32);
impl Luv {
    /// Converts RGB to CIE Luv
    #[inline]
    pub fn from_rgb(rgb: Rgb<u8>) -> Self {
        let xyz = Xyz::from_srgb(rgb);
        let [x, y, z] = [xyz.x, xyz.y, xyz.z];
        let den = x + 15.0 * y + 3.0 * z;

        let l = (if y < LUV_CUTOFF_FORWARD_Y {
            LUV_MULTIPLIER_FORWARD_Y * y
        } else {
            116f32 * y.cbrt() - 16f32
        })
        .min(100f32)
        .max(0f32);
        let (u, v);
        if den != 0f32 {
            let u_prime = 4f32 * x / den;
            let v_prime = 9f32 * y / den;
            u = 13f32 * l * (u_prime - LUV_WHITE_U_PRIME);
            v = 13f32 * l * (v_prime - LUV_WHITE_V_PRIME);
        } else {
            u = 0f32;
            v = 0f32;
        }

        Luv { l, u, v }
    }

    //
    #[inline]
    pub fn from_rgba(rgba: Rgba<u8>) -> Self {
        Luv::from_rgb(rgba.to_rgb())
    }

    pub fn to_rgb(&self) -> Rgb<u8> {
        if self.l <= 0f32 {
            return Xyz::new(0f32, 0f32, 0f32).to_srgb();
        }
        let l13 = 1f32 / (13f32 * self.l);
        let u = self.u * l13 + LUV_WHITE_U_PRIME;
        let v = self.v * l13 + LUV_WHITE_V_PRIME;
        let y = if self.l > 8f32 {
            ((self.l + 16f32) / 116f32).powi(3)
        } else {
            self.l * LUV_MULTIPLIER_INVERSE_Y
        };
        let (x, z);
        if v != 0f32 {
            let den = 1f32 / (4f32 * v);
            x = y * 9f32 * u * den;
            z = y * (12.0f32 - 3.0f32 * u - 20f32 * v) * den;
        } else {
            x = 0f32;
            z = 0f32;
        }

        Xyz::new(x, y, z).to_srgb()
    }

    pub fn new(l: f32, u: f32, v: f32) -> Luv {
        Luv { l, u, v }
    }
}

impl LCh {
    pub fn from_rgb(rgb: Rgb<u8>) -> Self {
        LCh::from_luv(Luv::from_rgb(rgb))
    }

    pub fn from_rgba(rgba: Rgba<u8>) -> Self {
        LCh::from_luv(Luv::from_rgba(rgba))
    }

    pub fn new(l: f32, c: f32, h: f32) -> Self {
        LCh { l, c, h }
    }

    pub fn from_luv(luv: Luv) -> Self {
        LCh {
            l: luv.l,
            c: luv.u.hypot(luv.v),
            h: luv.v.atan2(luv.u),
        }
    }

    pub fn to_rgb(&self) -> Rgb<u8> {
        self.to_luv().to_rgb()
    }

    pub fn to_luv(&self) -> Luv {
        Luv {
            l: self.l,
            u: self.c * self.h.cos(),
            v: self.c * self.h.sin(),
        }
    }
}

impl PartialEq<Luv> for Luv {
    /// Compares two colours ignoring chromaticity if L\* is zero.
    fn eq(&self, other: &Self) -> bool {
        if self.l != other.l {
            false
        } else if self.l == 0.0 {
            true
        } else {
            self.u == other.u && self.v == other.v
        }
    }
}

impl PartialEq<LCh> for LCh {
    /// Compares two colours ignoring chromaticity if L\* is zero and hue if C\*
    /// is zero.  Hues which are τ apart are compared equal.
    fn eq(&self, other: &Self) -> bool {
        if self.l != other.l {
            false
        } else if self.l == 0.0 {
            true
        } else if self.c != other.c {
            false
        } else if self.c == 0.0 {
            true
        } else {
            use std::f32::consts::TAU;
            self.h.rem_euclid(TAU) == other.h.rem_euclid(TAU)
        }
    }
}

impl EuclideanDistance for Luv {
    fn euclidean_distance(&self, other: Luv) -> f32 {
        (self.l - other.l).hypot3(self.u - other.u, self.v - other.v)
    }
}

impl EuclideanDistance for LCh {
    fn euclidean_distance(&self, other: LCh) -> f32 {
        (self.l - other.l).hypot3(self.c - other.c, self.h - other.h)
    }
}