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
use num_traits::Float;

use {Alpha, Color, Hsl, Hsv, Hwb, Lab, Lch, Luma, Xyz, Yxy};
use white_point::{D65, WhitePoint};
use rgb::{Linear, Rgb, RgbSpace, RgbStandard};

///FromColor provides conversion between the colors.
///
///It requires from_xyz and derives conversion to other colors as a default from this.
///These defaults must be overridden when direct conversion exists between colors.
///For example, Luma has direct conversion to LinRgb. So from_rgb conversion for Luma and
///from_luma for LinRgb is implemented directly. The from for the same color must override
///the default. For example, from_rgb for LinRgb will convert via Xyz which needs to be overridden
///with self to avoid the unnecessary converison.
pub trait FromColor<Wp = D65, T = f32>: Sized
where
    T: Float,
    Wp: WhitePoint,
{
    ///Convert from XYZ color space
    fn from_xyz(Xyz<Wp, T>) -> Self;

    ///Convert from Yxy color space
    fn from_yxy(inp: Yxy<Wp, T>) -> Self {
        Self::from_xyz(inp.into_xyz())
    }

    ///Convert from L\*a\*b\* color space
    fn from_lab(inp: Lab<Wp, T>) -> Self {
        Self::from_xyz(inp.into_xyz())
    }

    ///Convert from L\*C\*h° color space
    fn from_lch(inp: Lch<Wp, T>) -> Self {
        Self::from_lab(inp.into_lab())
    }

    ///Convert from RGB color space
    fn from_rgb<S: RgbSpace<WhitePoint = Wp>>(inp: Rgb<Linear<S>, T>) -> Self {
        Self::from_xyz(inp.into_xyz())
    }

    ///Convert from HSL color space
    fn from_hsl<S: RgbSpace<WhitePoint = Wp>>(inp: Hsl<S, T>) -> Self {
        Self::from_rgb(Rgb::<Linear<S>, T>::from_hsl(inp))
    }

    ///Convert from HSV color space
    fn from_hsv<S: RgbSpace<WhitePoint = Wp>>(inp: Hsv<S, T>) -> Self {
        Self::from_rgb(Rgb::<Linear<S>, T>::from_hsv(inp))
    }

    ///Convert from HWB color space
    fn from_hwb<S: RgbSpace<WhitePoint = Wp>>(inp: Hwb<S, T>) -> Self {
        Self::from_hsv(Hsv::<S, T>::from_hwb(inp))
    }

    ///Convert from Luma
    fn from_luma(inp: Luma<Wp, T>) -> Self {
        Self::from_xyz(inp.into_xyz())
    }
}

///IntoColor provides conversion between the colors.
///
///It requires into into_xyz and derives conversion to other colors as a default from this.
///These defaults must be overridden when direct conversion exists between colors.
pub trait IntoColor<Wp = D65, T = f32>: Sized
where
    T: Float,
    Wp: WhitePoint,
{
    ///Convert into XYZ space
    fn into_xyz(self) -> Xyz<Wp, T>;

    ///Convert into Yxy color space
    fn into_yxy(self) -> Yxy<Wp, T> {
        Yxy::from_xyz(self.into_xyz())
    }

    ///Convert into L\*a\*b\* color space
    fn into_lab(self) -> Lab<Wp, T> {
        Lab::from_xyz(self.into_xyz())
    }

    ///Convert into L\*C\*h° color space
    fn into_lch(self) -> Lch<Wp, T> {
        Lch::from_lab(self.into_lab())
    }

    ///Convert into RGB color space.
    fn into_rgb<S: RgbSpace<WhitePoint = Wp>>(self) -> Rgb<Linear<S>, T> {
        Rgb::from_xyz(self.into_xyz())
    }

    ///Convert into HSL color space
    fn into_hsl<S: RgbSpace<WhitePoint = Wp>>(self) -> Hsl<S, T> {
        let rgb: Rgb<Linear<S>, T> = self.into_rgb();
        Hsl::from_rgb(rgb)
    }

    ///Convert into HSV color space
    fn into_hsv<S: RgbSpace<WhitePoint = Wp>>(self) -> Hsv<S, T> {
        let rgb: Rgb<Linear<S>, T> = self.into_rgb();
        Hsv::from_rgb(rgb)
    }

    ///Convert into HWB color space
    fn into_hwb<S: RgbSpace<WhitePoint = Wp>>(self) -> Hwb<S, T> {
        let hsv: Hsv<S, T> = self.into_hsv();
        Hwb::from_hsv(hsv)
    }

    ///Convert into Luma
    fn into_luma(self) -> Luma<Wp, T> {
        Luma::from_xyz(self.into_xyz())
    }
}

macro_rules! impl_into_color {
    ($self_ty:ident, $from_fn: ident) => {
        impl<Wp, T> IntoColor<Wp, T> for $self_ty<Wp, T>
            where T: Float,
             Wp: WhitePoint
        {

            fn into_xyz(self) -> Xyz<Wp, T> {
                Xyz::$from_fn(self)
            }

            fn into_yxy(self) -> Yxy<Wp, T> {
                Yxy::$from_fn(self)
            }

            fn into_lab(self) -> Lab<Wp, T> {
                Lab::$from_fn(self)
            }

            fn into_lch(self) -> Lch<Wp, T> {
                Lch::$from_fn(self)
            }

            fn into_rgb<S: RgbSpace<WhitePoint=Wp>>(self) -> Rgb<Linear<S>, T> {
                Rgb::$from_fn(self)
            }

            fn into_hsl<S: RgbSpace<WhitePoint=Wp>>(self) -> Hsl<S, T> {
                Hsl::$from_fn(self)
            }

            fn into_hsv<S: RgbSpace<WhitePoint=Wp>>(self) -> Hsv<S, T> {
                Hsv::$from_fn(self)
            }

            fn into_luma(self) -> Luma<Wp, T> {
                Luma::$from_fn(self)
            }

        }

    }
}

macro_rules! impl_into_color_rgb {
    ($self_ty:ident, $from_fn: ident) => {
        impl<S, Wp, T> IntoColor<Wp, T> for $self_ty<S, T> where
            T: Float,
            Wp: WhitePoint,
            S: RgbSpace<WhitePoint=Wp>,
        {

            fn into_xyz(self) -> Xyz<Wp, T> {
                Xyz::$from_fn(self)
            }

            fn into_yxy(self) -> Yxy<Wp, T> {
                Yxy::$from_fn(self)
            }

            fn into_lab(self) -> Lab<Wp, T> {
                Lab::$from_fn(self)
            }

            fn into_lch(self) -> Lch<Wp, T> {
                Lch::$from_fn(self)
            }

            fn into_rgb<Sp: RgbSpace<WhitePoint=Wp>>(self) -> Rgb<Linear<Sp>, T> {
                Rgb::$from_fn(self)
            }

            fn into_hsl<Sp: RgbSpace<WhitePoint=Wp>>(self) -> Hsl<Sp, T> {
                Hsl::$from_fn(self)
            }

            fn into_hsv<Sp: RgbSpace<WhitePoint=Wp>>(self) -> Hsv<Sp, T> {
                Hsv::$from_fn(self)
            }

            fn into_luma(self) -> Luma<Wp, T> {
                Luma::$from_fn(self)
            }

        }

    }
}

macro_rules! impl_from_trait {
    (<$s:ident, $t:ident>($self_ty: ty, $into_fn: ident) => {$($other: ty),+}) => (
        impl<$s, $t> From<Color<$s, $t>> for $self_ty
            where $t: Float,
                $s: RgbSpace
        {
            fn from(color: Color<$s, $t>) -> $self_ty {
                match color {
                    Color::Luma(c) => c.$into_fn(),
                    Color::Rgb(c) => c.$into_fn(),
                    Color::Xyz(c) => c.$into_fn(),
                    Color::Yxy(c) => c.$into_fn(),
                    Color::Lab(c) => c.$into_fn(),
                    Color::Lch(c) => c.$into_fn(),
                    Color::Hsv(c) => c.$into_fn(),
                    Color::Hsl(c) => c.$into_fn(),
                    Color::Hwb(c) => c.$into_fn(),
                }
            }
        }

        impl<$s, $t> From<Color<$s, $t>> for Alpha<$self_ty,$t>
            where $t: Float,
                $s: RgbSpace
        {
            fn from(color: Color<$s, $t>) -> Alpha<$self_ty,$t> {
                Alpha {
                    color: color.into(),
                    alpha: $t::one(),
                }
            }
        }

        impl<$s, $t> From<Alpha<Color<$s, $t>, $t>> for $self_ty
            where $t: Float,
                $s: RgbSpace
        {
            fn from(color: Alpha<Color<$s, $t>, $t>) -> $self_ty {
                color.color.into()
            }
        }

        impl<$s, $t> From<Alpha<Color<$s, $t>, $t>> for Alpha<$self_ty,$t>
            where $t: Float,
                $s: RgbSpace
        {
            fn from(color: Alpha<Color<$s, $t>, $t>) -> Alpha<$self_ty,$t> {
                Alpha {
                    color: color.color.into(),
                    alpha: color.alpha,
                }
            }
        }

        $(
            impl<$s, $t> From<$other> for $self_ty
                where $t: Float,
                    $s: RgbSpace
            {
                fn from(other: $other) -> $self_ty {
                    other.$into_fn()
                }
            }

            impl<$s, $t> From<Alpha<$other, $t>> for Alpha<$self_ty, $t>
                where $t: Float,
                    $s: RgbSpace
            {
                fn from(other: Alpha<$other, $t>) -> Alpha<$self_ty, $t> {
                    Alpha {
                        color: other.color.$into_fn(),
                        alpha: other.alpha,
                    }
                }
            }

            impl<$s, $t> From<$other> for Alpha<$self_ty, $t>
                where $t: Float,
                    $s: RgbSpace
            {
                fn from(color: $other) -> Alpha<$self_ty, $t> {
                    Alpha {
                        color: color.$into_fn(),
                        alpha: $t::one(),
                    }
                }
            }

            impl<$s, $t> From<Alpha<$other, $t>> for $self_ty
                where $t: Float,
                    $s: RgbSpace
            {
                fn from(other: Alpha<$other, $t>) -> $self_ty {
                    other.color.$into_fn()
                }
            }

        )+
    )
}

macro_rules! impl_from_trait_other {
    (<$s:ident : $s_ty:ident, $t:ident>($self_ty: ty, |$into_ident:ident| $into_expr:expr) => {$($other: ty),+}) => (
        $(
            impl<$s, $t> From<$other> for $self_ty
                where $t: Float,
                    $s: $s_ty
            {
                fn from(other: $other) -> $self_ty {
                    let $into_ident = other;
                    $into_expr
                }
            }

            impl<$s, $t> From<Alpha<$other, $t>> for Alpha<$self_ty, $t>
                where $t: Float,
                    $s: $s_ty
            {
                fn from(other: Alpha<$other, $t>) -> Alpha<$self_ty, $t> {
                    let $into_ident = other.color;
                    Alpha {
                        color: $into_expr,
                        alpha: other.alpha,
                    }
                }
            }

            impl<$s, $t> From<$other> for Alpha<$self_ty, $t>
                where $t: Float,
                    $s: $s_ty
            {
                fn from(color: $other) -> Alpha<$self_ty, $t> {
                    let $into_ident = color;
                    Alpha {
                        color: $into_expr,
                        alpha: $t::one(),
                    }
                }
            }

            impl<$s, $t> From<Alpha<$other, $t>> for $self_ty
                where $t: Float,
                    $s: $s_ty
            {
                fn from(other: Alpha<$other, $t>) -> $self_ty {
                    let $into_ident = other.color;
                    $into_expr
                }
            }

        )+
    );


    (<$s:ident : $s_ty:ident, $t:ident>($self_ty: ty, $into_fn: ident) => {$($other: ty),+}) => (
        impl_from_trait_other!(<$s: $s_ty, $t>($self_ty, |a| a.$into_fn()) => {$($other),+});
    );
}

impl_into_color!(Xyz, from_xyz);
impl_into_color!(Yxy, from_yxy);
impl_into_color!(Lab, from_lab);
impl_into_color!(Lch, from_lch);
impl_into_color!(Luma, from_luma);
impl_into_color_rgb!(Hsl, from_hsl);
impl_into_color_rgb!(Hsv, from_hsv);
impl_into_color_rgb!(Hwb, from_hwb);

impl_from_trait!(<S, T> (Xyz<S::WhitePoint, T>, into_xyz) => {Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Yxy<S::WhitePoint, T>, into_yxy) => {Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Lab<S::WhitePoint, T>, into_lab) => {Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Lch<S::WhitePoint, T>, into_lch) => {Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});
impl_from_trait!(<S, T> (Luma<S::WhitePoint, T>, into_luma) => {Hsl<S, T>, Hsv<S, T>, Hwb<S, T>});

impl_from_trait!(<S, T> (Rgb<Linear<S>, T>, into_rgb) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, Hsl<S, T>, Hsv<S, T>, Hwb<S, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hsl<S, T>, into_hsl) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, Hsv<S, T>, Hwb<S, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hsv<S, T>, into_hsv) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, Hsl<S, T>, Hwb<S, T>, Luma<S::WhitePoint, T>});
impl_from_trait!(<S, T> (Hwb<S, T>, into_hwb) => {Xyz<S::WhitePoint, T>, Yxy<S::WhitePoint, T>, Lab<S::WhitePoint, T>, Lch<S::WhitePoint, T>, Hsl<S, T>, Hsv<S, T>, Luma<S::WhitePoint, T>});

impl_from_trait_other!(<Wp: WhitePoint, T> (Xyz<Wp, T>, into_xyz) => {Yxy<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Yxy<Wp, T>, into_yxy) => {Xyz<Wp, T>, Lab<Wp, T>, Lch<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Lab<Wp, T>, into_lab) => {Xyz<Wp, T>, Yxy<Wp, T>, Lch<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Lch<Wp, T>, into_lch) => {Xyz<Wp, T>, Yxy<Wp, T>, Lab<Wp, T>, Luma<Wp, T>});
impl_from_trait_other!(<Wp: WhitePoint, T> (Luma<Wp, T>, into_luma) => {Xyz<Wp, T>, Yxy<Wp, T>, Lab<Wp, T>, Lch<Wp, T>});

impl_from_trait_other!(<S: RgbStandard, T> (Xyz<<S::Space as RgbSpace>::WhitePoint, T>, into_xyz) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Yxy<<S::Space as RgbSpace>::WhitePoint, T>, into_yxy) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Lab<<S::Space as RgbSpace>::WhitePoint, T>, into_lab) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Lch<<S::Space as RgbSpace>::WhitePoint, T>, into_lch) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Hsl<S::Space, T>, into_hsl) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Hsv<S::Space, T>, into_hsv) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Hwb<S::Space, T>, into_hwb) => {Rgb<S, T>});
impl_from_trait_other!(<S: RgbStandard, T> (Luma<<S::Space as RgbSpace>::WhitePoint, T>, into_luma) => {Rgb<S, T>});