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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
use num::Float;

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

use {Alpha, Luma, Xyz, Hsv, Hsl, RgbHue};
use {Limited, Mix, Shade, GetHue, FromColor, Blend, ComponentWise};
use white_point::{WhitePoint, D65};
use ::rgb_primaries::rgb_to_xyz_matrix;
use matrix::{matrix_inverse, multiply_xyz_to_rgb};
use {clamp, flt};
use pixel::{RgbPixel, Srgb, GammaRgb};
use blend::PreAlpha;

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

///Linear RGB.
///
///RGB is probably the most common color space, when it comes to computer
///graphics, and it's defined as an additive mixture of red, green and blue
///light, where gray scale colors are created when these three channels are
///equal in strength. This particular RGB type is based on the ITU-R BT.709
///primaries, which makes it a linear version of sRGB.
///
///Conversions and operations on this color space assumes that it's linear,
///meaning that gamma correction is required when converting to and from a
///displayable RGB, such as sRGB. See the [`pixel`](pixel/index.html) module
///for encoding types.
#[derive(Debug, PartialEq)]
pub struct Rgb<Wp = D65, T = f32>
    where T: Float,
        Wp: WhitePoint<T>
{
    ///The amount of red light, where 0.0 is no red light and 1.0 is the
    ///highest displayable amount.
    pub red: T,

    ///The amount of green light, where 0.0 is no green light and 1.0 is the
    ///highest displayable amount.
    pub green: T,

    ///The amount of blue light, where 0.0 is no blue light and 1.0 is the
    ///highest displayable amount.
    pub blue: 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 Rgb<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{}

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

impl<T> Rgb<D65, T>
    where T: Float,
{
    ///Linear RGB with white point D65.
    pub fn new(red: T, green: T, blue: T) -> Rgb<D65, T> {
        Rgb {
            red: red,
            green: green,
            blue: blue,
            white_point: PhantomData,
        }
    }

    ///Linear RGB from 8 bit values with whtie point D65.
    pub fn new_u8(red: u8, green: u8, blue: u8) -> Rgb<D65, T> {
        Rgb {
            red: flt::<T,_>(red) / flt(255.0),
            green: flt::<T,_>(green) / flt(255.0),
            blue: flt::<T,_>(blue) / flt(255.0),
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> Rgb<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    ///Linear RGB.
    pub fn with_wp(red: T, green: T, blue: T) -> Rgb<Wp, T> {
        Rgb {
            red: red,
            green: green,
            blue: blue,
            white_point: PhantomData,
        }
    }

    ///Linear RGB from 8 bit values.
    pub fn with_wp_u8(red: u8, green: u8, blue: u8) -> Rgb<Wp, T> {
        Rgb {
            red: flt::<T,_>(red) / flt(255.0),
            green: flt::<T,_>(green) / flt(255.0),
            blue: flt::<T,_>(blue) / flt(255.0),
            white_point: PhantomData,
        }
    }

    ///Linear RGB from a linear pixel value.
    pub fn from_pixel<P: RgbPixel<T>>(pixel: &P) -> Rgb<Wp, T> {
        let (r, g, b, _) = pixel.to_rgba();
        Rgb::with_wp(r, g, b)
    }

    ///Convert to a linear RGB pixel. `Rgb` is already assumed to be linear,
    ///so the components will just be clamped to [0.0, 1.0] before conversion.
    ///
    ///```
    ///use palette::Rgb;
    ///
    ///let c = Rgb::new(0.5, 0.3, 0.1);
    ///assert_eq!((c.red, c.green, c.blue), c.to_pixel());
    ///assert_eq!((0.5, 0.3, 0.1), c.to_pixel());
    ///```
    pub fn to_pixel<P: RgbPixel<T>>(&self) -> P {
        P::from_rgba(
            clamp(self.red, T::zero(), T::one()),
            clamp(self.green, T::zero(), T::one()),
            clamp(self.blue, T::zero(), T::one()),
            T::one(),
        )
    }
}

///<span id="Rgba"></span>[`Rgba`](type.Rgba.html) implementations.
impl<T> Alpha<Rgb<D65, T>, T>
    where T: Float,
{
    ///Linear RGB with transparency and with white point D65.
    pub fn new(red: T, green: T, blue: T, alpha: T) -> Rgba<D65, T> {
        Alpha {
            color: Rgb::new(red, green, blue),
            alpha: alpha,
        }
    }

    ///Linear RGB with transparency from 8 bit values and with white point D65.
    pub fn new_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Rgba<D65, T> {
        Alpha {
            color: Rgb::new_u8(red, green, blue),
            alpha: flt::<T,_>(alpha) / flt(255.0),
        }
    }

}

///<span id="Rgba"></span>[`Rgba`](type.Rgba.html) implementations.
impl<Wp, T> Alpha<Rgb<Wp, T>, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    ///Linear RGB with transparency.
    pub fn with_wp(red: T, green: T, blue: T, alpha: T) -> Rgba<Wp, T> {
        Alpha {
            color: Rgb::with_wp(red, green, blue),
            alpha: alpha,
        }
    }

    ///Linear RGB with transparency from 8 bit values.
    pub fn with_wp_u8(red: u8, green: u8, blue: u8, alpha: u8) -> Rgba<Wp, T> {
        Alpha {
            color: Rgb::with_wp_u8(red, green, blue),
            alpha: flt::<T,_>(alpha) / flt(255.0),
        }
    }

    ///Linear RGB from a linear pixel value.
    pub fn from_pixel<P: RgbPixel<T>>(pixel: &P) -> Rgba<Wp, T> {
        let (r, g, b, a) = pixel.to_rgba();
        Rgba::with_wp(r, g, b, a)
    }

    ///Convert to a linear RGB pixel. `Rgb` is already assumed to be linear,
    ///so the components will just be clamped to [0.0, 1.0] before conversion.
    ///
    ///```
    ///use palette::Rgba;
    ///
    ///let c = Rgba::new(0.5, 0.3, 0.1, 0.5);
    ///assert_eq!((c.red, c.green, c.blue, c.alpha), c.to_pixel());
    ///assert_eq!((0.5, 0.3, 0.1, 0.5), c.to_pixel());
    ///```
    pub fn to_pixel<P: RgbPixel<T>>(&self) -> P {
        P::from_rgba(
            clamp(self.red, T::zero(), T::one()),
            clamp(self.green, T::zero(), T::one()),
            clamp(self.blue, T::zero(), T::one()),
            clamp(self.alpha, T::zero(), T::one())
        )
    }
}

impl<Wp, T> FromColor<Wp, T> for Rgb<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn from_xyz(xyz: Xyz<Wp, T>) -> Self {
        let transform_matrix = matrix_inverse(&rgb_to_xyz_matrix::<Wp, T>());
        multiply_xyz_to_rgb::<Wp, Wp, T>(&transform_matrix, &xyz)
    }


    fn from_rgb(rgb: Rgb<Wp, T>) -> Self {
        rgb
    }

    fn from_hsl(hsl: Hsl<Wp, T>) -> Self {
        let c = (T::one() - ( hsl.lightness * flt(2.0) - T::one()).abs()) * hsl.saturation;
        let h = hsl.hue.to_positive_degrees() / flt(60.0);
        let x = c * (T::one() - (h % flt(2.0) - T::one()).abs());
        let m = hsl.lightness - c * flt(0.5);

        let (red, green, blue) = if h >= T::zero() && h < T::one() {
            (c, x, T::zero())
        } else if h >= T::one() && h < flt(2.0) {
            (x, c, T::zero())
        } else if h >= flt(2.0) && h < flt(3.0) {
            (T::zero(), c, x)
        } else if h >= flt(3.0) && h < flt(4.0) {
            (T::zero(), x, c)
        } else if h >= flt(4.0) && h < flt(5.0) {
            (x, T::zero(), c)
        } else {
            (c, T::zero(), x)
        };


        Rgb {
            red: red + m,
            green: green + m,
            blue: blue + m,
            white_point: PhantomData,
        }
    }

    fn from_hsv(hsv: Hsv<Wp, T>) -> Self {
        let c = hsv.value * hsv.saturation;
        let h = hsv.hue.to_positive_degrees() / flt(60.0);
        let x = c * (T::one() - (h % flt(2.0) - T::one()).abs());
        let m = hsv.value - c;

        let (red, green, blue) = if h >= T::zero() && h < T::one() {
            (c, x, T::zero())
        } else if h >= T::one() && h < flt(2.0) {
            (x, c, T::zero())
        } else if h >= flt(2.0) && h < flt(3.0) {
            (T::zero(), c, x)
        } else if h >= flt(3.0) && h < flt(4.0) {
            (T::zero(), x, c)
        } else if h >= flt(4.0) && h < flt(5.0) {
            (x, T::zero(), c)
        } else {
            (c, T::zero(), x)
        };


        Rgb {
            red: red + m,
            green: green + m,
            blue: blue + m,
            white_point: PhantomData,
        }

    }

    fn from_luma(luma: Luma<Wp, T>) -> Self {
        Rgb {
            red: luma.luma,
            green: luma.luma,
            blue: luma.luma,
            white_point: PhantomData,
        }
    }

}

impl<Wp, T> Limited for Rgb<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn is_valid(&self) -> bool {
        self.red >= T::zero() && self.red <= T::one() &&
        self.green >= T::zero() && self.green <= T::one() &&
        self.blue >= T::zero() && self.blue <= T::one()
    }

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

    fn clamp_self(&mut self) {
        self.red = clamp(self.red, T::zero(), T::one());
        self.green = clamp(self.green, T::zero(), T::one());
        self.blue = clamp(self.blue, T::zero(), T::one());
    }
}

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

    fn mix(&self, other: &Rgb<Wp, T>, factor: T) -> Rgb<Wp, T> {
        let factor = clamp(factor, T::zero(), T::one());

        Rgb {
            red: self.red + factor * (other.red - self.red),
            green: self.green + factor * (other.green - self.green),
            blue: self.blue + factor * (other.blue - self.blue),
            white_point: PhantomData,
        }
    }
}

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

    fn lighten(&self, amount: T) -> Rgb<Wp, T> {
        Rgb {
            red: self.red + amount,
            green: self.green + amount,
            blue: self.blue + amount,
            white_point: PhantomData,
        }
    }
}

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

    fn get_hue(&self) -> Option<RgbHue<T>> {
        let sqrt_3: T = flt(1.73205081);

        if self.red == self.green && self.red == self.blue {
            None
        } else {
            Some(RgbHue::from_radians((sqrt_3 * (self.green - self.blue)).atan2(self.red * flt(2.0) - self.green - self.blue)))
        }
    }
}

impl<Wp, T> Blend for Rgb<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    type Color = Rgb<Wp, T>;

    fn into_premultiplied(self) -> PreAlpha<Rgb<Wp, T>, T> {
        Rgba::from(self).into()
    }

    fn from_premultiplied(color: PreAlpha<Rgb<Wp, T>, T>) -> Self {
        Rgba::from(color).into()
    }
}

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

    fn component_wise<F: FnMut(T, T) -> T>(&self, other: &Rgb<Wp, T>, mut f: F) -> Rgb<Wp, T> {
        Rgb {
            red: f(self.red, other.red),
            green: f(self.green, other.green),
            blue: f(self.blue, other.blue),
            white_point: PhantomData,
        }
    }

    fn component_wise_self<F: FnMut(T) -> T>(&self, mut f: F) -> Rgb<Wp, T> {
        Rgb {
            red: f(self.red),
            green: f(self.green),
            blue: f(self.blue),
            white_point: PhantomData,
        }
    }
}

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

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

    fn add(self, other: Rgb<Wp, T>) -> Rgb<Wp, T> {
        Rgb {
            red: self.red + other.red,
            green: self.green + other.green,
            blue: self.blue + other.blue,
            white_point: PhantomData,
        }
    }
}

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

    fn add(self, c: T) -> Rgb<Wp, T> {
        Rgb {
            red: self.red + c,
            green: self.green + c,
            blue: self.blue + c,
            white_point: PhantomData,
        }
    }
}

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

    fn sub(self, other: Rgb<Wp, T>) -> Rgb<Wp, T> {
        Rgb {
            red: self.red - other.red,
            green: self.green - other.green,
            blue: self.blue - other.blue,
            white_point: PhantomData,
        }
    }
}

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

    fn sub(self, c: T) -> Rgb<Wp, T> {
        Rgb {
            red: self.red - c,
            green: self.green - c,
            blue: self.blue - c,
            white_point: PhantomData,
        }
    }
}

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

    fn mul(self, other: Rgb<Wp, T>) -> Rgb<Wp, T> {
        Rgb {
            red: self.red * other.red,
            green: self.green * other.green,
            blue: self.blue * other.blue,
            white_point: PhantomData,
        }
    }
}

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

    fn mul(self, c: T) -> Rgb<Wp, T> {
        Rgb {
            red: self.red * c,
            green: self.green * c,
            blue: self.blue * c,
            white_point: PhantomData,
        }
    }
}

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

    fn div(self, other: Rgb<Wp, T>) -> Rgb<Wp, T> {
        Rgb {
            red: self.red / other.red,
            green: self.green / other.green,
            blue: self.blue / other.blue,
            white_point: PhantomData,
        }
    }
}

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

    fn div(self, c: T) -> Rgb<Wp, T> {
        Rgb {
            red: self.red / c,
            green: self.green / c,
            blue: self.blue / c,
            white_point: PhantomData,
        }
    }
}

impl<Wp, T> From<Srgb<Wp, T>> for Rgb<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn from(srgb: Srgb<Wp, T>) -> Rgb<Wp, T> {
        srgb.to_linear().into()
    }
}

impl<Wp, T> From<GammaRgb<Wp, T>> for Rgb<Wp, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn from(gamma_rgb: GammaRgb<Wp, T>) -> Rgb<Wp, T> {
        gamma_rgb.to_linear().into()
    }
}

impl<Wp, T> From<Srgb<Wp, T>> for Alpha<Rgb<Wp, T>, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn from(srgb: Srgb<Wp, T>) -> Alpha<Rgb<Wp, T>, T> {
        srgb.to_linear()
    }
}

impl<Wp, T> From<GammaRgb<Wp, T>> for Alpha<Rgb<Wp, T>, T>
    where T: Float,
        Wp: WhitePoint<T>
{
    fn from(gamma_rgb: GammaRgb<Wp, T>) -> Alpha<Rgb<Wp, T>, T> {
        gamma_rgb.to_linear()
    }
}

#[cfg(test)]
mod test {
    use Rgb;

    #[test]
    fn ranges() {
        assert_ranges!{
            Rgb;
            limited {
                red: 0.0 => 1.0,
                green: 0.0 => 1.0,
                blue: 0.0 => 1.0
            }
            limited_min {}
            unlimited {}
        }
    }
}