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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! The HSL device-dependent polar color model

use crate::channel::{
    AngularChannel, AngularChannelScalar, ChannelCast, ChannelFormatCast, ColorChannel,
    PosNormalBoundedChannel, PosNormalChannelScalar,
};
use crate::color;
use crate::color::{Color, FromTuple};
use crate::convert;
use crate::convert::GetChroma;
use crate::encoding::EncodableColor;
use crate::rgb::Rgb;
use crate::tags::HslTag;
use angle;
use angle::{Angle, Deg, FromAngle, IntoAngle};
#[cfg(feature = "approx")]
use approx;
use num_traits;
use std::fmt;
use std::ops;

//TODO: Consider adding an `HCL` constructor and conversion
/// The HSL device-dependent polar color model
///
/// ![hsl-diagram](https://upload.wikimedia.org/wikipedia/commons/6/6b/HSL_color_solid_cylinder_saturation_gray.png)
///
/// HSL is defined by a hue (base color), saturation (color richness) and value (whiteness).
/// Like HSV, HSL is modeled as a cylinder, however the underlying space is two cones
/// stacked bottom-to-bottom. /// This causes some level of
/// distortion and a degeneracy at `S=0` or `L={0,1}`. Thus, while easy to reason about, it is not good for
/// perceptual uniformity. It does an okay job with averaging colors or doing other math, but prefer
/// the CIE spaces for uniform gradients.
///
/// Hsl takes two type parameters: the cartesian channel scalar, and an angular channel scalar.
///
/// Hsl is in the same color space and encoding as the parent RGB space, it is merely a geometric
/// transformation and distortion.
///
/// For an undistorted device-dependent polar color model, look at
/// [Hsi](../hsi/struct.Hsi.html).
#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Hash)]
pub struct Hsl<T, A = Deg<T>> {
    hue: AngularChannel<A>,
    saturation: PosNormalBoundedChannel<T>,
    lightness: PosNormalBoundedChannel<T>,
}

impl<T, A> Hsl<T, A>
where
    T: PosNormalChannelScalar,
    A: AngularChannelScalar,
{
    /// Construct an `Hsl` instance from hue, saturation and lightness
    pub fn new(hue: A, saturation: T, lightness: T) -> Self {
        Hsl {
            hue: AngularChannel::new(hue),
            saturation: PosNormalBoundedChannel::new(saturation),
            lightness: PosNormalBoundedChannel::new(lightness),
        }
    }

    impl_color_color_cast_angular!(
        Hsl {
            hue,
            saturation,
            lightness
        },
        chan_traits = { PosNormalChannelScalar }
    );

    /// Returns the hue scalar
    pub fn hue(&self) -> A {
        self.hue.0.clone()
    }
    /// Returns the saturation scalar
    pub fn saturation(&self) -> T {
        self.saturation.0.clone()
    }
    /// Returns the lightness scalar
    pub fn lightness(&self) -> T {
        self.lightness.0.clone()
    }
    /// Returns a mutable reference to the hue scalar
    pub fn hue_mut(&mut self) -> &mut A {
        &mut self.hue.0
    }
    /// Returns a mutable reference to the saturation scalar
    pub fn saturation_mut(&mut self) -> &mut T {
        &mut self.saturation.0
    }
    /// Returns a mutable reference to the lightness scalar
    pub fn lightness_mut(&mut self) -> &mut T {
        &mut self.lightness.0
    }
    /// Set the hue channel value
    pub fn set_hue(&mut self, val: A) {
        self.hue.0 = val;
    }
    /// Set the saturation channel value
    pub fn set_saturation(&mut self, val: T) {
        self.saturation.0 = val;
    }
    /// Set the lightness channel value
    pub fn set_lightness(&mut self, val: T) {
        self.lightness.0 = val;
    }
}

impl<T, A> Color for Hsl<T, A>
where
    T: PosNormalChannelScalar,
    A: AngularChannelScalar,
{
    type Tag = HslTag;
    type ChannelsTuple = (A, T, T);

    fn num_channels() -> u32 {
        3
    }
    fn to_tuple(self) -> Self::ChannelsTuple {
        (self.hue.0, self.saturation.0, self.lightness.0)
    }
}

impl<T, A> FromTuple for Hsl<T, A>
where
    T: PosNormalChannelScalar,
    A: AngularChannelScalar,
{
    fn from_tuple(values: Self::ChannelsTuple) -> Self {
        Hsl::new(values.0, values.1, values.2)
    }
}

impl<T, A> color::PolarColor for Hsl<T, A>
where
    T: PosNormalChannelScalar,
    A: AngularChannelScalar,
{
    type Angular = A;
    type Cartesian = T;
}

impl<T, A> color::Invert for Hsl<T, A>
where
    T: PosNormalChannelScalar,
    A: AngularChannelScalar,
{
    impl_color_invert!(Hsl {
        hue,
        saturation,
        lightness
    });
}

impl<T, A> color::Lerp for Hsl<T, A>
where
    T: PosNormalChannelScalar + color::Lerp,
    A: AngularChannelScalar + color::Lerp,
{
    type Position = A::Position;

    impl_color_lerp_angular!(Hsl<T> {hue, saturation, lightness});
}

impl<T, A> color::Bounded for Hsl<T, A>
where
    T: PosNormalChannelScalar,
    A: AngularChannelScalar,
{
    impl_color_bounded!(Hsl {
        hue,
        saturation,
        lightness
    });
}

impl<T, A> EncodableColor for Hsl<T, A>
where
    T: PosNormalChannelScalar + num_traits::Float,
    A: AngularChannelScalar + Angle<Scalar = T> + FromAngle<angle::Turns<T>>,
{
}

#[cfg(feature = "approx")]
impl<T, A> approx::AbsDiffEq for Hsl<T, A>
where
    T: PosNormalChannelScalar + approx::AbsDiffEq<Epsilon = A::Epsilon>,
    A: AngularChannelScalar + approx::AbsDiffEq,
    A::Epsilon: Clone + num_traits::Float,
{
    impl_abs_diff_eq!({hue, saturation, lightness});
}
#[cfg(feature = "approx")]
impl<T, A> approx::RelativeEq for Hsl<T, A>
where
    T: PosNormalChannelScalar + approx::RelativeEq<Epsilon = A::Epsilon>,
    A: AngularChannelScalar + approx::RelativeEq,
    A::Epsilon: Clone + num_traits::Float,
{
    impl_rel_eq!({hue, saturation, lightness});
}
#[cfg(feature = "approx")]
impl<T, A> approx::UlpsEq for Hsl<T, A>
where
    T: PosNormalChannelScalar + approx::UlpsEq<Epsilon = A::Epsilon>,
    A: AngularChannelScalar + approx::UlpsEq,
    A::Epsilon: Clone + num_traits::Float,
{
    impl_ulps_eq!({hue, saturation, lightness});
}

impl<T, A> Default for Hsl<T, A>
where
    T: PosNormalChannelScalar + num_traits::Zero,
    A: AngularChannelScalar + num_traits::Zero,
{
    impl_color_default!(Hsl {
        hue: AngularChannel,
        saturation: PosNormalBoundedChannel,
        lightness: PosNormalBoundedChannel
    });
}
impl<T, A> fmt::Display for Hsl<T, A>
where
    T: PosNormalChannelScalar + fmt::Display,
    A: AngularChannelScalar + fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Hsl({}, {}, {})",
            self.hue, self.saturation, self.lightness
        )
    }
}

impl<T, A> convert::GetChroma for Hsl<T, A>
where
    T: PosNormalChannelScalar + ops::Mul<T, Output = T> + num_traits::Float,
    A: AngularChannelScalar,
{
    type ChromaType = T;
    fn get_chroma(&self) -> T {
        let one: T = num_traits::cast(1.0).unwrap();
        let scaled_lightness: T =
            (num_traits::cast::<_, T>(2.0).unwrap() * self.lightness() - one).abs();

        (one - scaled_lightness) * self.saturation()
    }
}
impl<T, A> convert::GetHue for Hsl<T, A>
where
    T: PosNormalChannelScalar,
    A: AngularChannelScalar,
{
    impl_color_get_hue_angular!(Hsl);
}

impl<T, A> convert::FromColor<Hsl<T, A>> for Rgb<T>
where
    T: PosNormalChannelScalar + num_traits::Float,
    A: AngularChannelScalar,
{
    fn from_color(from: &Hsl<T, A>) -> Self {
        let (hue_seg, hue_frac) = convert::decompose_hue_segment(from);
        let one_half: T = num_traits::cast(0.5).unwrap();

        let hue_frac_t: T = num_traits::cast(hue_frac).unwrap();

        let chroma = from.get_chroma();
        let channel_min = from.lightness() - num_traits::cast::<_, T>(0.5).unwrap() * chroma;
        let channel_max = channel_min + chroma;

        match hue_seg {
            0 => {
                let g = chroma * (hue_frac_t - one_half) + from.lightness();
                Rgb::new(channel_max, g, channel_min)
            }
            1 => {
                let r = chroma * (one_half - hue_frac_t) + from.lightness();
                Rgb::new(r, channel_max, channel_min)
            }
            2 => {
                let b = chroma * (hue_frac_t - one_half) + from.lightness();
                Rgb::new(channel_min, channel_max, b)
            }
            3 => {
                let g = chroma * (one_half - hue_frac_t) + from.lightness();
                Rgb::new(channel_min, g, channel_max)
            }
            4 => {
                let r = chroma * (hue_frac_t - one_half) + from.lightness();
                Rgb::new(r, channel_min, channel_max)
            }
            5 => {
                let b = chroma * (one_half - hue_frac_t) + from.lightness();
                Rgb::new(channel_max, channel_min, b)
            }
            _ => unreachable!(),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::color::*;
    use crate::convert::*;
    use crate::rgb::Rgb;
    use angle::*;
    use approx::*;
    use std::f32::consts;

    use crate::test;

    #[test]
    fn test_construct() {
        let c1 = Hsl::new(Deg(90.0), 0.5, 0.25);
        assert_eq!(c1.hue(), Deg(90.0));
        assert_eq!(c1.saturation(), 0.5);
        assert_eq!(c1.lightness(), 0.25);
        assert_eq!(c1.to_tuple(), (Deg(90.0), 0.5, 0.25));
        assert_eq!(Hsl::from_tuple(c1.to_tuple()), c1);

        let c2 = Hsl::new(Rad(consts::PI), 0.20f32, 0.90f32);
        assert_eq!(c2.hue(), Rad(consts::PI));
        assert_eq!(c2.saturation(), 0.2);
        assert_eq!(c2.lightness(), 0.90);
    }

    #[test]
    fn test_chroma() {
        let test_data = test::build_hs_test_data();

        for item in test_data.iter() {
            let chroma = item.hsl.get_chroma();
            assert_relative_eq!(chroma, item.chroma, epsilon = 1e-3);
        }
    }

    #[test]
    fn test_invert() {
        let c1 = Hsl::new(Deg(100f32), 0.77f32, 0.5);
        assert_relative_eq!(c1.clone().invert().invert(), c1);
        assert_relative_eq!(c1.invert(), Hsl::new(Deg(280f32), 0.23f32, 0.5));

        let c2 = Hsl::new(Turns(0.10), 0.11, 0.55);
        assert_relative_eq!(c2.clone().invert().invert(), c2);
        assert_relative_eq!(c2.invert(), Hsl::new(Turns(0.60), 0.89, 0.45));
    }

    #[test]
    fn test_lerp() {
        let c1 = Hsl::new(Turns(0.2), 0.25, 0.80);
        let c2 = Hsl::new(Turns(0.8), 0.75, 0.30);
        assert_relative_eq!(c1.lerp(&c2, 0.0), c1);
        assert_relative_eq!(c1.lerp(&c2, 1.0), c2);
        assert_relative_eq!(c1.lerp(&c2, 0.5), Hsl::new(Turns(0.0), 0.5, 0.55));
    }

    #[test]
    fn test_hsl_to_rgb() {
        let test_data = test::build_hs_test_data();

        for item in test_data.iter() {
            let rgb = Rgb::from_color(&item.hsl);
            assert_relative_eq!(rgb, item.rgb, epsilon = 1e-3);
            let hsl = Hsl::from_color(&rgb);
            assert_relative_eq!(hsl, item.hsl, epsilon = 1e-3);
        }
    }

    #[test]
    fn test_color_cast() {
        let c1 = Hsl::new(Deg(90.0), 0.23, 0.45);
        assert_relative_eq!(c1.color_cast(), Hsl::new(Turns(0.25f32), 0.23f32, 0.45f32));
        assert_relative_eq!(c1.color_cast(), c1, epsilon = 1e-7);
        assert_relative_eq!(
            c1.color_cast::<f32, Rad<f32>>().color_cast(),
            c1,
            epsilon = 1e-7
        );
    }
}