prisma 0.1.1

A color library for both simple and complex color manipulation, intending to be the go to rust color library for most tasks. It can handle conversion between a large number of color models, and can convert into the CIE device independent color spaces. Prisma tries to be easy to use while encouraging correct transformations, making mathematically correct conversions easy without knowing the whole field of color science.
Documentation
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
//! A wrapper type adding an alpha channel to other color types
#![allow(non_camel_case_types)]

use crate::channel::{
    AngularChannelScalar, ColorChannel, NormalChannelScalar, PosNormalBoundedChannel,
    PosNormalChannelScalar,
};
use crate::color::{
    Bounded, Broadcast, Color, Color3, Color4, Flatten, FromTuple, HomogeneousColor, Invert, Lerp,
    PolarColor,
};
use crate::convert::{FromColor, FromHsi, FromYCbCr};
use crate::encoding::EncodableColor;
use crate::hsi::{Hsi, HsiOutOfGamutMode};
use crate::tags::AlphaTag;
use crate::ycbcr::{YCbCr, YCbCrModel, YCbCrOutOfGamutMode};
use angle::{Angle, Deg};
#[cfg(feature = "approx")]
use approx;
use num_traits;
use std::fmt;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::slice;

use crate::lms::Lms;
use crate::{eHsi, Hsl, Hsv, Hwb, Lab, Lchab, Lchuv, Luv, Rgb, Rgi, XyY, Xyz};

/// A wrapper around a color with an alpha channel
///
/// `Alpha<T>` makes it easy to add an alpha channel to any other color and share code between
/// all color types. `Alpha<T>` implements `Deref` and `DerefMut`, making it able to act like the
/// underlying color in many situations.
#[repr(C)]
#[derive(Copy, Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct Alpha<T, InnerColor> {
    color: InnerColor,
    alpha: PosNormalBoundedChannel<T>,
}

impl<T, InnerColor> Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color,
{
    /// Construct an `Alpha` object from a color and an alpha value
    pub fn new(color: InnerColor, alpha: T) -> Self {
        Alpha {
            color,
            alpha: PosNormalBoundedChannel::new(alpha),
        }
    }
    /// Break apart an `Alpha` into the inner color and alpha channel value
    pub fn decompose(self) -> (InnerColor, T) {
        (self.color, self.alpha.0)
    }

    /// Returns a reference to the inner color
    pub fn color(&self) -> &InnerColor {
        &self.color
    }
    /// Returns the alpha scalar
    pub fn alpha(&self) -> T {
        self.alpha.0.clone()
    }
    /// Returns a mutable reference to the inner color
    pub fn color_mut(&mut self) -> &mut InnerColor {
        &mut self.color
    }
    /// Returns a mutable reference to the alpha scalar
    pub fn alpha_mut(&mut self) -> &mut T {
        &mut self.alpha.0
    }
    /// Set the inner color
    pub fn set_color(&mut self, color: InnerColor) {
        self.color = color;
    }
    /// Set the alpha channel value
    pub fn set_alpha(&mut self, alpha: T) {
        self.alpha.0 = alpha
    }
}

impl<T, InnerColor> Color for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color,
{
    type Tag = AlphaTag<InnerColor::Tag>;
    type ChannelsTuple = (InnerColor::ChannelsTuple, T);

    fn num_channels() -> u32 {
        InnerColor::num_channels() + 1
    }

    fn to_tuple(self) -> Self::ChannelsTuple {
        (self.color.to_tuple(), self.alpha.0)
    }
}

impl<T, InnerColor> Color4 for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color3,
{
}

impl<T, InnerColor> FromTuple for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + FromTuple,
{
    fn from_tuple(values: Self::ChannelsTuple) -> Self {
        Alpha::new(InnerColor::from_tuple(values.0), values.1)
    }
}

impl<T, InnerColor> Invert for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + Invert,
{
    fn invert(self) -> Self {
        Alpha {
            color: self.color.invert(),
            alpha: self.alpha.invert(),
        }
    }
}

impl<T, InnerColor> Lerp for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar + Lerp<Position = InnerColor::Position>,
    InnerColor: Color + Lerp,
{
    type Position = InnerColor::Position;

    fn lerp(&self, right: &Self, pos: Self::Position) -> Self {
        Alpha {
            color: self.color.lerp(&right.color, pos.clone()),
            alpha: self.alpha.lerp(&right.alpha, pos),
        }
    }
}

impl<T, InnerColor> Bounded for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + Bounded,
{
    fn normalize(self) -> Self {
        Alpha {
            color: self.color.normalize(),
            alpha: self.alpha.normalize(),
        }
    }
    fn is_normalized(&self) -> bool {
        self.color.is_normalized() && self.alpha.is_normalized()
    }
}

impl<T, InnerColor> HomogeneousColor for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + HomogeneousColor<ChannelFormat = T>,
{
    type ChannelFormat = T;
    fn clamp(self, min: T, max: T) -> Self {
        Alpha {
            color: self.color.clamp(min.clone(), max.clone()),
            alpha: self.alpha.clamp(min, max),
        }
    }
}

impl<T, InnerColor> Broadcast for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + HomogeneousColor<ChannelFormat = T> + Broadcast,
{
    fn broadcast(value: T) -> Self {
        Alpha {
            color: InnerColor::broadcast(value.clone()),
            alpha: PosNormalBoundedChannel::new(value),
        }
    }
}

impl<T, InnerColor> Flatten for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + Flatten + HomogeneousColor<ChannelFormat = T>,
{
    impl_color_as_slice!(T);

    fn from_slice(values: &[T]) -> Self {
        Alpha {
            color: InnerColor::from_slice(values),
            alpha: PosNormalBoundedChannel::new(values[Self::num_channels() as usize - 1].clone()),
        }
    }
}

impl<T, InnerColor> PolarColor for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + PolarColor<Cartesian = T>,
{
    type Angular = InnerColor::Angular;
    type Cartesian = InnerColor::Cartesian;
}

impl<T, InnerColor> EncodableColor for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: EncodableColor,
{
}

impl<T, InnerColor, InnerColor2> FromColor<Alpha<T, InnerColor2>> for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + FromColor<InnerColor2>,
    InnerColor2: Color,
{
    fn from_color(from: &Alpha<T, InnerColor2>) -> Self {
        Alpha::new(InnerColor::from_color(from.color()), from.alpha())
    }
}
impl<T, InnerColor, A> FromHsi<Alpha<T, Hsi<T, A>>> for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color + FromHsi<Hsi<T, A>>,
    A: AngularChannelScalar + Angle,
{
    fn from_hsi(from: &Alpha<T, Hsi<T, A>>, out_of_gamut_mode: HsiOutOfGamutMode) -> Self {
        Alpha::new(
            InnerColor::from_hsi(from.color(), out_of_gamut_mode),
            from.alpha(),
        )
    }
}
impl<T, InnerColor, M> FromYCbCr<Alpha<T, YCbCr<T, M>>> for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar + NormalChannelScalar,
    InnerColor: Color + FromYCbCr<YCbCr<T, M>>,
    M: YCbCrModel<T>,
{
    fn from_ycbcr(from: &Alpha<T, YCbCr<T, M>>, out_of_gamut_mode: YCbCrOutOfGamutMode) -> Self {
        Alpha::new(
            InnerColor::from_ycbcr(from.color(), out_of_gamut_mode),
            from.alpha(),
        )
    }
}

impl<T, InnerColor> Deref for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color,
{
    type Target = InnerColor;
    fn deref(&self) -> &InnerColor {
        &self.color
    }
}

impl<T, InnerColor> DerefMut for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar,
    InnerColor: Color,
{
    fn deref_mut(&mut self) -> &mut InnerColor {
        &mut self.color
    }
}

#[cfg(feature = "approx")]
impl<T, InnerColor> approx::AbsDiffEq for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar + approx::AbsDiffEq<Epsilon = InnerColor::Epsilon>,
    InnerColor: Color + approx::AbsDiffEq,
    InnerColor::Epsilon: Clone + num_traits::Float,
{
    impl_abs_diff_eq!({color, alpha});
}
#[cfg(feature = "approx")]
impl<T, InnerColor> approx::RelativeEq for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar + approx::RelativeEq<Epsilon = InnerColor::Epsilon>,
    InnerColor: Color + approx::RelativeEq,
    InnerColor::Epsilon: Clone + num_traits::Float,
{
    impl_rel_eq!({color, alpha});
}
#[cfg(feature = "approx")]
impl<T, InnerColor> approx::UlpsEq for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar + approx::UlpsEq<Epsilon = InnerColor::Epsilon>,
    InnerColor: Color + approx::UlpsEq,
    InnerColor::Epsilon: Clone + num_traits::Float,
{
    impl_ulps_eq!({color, alpha});
}

impl<T, InnerColor> Default for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar + Default + num_traits::Zero,
    InnerColor: Color + Default + num_traits::Zero,
{
    fn default() -> Self {
        Alpha {
            color: InnerColor::default(),
            alpha: PosNormalBoundedChannel::default(),
        }
    }
}

impl<T, InnerColor> fmt::Display for Alpha<T, InnerColor>
where
    T: PosNormalChannelScalar + fmt::Display,
    InnerColor: Color + fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Alpha({}, {})", self.color, self.alpha)
    }
}

/// An `Rgb` value with an alpha channel
pub type Rgba<T> = Alpha<T, Rgb<T>>;
/// An `Rgi` value with an alpha channel
pub type Rgia<T> = Alpha<T, Rgi<T>>;
/// An `Hsl` value with an alpha channel
pub type Hsla<T, A> = Alpha<T, Hsl<T, A>>;
/// An `Hsv` value with an alpha channel
pub type Hsva<T, A> = Alpha<T, Hsv<T, A>>;
/// An `Hwb` value with an alpha channel
pub type Hwba<T, A> = Alpha<T, Hwb<T, A>>;
/// An `Hsi` value with an alpha channel
pub type Hsia<T, A> = Alpha<T, Hsi<T, A>>;
/// An `eHsi` value with an alpha channel
pub type eHsia<T, A> = Alpha<T, eHsi<T, A>>;
/// An `YCbCr` value with an alpha channel
pub type YCbCra<T, M> = Alpha<T, YCbCr<T, M>>;
/// An `Xyz` value with an alpha channel
pub type Xyza<T> = Alpha<T, Xyz<T>>;
/// An `XyY` value with an alpha channel
pub type XyYa<T> = Alpha<T, XyY<T>>;
/// An `Lab` value with an alpha channel
pub type Laba<T, W> = Alpha<T, Lab<T, W>>;
/// An `Luv` value with an alpha channel
pub type Luva<T, W> = Alpha<T, Luv<T, W>>;
/// An `Lchab` value with an alpha channel
pub type Lchaba<T, W, A = Deg<T>> = Alpha<T, Lchab<T, W, A>>;
/// An `Lchuv` value with an alpha channel
pub type Lchauv<T, W, A = Deg<T>> = Alpha<T, Lchuv<T, W, A>>;
/// An `Lmsa` value with an alpha channel
pub type Lmsa<T, M> = Alpha<T, Lms<T, M>>;

#[cfg(test)]
mod test {
    use super::*;
    use crate::rgb::*;
    use approx::*;

    #[test]
    fn test_construct() {
        let c1 = Rgba::new(Rgb::new(30u8, 120u8, 255u8), 222u8);
        assert_eq!(c1.alpha(), 222u8);
        assert_eq!(c1.color().red(), 30u8);
        assert_eq!(c1.color().green(), 120u8);
        assert_eq!(c1.color().blue(), 255u8);
        let (ic1, a) = c1.to_tuple();
        assert_eq!(ic1, (30u8, 120, 255));
        assert_eq!(a, 222u8);

        let mut c2 = Hsva::new(Hsv::new(Deg(0.3f32), 0.66, 0.9), 0.25f32);
        assert_eq!(c2.alpha(), 0.25f32);
        assert_ulps_eq!(*c2.color(), Hsv::new(Deg(0.3f32), 0.66, 0.9));
        assert_eq!(c2, Hsva::from_tuple(((Deg(0.3f32), 0.66f32, 0.9), 0.25)));
        *c2.alpha_mut() = 0.75;
        *c2.color_mut().saturation_mut() = 0.01;
        assert_ulps_eq!(c2, Hsva::new(Hsv::new(Deg(0.3f32), 0.01, 0.9), 0.75f32));

        let (c, a) = c2.clone().decompose();
        assert_eq!(c, *c2.color());
        assert_eq!(a, c2.alpha());

        let c3 = Rgba::broadcast(50u8);
        assert_eq!(c3, Rgba::from_tuple(((50u8, 50, 50), 50)));

        let c4 = Rgba::new(Rgb::new(0.2, 0.6, 0.99), 0.05);
        assert_relative_eq!(
            c4.clamp(0.25, 0.75),
            Rgba::new(Rgb::new(0.25, 0.6, 0.75), 0.25)
        );
    }

    #[test]
    fn test_invert() {
        let c1 = Rgba::new(Rgb::new(30u8, 255u8, 200u8), 155u8);
        assert_eq!(c1.clone().invert().invert(), c1);
        assert_eq!(c1.invert(), Rgba::new(Rgb::new(225u8, 0, 55), 100u8));

        let c2 = Hsva::new(Hsv::new(Deg(120.0f32), 0.3f32, 0.85), 0.3f32);
        assert_relative_eq!(c2.clone().invert().invert(), c2, epsilon = 1e-6);
        assert_relative_eq!(
            c2.invert(),
            Hsva::new(Hsv::new(Deg(300.0f32), 0.7f32, 0.15), 0.7f32),
            epsilon = 1e-4
        );
    }

    #[test]
    fn test_lerp() {
        let c1 = Rgba::new(Rgb::new(120u8, 200, 0), 150);
        let c2 = Rgba::new(Rgb::new(250u8, 100, 220), 55);
        assert_eq!(c1.lerp(&c2, 0.0), c1);
        assert_eq!(c1.lerp(&c2, 1.0), c2);
        assert_eq!(c1.lerp(&c2, 0.5), Rgba::new(Rgb::new(185u8, 150, 110), 102));

        let c3 = Hsva::new(Hsv::new(Deg(60.0), 0.25, 0.55), 0.95);
        let c4 = Hsva::new(Hsv::new(Deg(340.0), 0.95, 0.0), 0.25);
        assert_relative_eq!(c3.lerp(&c4, 0.0), c3);
        assert_relative_eq!(c3.lerp(&c4, 1.0), c4);
        assert_relative_eq!(
            c3.lerp(&c4, 0.25),
            Hsva::new(Hsv::new(Deg(40.0), 0.425, 0.41250), 0.7750)
        );
    }

    #[test]
    fn test_flatten() {
        let c1 = Rgba::new(Rgb::new(100u8, 50, 175), 254);
        assert_eq!(c1.as_slice(), &[100u8, 50, 175, 254]);
        assert_eq!(Rgba::from_slice(c1.as_slice()), c1);
    }

    #[test]
    fn test_deref() {
        let mut c1 = Rgba::new(Rgb::new(50, 250, 0u8), 100u8);

        assert_eq!(c1.red(), 50);
        assert_eq!(c1.green(), 250);
        assert_eq!(c1.blue(), 0);
        assert_eq!(c1.alpha(), 100);

        c1.set_red(100);
        assert_eq!(c1.red(), 100);
    }
}