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

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

///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<T>: Sized
    where T: Float,
{
    ///Convert from XYZ color space
    fn from_xyz(Xyz<T>) -> Self;

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

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

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

    ///Convert from RGB color space
    fn from_rgb(inp: Rgb<T>) -> Self {
        Self::from_xyz(inp.into_xyz())
    }

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

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

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

    ///Convert from Luma
    fn from_luma(inp: Luma<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<T>: Sized
    where T: Float,
{

    ///Convert into XYZ space
    fn into_xyz(self) -> Xyz<T>;

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

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

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

    ///Convert into RGB color space.
    fn into_rgb(self) -> Rgb<T> {
        Rgb::from_xyz(self.into_xyz())
    }

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

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

    ///Convert into HWB color space
    fn into_hwb(self) -> Hwb<T> {
        Hwb::from_hsv(self.into_hsv())
    }

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

}

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

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

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

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

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

            fn into_rgb(self) -> Rgb<T> {
                Rgb::$from_fn(self)
            }

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

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

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

        }

    }
}


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

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

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

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

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

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

            impl<T: Float> From<Alpha<$other<T>, T>> for $self_ty<T> {
                fn from(other: Alpha<$other<T>, T>) -> $self_ty<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});