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

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

///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 Rgb. So from_rgb conversion for Luma and
///from_luma for Rgb is implemented directly. The from for the same color must override
///the default. For example, from_rgb for Rgb 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<T>
{
    ///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(inp: Rgb<Wp, T>) -> Self {
        Self::from_xyz(inp.into_xyz())
    }

    ///Convert from HSL color space
    fn from_hsl(inp: Hsl<Wp, T>) -> Self {
        Self::from_rgb(inp.into_rgb())
    }

    ///Convert from HSV color space
    fn from_hsv(inp: Hsv<Wp, T>) -> Self {
        Self::from_rgb(inp.into_rgb())
    }

    ///Convert from HWB color space
    fn from_hwb(inp: Hwb<Wp, T>) -> Self {
        Self::from_hsv(inp.into_hsv())
    }

    ///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<T>
{

    ///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(self) -> Rgb<Wp, T> {
        Rgb::from_xyz(self.into_xyz())
    }

    ///Convert into HSL color space
    fn into_hsl(self) -> Hsl<Wp, T> {
        Hsl::from_rgb(self.into_rgb())
    }

    ///Convert into HSV color space
    fn into_hsv(self) -> Hsv<Wp, T> {
        Hsv::from_rgb(self.into_rgb())
    }

    ///Convert into HWB color space
    fn into_hwb(self) -> Hwb<Wp, T> {
        Hwb::from_hsv(self.into_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<T>
        {

            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(self) -> Rgb<Wp, T> {
                Rgb::$from_fn(self)
            }

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

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

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

        }

    }
}


macro_rules! impl_from_trait {
    (($self_ty: ident, $into_fn: ident) => {$($other: ident),+}) => (
        impl<Wp, T> From<Alpha<$self_ty<Wp, T>, T>> for $self_ty<Wp, T>
            where T: Float,
                Wp: WhitePoint<T>
        {
            fn from(color: Alpha<$self_ty<Wp, T>, T>) -> $self_ty<Wp, T> {
                color.color
            }
        }

        impl<Wp, T> From<Color<Wp, T>> for $self_ty<Wp, T>
            where T: Float,
                Wp: WhitePoint<T>
        {
            fn from(color: Color<Wp, T>) -> $self_ty<Wp, T> {
                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<Wp, T> From<Color<Wp, T>> for Alpha<$self_ty<Wp, T>,T>
            where T: Float,
                Wp: WhitePoint<T>
        {
            fn from(color: Color<Wp, T>) -> Alpha<$self_ty<Wp, T>,T> {
                Alpha {
                    color: color.into(),
                    alpha: T::one(),
                }
            }
        }

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

        $(
            impl<Wp, T> From<$other<Wp, T>> for $self_ty<Wp, T>
                where T: Float,
                    Wp: WhitePoint<T>
            {
                fn from(other: $other<Wp, T>) -> $self_ty<Wp, T> {
                    other.$into_fn()
                }
            }

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

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

            impl<Wp, T> From<Alpha<$other<Wp, T>, T>> for $self_ty<Wp, T>
                where T: Float,
                    Wp: WhitePoint<T>
            {
                fn from(other: Alpha<$other<Wp, T>, T>) -> $self_ty<Wp, T> {
                    other.color.$into_fn()
                }
            }

        )+
    )
}

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!(Rgb, from_rgb);
impl_into_color!(Hsl, from_hsl);
impl_into_color!(Hsv, from_hsv);
impl_into_color!(Hwb, from_hwb);
impl_into_color!(Luma ,from_luma);


impl_from_trait!((Xyz, into_xyz) => {Yxy, Lab, Lch, Rgb, Hsl, Hsv, Hwb, Luma});
impl_from_trait!((Yxy, into_yxy) => {Xyz, Lab, Lch, Rgb, Hsl, Hsv, Hwb, Luma});
impl_from_trait!((Lab, into_lab) => {Xyz, Yxy, Lch, Rgb, Hsl, Hsv, Hwb, Luma});
impl_from_trait!((Lch, into_lch) => {Xyz, Yxy, Lab, Rgb, Hsl, Hsv, Hwb, Luma});
impl_from_trait!((Rgb, into_rgb) => {Xyz, Yxy, Lab, Lch, Hsl, Hsv, Hwb, Luma});
impl_from_trait!((Hsl, into_hsl) => {Xyz, Yxy, Lab, Lch, Rgb, Hsv, Hwb, Luma});
impl_from_trait!((Hsv, into_hsv) => {Xyz, Yxy, Lab, Lch, Rgb, Hsl, Hwb, Luma});
impl_from_trait!((Hwb, into_hwb) => {Xyz, Yxy, Lab, Lch, Rgb, Hsl, Hsv, Luma});
impl_from_trait!((Luma, into_luma) => {Xyz, Yxy, Lab, Lch, Rgb, Hsl, Hsv, Hwb});