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
// Copyright 2013 The color-rs developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{borrow::{Borrow, BorrowMut}, ops::{Mul, Div, Add, Sub, Index, IndexMut}};
use color_space::TransferFunction;
use num_traits::Saturating;
use std::mem;
use {Color, Channel};
use {Rgb, Rg, ToRgb, Hsv, YCbCr};
use color_space::{Srgb, LinearRgb};
use luma::{Luma, ToLuma};

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Debug)]
pub struct AlphaColor<T, C> { pub c: C, pub a: T }

pub type Rgba<T = u8, S = Srgb> = AlphaColor<T, Rgb<T,S>>;
pub type LumaA<T = f32, S = Srgb> = AlphaColor<T, Luma<T,S>>;
pub type Hsva<T = f32, S = Srgb> = AlphaColor<T, Hsv<T,S>>;
pub type YCbCra<T> = AlphaColor<T, YCbCr<T>>;

impl<T, C> AlphaColor<T, C>{
    pub const fn new(c: C, a: T) -> AlphaColor<T,C>{
        AlphaColor{c, a}
    }
}

impl<T: Channel, C: Color<T>> Color<T> for AlphaColor<T, C> {
    /// Clamps the components of the color to the range `(lo,hi)`.
    #[inline]
    fn clamp_s(self, lo: T, hi: T) -> AlphaColor<T, C> {
        AlphaColor {
            c: self.c.clamp_s(lo, hi),
            a: self.a.clamp(lo, hi),
        }
    }

    /// Clamps the components of the color component-wise between `lo` and `hi`.
    #[inline]
    fn clamp_c(self, lo: AlphaColor<T, C>, hi: AlphaColor<T, C>) -> AlphaColor<T, C> {
        AlphaColor {
            c: self.c.clamp_c(lo.c, hi.c),
            a: self.a.clamp(lo.a, hi.a),
        }
    }

    /// Inverts the color.
    #[inline]
    fn inverse(self) -> AlphaColor<T, C> {
        AlphaColor {
            c: self.c.inverse(),
            a: self.a.invert_channel(),
        }
    }

    #[inline]
    fn mix(self, other: Self, value: T) -> Self {
        AlphaColor {
            c: self.c.mix(other.c, value),
            a: self.a.mix(other.a, value)
        }
    }
}

#[macro_export]
macro_rules! rgba{
    ( $r: expr, $g: expr, $b: expr, $a: expr ) => ({
        use $crate::{Rgba, Rgb};
        Rgba{ c: Rgb::<_,$crate::color_space::Srgb>::new($r, $g, $b), a: $a }
    });
    ( $to_rgb: expr, $a: expr ) => ({
        use $crate::{Rgba,ToRgb};
        Rgba{ c: $to_rgb.to_rgb(), a: $a }
    });
}

#[macro_export]
macro_rules! rgba_linear{
    ( $r: expr, $g: expr, $b: expr, $a: expr ) => ({
        use $crate::{Rgba, Rgb};
        Rgba{ c: Rgb::<_,$crate::color_space::LinearRgb>::new($r, $g, $b), a: $a }
    });
    ( $to_rgb: expr, $a: expr ) => ({
        use $crate::{Rgba,ToRgb};
        Rgba{ c: $to_rgb.to_rgb().to_linear(), a: $a }
    });
}

impl<T:Channel, S: TransferFunction> Rgba<T, S> {
    pub fn from_hex(hex: u32) -> Rgba<T, S> {
        let r = hex >> 24 & 0xFF;
        let g = hex >> 16 & 0xFF;
        let b = hex >> 8 & 0xFF;
        let a = hex & 0xFF;
        Rgba{c: Rgb::new(r as u8, g as u8, b as u8), a: a as u8}.to_rgba()
    }

    #[inline]
    pub fn rg(&self) -> Rg<T, S> {
        self.c.rg()
    }

    #[inline]
    pub fn rb(&self) -> Rg<T, S> {
        self.c.rb()
    }

    #[inline]
    pub fn gr(&self) -> Rg<T, S> {
        self.c.gr()
    }

    #[inline]
    pub fn gb(&self) -> Rg<T, S> {
        self.c.gb()
    }

    #[inline]
    pub fn br(&self) -> Rg<T, S> {
        self.c.br()
    }

    #[inline]
    pub fn bg(&self) -> Rg<T, S> {
        self.c.bg()
    }

    #[inline]
    pub fn ar(&self) -> Rg<T, S> {
        Rg::new(self.a, self.c.r)
    }

    #[inline]
    pub fn ag(&self) -> Rg<T, S> {
        Rg::new(self.a, self.c.g)
    }

    #[inline]
    pub fn ab(&self) -> Rg<T, S> {
        Rg::new(self.a, self.c.b)
    }

    #[inline]
    pub fn ra(&self) -> Rg<T, S> {
        Rg::new(self.c.r, self.a)
    }

    #[inline]
    pub fn ga(&self) -> Rg<T, S> {
        Rg::new(self.c.g, self.a)
    }

    #[inline]
    pub fn ba(&self) -> Rg<T, S> {
        Rg::new(self.c.b, self.a)
    }

    #[inline]
    pub fn rgb(&self) -> Rgb<T, S> {
        self.c.rgb()
    }

    #[inline]
    pub fn rbg(&self) -> Rgb<T, S> {
        self.c.rbg()
    }

    #[inline]
    pub fn bgr(&self) -> Rgb<T, S> {
        self.c.bgr()
    }

    #[inline]
    pub fn brg(&self) -> Rgb<T, S> {
        self.c.brg()
    }

    #[inline]
    pub fn grb(&self) -> Rgb<T, S> {
        self.c.grb()
    }

    #[inline]
    pub fn gbr(&self) -> Rgb<T, S> {
        self.c.gbr()
    }

    #[inline]
    pub fn rga(&self) -> Rgb<T, S> {
        Rgb::new(self.c.r,self.c.g,self.a)
    }

    #[inline]
    pub fn rba(&self) -> Rgb<T, S> {
        Rgb::new(self.c.r,self.c.b,self.a)
    }

    #[inline]
    pub fn bra(&self) -> Rgb<T, S> {
        Rgb::new(self.c.b,self.c.r,self.a)
    }

    #[inline]
    pub fn bga(&self) -> Rgb<T, S> {
        Rgb::new(self.c.b,self.c.g,self.a)
    }

    #[inline]
    pub fn gra(&self) -> Rgb<T, S> {
        Rgb::new(self.c.g,self.c.r,self.a)
    }

    #[inline]
    pub fn gba(&self) -> Rgb<T, S> {
        Rgb::new(self.c.g,self.c.b,self.a)
    }

    #[inline]
    pub fn arg(&self) -> Rgb<T, S> {
        Rgb::new(self.a,self.c.r,self.c.g)
    }

    #[inline]
    pub fn arb(&self) -> Rgb<T, S> {
        Rgb::new(self.a,self.c.r,self.c.b)
    }

    #[inline]
    pub fn agr(&self) -> Rgb<T, S> {
        Rgb::new(self.a,self.c.g,self.c.r)
    }

    #[inline]
    pub fn agb(&self) -> Rgb<T, S> {
        Rgb::new(self.a,self.c.g,self.c.b)
    }

    #[inline]
    pub fn abr(&self) -> Rgb<T, S> {
        Rgb::new(self.a,self.c.b,self.c.r)
    }

    #[inline]
    pub fn abg(&self) -> Rgb<T, S> {
        Rgb::new(self.a,self.c.b,self.c.g)
    }

    #[inline]
    pub fn rag(&self) -> Rgb<T, S> {
        Rgb::new(self.c.r,self.a,self.c.g)
    }

    #[inline]
    pub fn rab(&self) -> Rgb<T, S> {
        Rgb::new(self.c.r,self.a,self.c.b)
    }

    #[inline]
    pub fn gar(&self) -> Rgb<T, S> {
        Rgb::new(self.c.g,self.a,self.c.r)
    }

    #[inline]
    pub fn gab(&self) -> Rgb<T, S> {
        Rgb::new(self.c.g,self.a,self.c.b)
    }

    #[inline]
    pub fn bar(&self) -> Rgb<T, S> {
        Rgb::new(self.c.b,self.a,self.c.r)
    }

    #[inline]
    pub fn bag(&self) -> Rgb<T, S> {
        Rgb::new(self.c.b,self.a,self.c.g)
    }

    #[inline]
    pub fn rgba(&self) -> Rgba<T, S> {
        rgba!(self.c, self.a)
    }

    #[inline]
    pub fn rbga(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.r, self.c.b, self.c.g), self.a)
    }

    #[inline]
    pub fn grba(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.g, self.c.r, self.c.b), self.a)
    }

    #[inline]
    pub fn gbra(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.g, self.c.b, self.c.r), self.a)
    }

    #[inline]
    pub fn brga(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.b, self.c.r, self.c.g), self.a)
    }

    #[inline]
    pub fn bgra(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.b, self.c.g, self.c.r), self.a)
    }

    #[inline]
    pub fn argb(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.a, self.c.r, self.c.g), self.c.b)
    }

    #[inline]
    pub fn arbg(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.a, self.c.r, self.c.b), self.c.g)
    }

    #[inline]
    pub fn agrb(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.a, self.c.g, self.c.r), self.c.b)
    }

    #[inline]
    pub fn agbr(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.a, self.c.g, self.c.b), self.c.r)
    }

    #[inline]
    pub fn abrg(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.a, self.c.b, self.c.r), self.c.g)
    }

    #[inline]
    pub fn abgr(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.a, self.c.b, self.c.g), self.c.r)
    }

    #[inline]
    pub fn ragb(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.r, self.a, self.c.g), self.c.b)
    }

    #[inline]
    pub fn rabg(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.r, self.a, self.c.b), self.c.g)
    }

    #[inline]
    pub fn garb(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.g, self.a, self.c.r), self.c.b)
    }

    #[inline]
    pub fn gabr(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.g, self.a, self.c.b), self.c.r)
    }

    #[inline]
    pub fn barg(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.b, self.a, self.c.r), self.c.g)
    }

    #[inline]
    pub fn bagr(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.b, self.a, self.c.g), self.c.r)
    }

    #[inline]
    pub fn rgab(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.r, self.c.g, self.a), self.c.b)
    }

    #[inline]
    pub fn rbag(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.r, self.c.b, self.a), self.c.g)
    }

    #[inline]
    pub fn grab(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.g, self.c.r, self.a), self.c.b)
    }

    #[inline]
    pub fn gbar(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.g, self.c.b, self.a), self.c.r)
    }

    #[inline]
    pub fn brag(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.b, self.c.r, self.a), self.c.g)
    }

    #[inline]
    pub fn bgar(&self) -> Rgba<T, S> {
        Rgba::new(Rgb::new(self.c.b, self.c.g, self.a), self.c.r)
    }
}

impl<T: Channel, S: TransferFunction> Rgba<T, S> {
    pub fn to_standard<S2: TransferFunction>(&self) -> Rgba<T, S2>{
        let c = self.c.to_standard();
        Rgba{c, a: self.a}
    }

    pub fn to_linear(&self) -> Rgba<T, LinearRgb>{
        let c = self.c.to_linear();
        Rgba{c, a: self.a}
    }
}


pub trait ToRgba{
    type Standard: TransferFunction;
    fn to_rgba<T: Channel>(&self) -> Rgba<T, Self::Standard>;
}

impl<T: Channel, C: ToRgb> ToRgba for AlphaColor<T,C>{
    type Standard = <C as ToRgb>::Standard;
    #[inline]
    fn to_rgba<U: Channel>(&self) -> Rgba<U, Self::Standard>{
        Rgba{c: self.c.to_rgb(), a: self.a.to_channel()}
    }
}

impl<T, C: ToRgb> ToRgb for AlphaColor<T, C> {
    type Standard = <C as ToRgb>::Standard;
    #[inline]
    fn to_rgb<U:Channel>(&self) -> Rgb<U, Self::Standard> {
        self.c.to_rgb()
    }
}

impl<T, C: ToLuma> ToLuma for AlphaColor<T, C> {
    type Standard = <C as ToLuma>::Standard;
    #[inline]
    fn to_luma<U:Channel>(&self) -> Luma<U, Self::Standard> {
        self.c.to_luma()
    }
}

impl<T:Channel, C: Mul<Output=C>> Mul for AlphaColor<T,C> {
    type Output = AlphaColor<T,C>;

    #[inline]
    fn mul(self, rhs: AlphaColor<T,C>) -> AlphaColor<T,C> {
        AlphaColor{ c: self.c.mul(rhs.c),
             a: self.a.normalized_mul(rhs.a) }
    }
}

impl<T:Channel + Mul<T,Output=T>, C: Mul<T,Output=C>> Mul<T> for AlphaColor<T,C> {
    type Output = AlphaColor<T,C>;

    #[inline]
    fn mul(self, rhs: T) -> AlphaColor<T,C> {
        let color = self.c * rhs;
        AlphaColor{ c: color,
             a: self.a * rhs }
    }
}

impl<T:Channel, C: Div<Output=C>> Div for AlphaColor<T,C> {
    type Output = AlphaColor<T,C>;

    #[inline]
    fn div(self, rhs: AlphaColor<T,C>) -> AlphaColor<T,C> {
        AlphaColor{ c: self.c.div(rhs.c),
             a: self.a.normalized_div(rhs.a) }
    }
}

impl<T:Channel + Div<T,Output=T>, C: Div<T,Output=C>> Div<T> for AlphaColor<T,C> {
    type Output = AlphaColor<T,C>;

    #[inline]
    fn div(self, rhs: T) -> AlphaColor<T,C> {
        let color = self.c / rhs;
        AlphaColor{ c: color,
             a: self.a / rhs }
    }
}

impl<T:Channel + Add<T,Output=T>, C: Add<Output=C>> Add for AlphaColor<T,C>{
    type Output = AlphaColor<T,C>;

    #[inline]
    fn add(self, rhs: AlphaColor<T,C>) -> AlphaColor<T,C> {
        AlphaColor{ c: self.c + rhs.c,
             a: self.a + rhs.a }
    }
}

impl<T:Channel + Sub<T,Output=T>, C: Sub<Output=C>> Sub for AlphaColor<T,C>{
    type Output = AlphaColor<T,C>;

    #[inline]
    fn sub(self, rhs: AlphaColor<T,C>) -> AlphaColor<T,C> {
        AlphaColor{ c: self.c - rhs.c,
             a: self.a - rhs.a }
    }
}

impl<T:Channel + Saturating, C: Saturating> Saturating for AlphaColor<T,C>{
    fn saturating_add(self, v: AlphaColor<T,C>) -> AlphaColor<T,C> {
        AlphaColor{ c: self.c.saturating_add(v.c),
              a: self.a.saturating_add(v.a) }
    }

    fn saturating_sub(self, v: AlphaColor<T,C>) -> AlphaColor<T,C> {
        AlphaColor{ c: self.c.saturating_sub(v.c),
              a: self.a.saturating_sub(v.a) }
    }
}

impl<T, C: AsRef<[T;3]>> Index<usize> for AlphaColor<T,C> {
    type Output = T;
    fn index<'a>(&'a self, index: usize) -> &'a T {
        self.as_ref().index(index)
    }
}

impl<T, C: AsRef<[T;3]> + AsMut<[T;3]>> IndexMut<usize> for AlphaColor<T,C> {
    fn index_mut<'a>(&'a mut self, index: usize) -> &'a mut T {
        self.as_mut().index_mut(index)
    }
}

impl<T, C: AsRef<[T;3]>> AsRef<[T;4]> for AlphaColor<T,C> {
    fn as_ref(&self) -> &[T;4] {
        unsafe{ mem::transmute(self)}
    }
}

impl<T, C: AsMut<[T;3]>> AsMut<[T;4]> for AlphaColor<T,C> {
    fn as_mut(&mut self) -> &mut [T;4] {
        unsafe{ mem::transmute(self)}
    }
}

impl<T, C: Borrow<[T;3]>> Borrow<[T;4]> for AlphaColor<T,C> {
    fn borrow(&self) -> &[T;4] {
        unsafe{ mem::transmute(self)}
    }
}

impl<T, C: BorrowMut<[T;3]>> BorrowMut<[T;4]> for AlphaColor<T,C> {
    fn borrow_mut(&mut self) -> &mut [T;4] {
        unsafe{ mem::transmute(self)}
    }
}