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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use num::Float;

use std::ops::{Add, Sub};
use std::marker::PhantomData;

use {Alpha, Rgb, Xyz, Hsl, Hwb};
use {Limited, Mix, Shade, GetHue, Hue, Saturate, RgbHue, FromColor};
use {clamp, flt};
use white_point::{WhitePoint, D65};

///Linear HSV with an alpha component. See the [`Hsva` implementation in `Alpha`](struct.Alpha.html#Hsva).
pub type Hsva<Wp = D65, T = f32> = Alpha<Hsv<Wp, T>, T>;

///Linear HSV color space.
///
///HSV is a cylindrical version of [RGB](struct.Rgb.html) and it's very
///similar to [HSL](struct.Hsl.html). The difference is that the `value`
///component in HSV determines the _brightness_ of the color, and not the
///_lightness_. The difference is that, for example, red (100% R, 0% G, 0% B)
///and white (100% R, 100% G, 100% B) has the same brightness (or value), but
///not the same lightness.
#[derive(Debug, PartialEq)]
pub struct Hsv<Wp = D65, T = f32>
    where T: Float,
        Wp: WhitePoint<T>
{
    ///The hue of the color, in degrees. Decides if it's red, blue, purple,
    ///etc.
    pub hue: RgbHue<T>,

    ///The colorfulness of the color. 0.0 gives gray scale colors and 1.0 will
    ///give absolutely clear colors.
    pub saturation: T,

    ///Decides how bright the color will look. 0.0 will be black, and 1.0 will
    ///give a bright an clear color that goes towards white when `saturation`
    ///goes towards 0.0.
    pub value: T,

    ///The white point associated with the color's illuminant and observer.
    ///D65 for 2 degree observer is used by default.
    pub white_point: PhantomData<Wp>,
}

impl<Wp, T> Copy for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{}

impl<Wp, T> Clone for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn clone(&self) -> Hsv<Wp, T> { *self }
}


impl<T> Hsv<D65, T>
    where T: Float,
{
    ///Linear HSV with white point D65.
    pub fn new(hue: RgbHue<T>, saturation: T, value: T) -> Hsv<D65, T> {
        Hsv {
            hue: hue,
            saturation: saturation,
            value: value,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    ///Linear HSV.
    pub fn with_wp(hue: RgbHue<T>, saturation: T, value: T) -> Hsv<Wp, T> {
        Hsv {
            hue: hue,
            saturation: saturation,
            value: value,
            white_point: PhantomData,
        }
    }
}

///<span id="Hsva"></span>[`Hsva`](type.Hsva.html) implementations.
impl<T> Alpha<Hsv<D65, T>, T>
    where T: Float,
{
    ///Linear HSV and transparency with white point D65.
    pub fn new(hue: RgbHue<T>, saturation: T, value: T, alpha: T) -> Hsva<D65, T> {
        Alpha {
            color: Hsv::new(hue, saturation, value),
            alpha: alpha,
        }
    }
}

///<span id="Hsva"></span>[`Hsva`](type.Hsva.html) implementations.
impl<Wp, T> Alpha<Hsv<Wp, T>, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    ///Linear HSV and transparency.
    pub fn with_wp(hue: RgbHue<T>, saturation: T, value: T, alpha: T) -> Hsva<Wp, T> {
        Alpha {
            color: Hsv::with_wp(hue, saturation, value),
            alpha: alpha,
        }
    }
}

impl<Wp, T> FromColor<Wp, T> for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn from_xyz(xyz: Xyz<Wp, T>) -> Self {
        let rgb: Rgb<Wp, T> = Rgb::from_xyz(xyz);
        // let hsl = xyz.into_hsl();
        Self::from_rgb(rgb)
    }

    fn from_rgb(rgb: Rgb<Wp, T>) -> Self {
        let ( max, min, sep , coeff) = {
            let (max, min , sep, coeff) = if rgb.red > rgb.green {
                (rgb.red, rgb.green, rgb.green - rgb.blue, T::zero() )
            } else {
                (rgb.green, rgb.red, rgb.blue - rgb.red , flt(2.0))
            };
            if rgb.blue > max {
                ( rgb.blue, min , rgb.red - rgb.green , flt(4.0))
            } else {
                let min_val = if rgb.blue < min { rgb.blue } else { min };
                (max , min_val , sep, coeff)
            }
        };

        let mut h = T::zero();
        let mut s = T::zero();
        let v = max;

        if max != min {
            let d = max - min;
            s = d / max;
            h = (( sep / d ) + coeff) *  flt(60.0);
        };

        Hsv {
            hue: h.into(),
            saturation: s,
            value: v,
            white_point: PhantomData,
        }
    }

    fn from_hsl(hsl: Hsl<Wp, T>) -> Self {
        let x = hsl.saturation * if hsl.lightness < flt(0.5) {
            hsl.lightness
        } else {
            T::one() - hsl.lightness
        };
        let mut s = T::zero();

        // avoid divide by zero
        let denom = hsl.lightness + x;
        if denom.is_normal() {
            s = x * flt(2.0) / denom;
        }
        Hsv {
            hue: hsl.hue,
            saturation: s,
            value: hsl.lightness + x,
            white_point: PhantomData,
        }
    }

    fn from_hsv(hsv: Hsv<Wp, T>) -> Self {
        hsv
    }

    fn from_hwb(hwb: Hwb<Wp, T>) -> Self {
        let inv = T::one() - hwb.blackness;
        // avoid divide by zero
        let s = if inv.is_normal() {
            T::one() - ( hwb.whiteness / inv )
        } else {
            T::zero()
        };
        Hsv {
            hue: hwb.hue,
            saturation: s,
            value: inv,
            white_point: PhantomData,
        }
    }

}

impl<Wp, T> Limited for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn is_valid(&self) -> bool {
        self.saturation >= T::zero() && self.saturation <= T::one() &&
        self.value >= T::zero() && self.value <= T::one()
    }

    fn clamp(&self) -> Hsv<Wp, T> {
        let mut c = *self;
        c.clamp_self();
        c
    }

    fn clamp_self(&mut self) {
        self.saturation = clamp(self.saturation, T::zero(), T::one());
        self.value = clamp(self.value, T::zero(), T::one());
    }
}

impl<Wp, T> Mix for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Scalar = T;

    fn mix(&self, other: &Hsv<Wp, T>, factor: T) -> Hsv<Wp, T> {
        let factor = clamp(factor, T::zero(), T::one());
        let hue_diff: T = (other.hue - self.hue).to_degrees();

        Hsv {
            hue: self.hue + factor * hue_diff,
            saturation: self.saturation + factor * (other.saturation - self.saturation),
            value: self.value + factor * (other.value - self.value),
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Shade for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Scalar = T;

    fn lighten(&self, amount: T) -> Hsv<Wp, T> {
        Hsv {
            hue: self.hue,
            saturation: self.saturation,
            value: self.value + amount,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> GetHue for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Hue = RgbHue<T>;

    fn get_hue(&self) -> Option<RgbHue<T>> {
        if self.saturation <= T::zero() || self.value <= T::zero() {
            None
        } else {
            Some(self.hue)
        }
    }
}

impl<Wp, T> Hue for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn with_hue(&self, hue: RgbHue<T>) -> Hsv<Wp, T> {
        Hsv {
            hue: hue,
            saturation: self.saturation,
            value: self.value,
            white_point: PhantomData,
        }
    }

    fn shift_hue(&self, amount: RgbHue<T>) -> Hsv<Wp, T> {
        Hsv {
            hue: self.hue + amount,
            saturation: self.saturation,
            value: self.value,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Saturate for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Scalar = T;

    fn saturate(&self, factor: T) -> Hsv<Wp, T> {
        Hsv {
            hue: self.hue,
            saturation: self.saturation * (T::one() + factor),
            value: self.value,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Default for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn default() -> Hsv<Wp, T> {
        Hsv::with_wp(RgbHue::from(T::zero()), T::zero(), T::zero())
    }
}

impl<Wp, T> Add<Hsv<Wp, T>> for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Output = Hsv<Wp, T>;

    fn add(self, other: Hsv<Wp, T>) -> Hsv<Wp, T> {
        Hsv {
            hue: self.hue + other.hue,
            saturation: self.saturation + other.saturation,
            value: self.value + other.value,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Add<T> for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Output = Hsv<Wp, T>;

    fn add(self, c: T) -> Hsv<Wp, T> {
        Hsv {
            hue: self.hue + c,
            saturation: self.saturation + c,
            value: self.value + c,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Sub<Hsv<Wp, T>> for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Output = Hsv<Wp, T>;

    fn sub(self, other: Hsv<Wp, T>) -> Hsv<Wp, T> {
        Hsv {
            hue: self.hue - other.hue,
            saturation: self.saturation - other.saturation,
            value: self.value - other.value,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Sub<T> for Hsv<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Output = Hsv<Wp, T>;

    fn sub(self, c: T) -> Hsv<Wp, T> {
        Hsv {
            hue: self.hue - c,
            saturation: self.saturation - c,
            value: self.value - c,
            white_point: PhantomData,
        }
    }
}

#[cfg(test)]
mod test {
    use super::Hsv;
    use ::{Rgb, Hsl};

    #[test]
    fn red() {
        let a = Hsv::from(Rgb::new(1.0, 0.0, 0.0));
        let b = Hsv::new(0.0.into(), 1.0, 1.0);
        let c = Hsv::from(Hsl::new(0.0.into(), 1.0, 0.5));

        assert_relative_eq!(a, b);
        assert_relative_eq!(a, c);
    }

    #[test]
    fn orange() {
        let a = Hsv::from(Rgb::new(1.0, 0.5, 0.0));
        let b = Hsv::new(30.0.into(), 1.0, 1.0);
        let c = Hsv::from(Hsl::new(30.0.into(), 1.0, 0.5));

        assert_relative_eq!(a, b);
        assert_relative_eq!(a, c);
    }

    #[test]
    fn green() {
        let a = Hsv::from(Rgb::new(0.0, 1.0, 0.0));
        let b = Hsv::new(120.0.into(), 1.0, 1.0);
        let c = Hsv::from(Hsl::new(120.0.into(), 1.0, 0.5));

        assert_relative_eq!(a, b);
        assert_relative_eq!(a, c);
    }

    #[test]
    fn blue() {
        let a = Hsv::from(Rgb::new(0.0, 0.0, 1.0));
        let b = Hsv::new(240.0.into(), 1.0, 1.0);
        let c = Hsv::from(Hsl::new(240.0.into(), 1.0, 0.5));

        assert_relative_eq!(a, b);
        assert_relative_eq!(a, c);
    }

    #[test]
    fn purple() {
        let a = Hsv::from(Rgb::new(0.5, 0.0, 1.0));
        let b = Hsv::new(270.0.into(), 1.0, 1.0);
        let c = Hsv::from(Hsl::new(270.0.into(), 1.0, 0.5));

        assert_relative_eq!(a, b);
        assert_relative_eq!(a, c);
    }

    #[test]
    fn ranges() {
        assert_ranges!{
            Hsv;
            limited {
                saturation: 0.0 => 1.0,
                value: 0.0 => 1.0
            }
            limited_min {}
            unlimited {
                hue: -360.0 => 360.0
            }
        }
    }
}