palette/macros/
convert.rs

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
/// Check that traits for converting to and from XYZ have been implemented.
#[cfg(test)]
macro_rules! test_convert_into_from_xyz {
    ($ty:ty) => {
        #[test]
        fn convert_from_xyz() {
            use crate::FromColor;

            let _: $ty = <$ty>::from_color(crate::Xyz::<crate::white_point::D65, f32>::default());
        }

        #[test]
        fn convert_into_xyz() {
            use crate::FromColor;

            let _: crate::Xyz = crate::Xyz::from_color(<$ty>::default());
        }
    };
}

macro_rules! impl_tuple_conversion {
    ($ty: ident as ($($component_ty: ident),+)) => {
        impl_tuple_conversion!($ty<> as ($($component_ty),+));
    };
    ($ty: ident <$($ty_param: ident),*> as ($($component_ty: ident),+)) => {
        impl<$($ty_param,)* T> From<($($component_ty,)+)> for $ty<$($ty_param,)* T> {
            fn from(components: ($($component_ty,)+)) -> Self {
                Self::from_components(components)
            }
        }

        impl<$($ty_param,)* T> From<$ty<$($ty_param,)* T>> for ($($component_ty,)+) {
            fn from(color: $ty<$($ty_param,)* T>) -> ($($component_ty,)+) {
                color.into_components()
            }
        }

        impl<$($ty_param,)* T, A> From<($($component_ty,)+ A)> for crate::Alpha<$ty<$($ty_param,)* T>, A> {
            fn from(components: ($($component_ty,)+ A)) -> Self {
                Self::from_components(components)
            }
        }

        impl<$($ty_param,)* T, A> From<crate::Alpha<$ty<$($ty_param,)* T>, A>> for ($($component_ty,)+ A) {
            fn from(color: crate::Alpha<$ty<$($ty_param,)* T>, A>) -> ($($component_ty,)+ A) {
                color.into_components()
            }
        }
    };
}

macro_rules! __replace_generic_hue {
    (H, $hue_ty: ident) => {$hue_ty<T>};
    ($other: ident, $hue_ty: ident) => {$other};
}

macro_rules! impl_tuple_conversion_hue {
    ($ty: ident as ($($component_ty: ident),+), $hue_ty: ident) => {
        impl_tuple_conversion_hue!($ty<> as ($($component_ty),+), $hue_ty);
    };
    ($ty: ident <$($ty_param: ident),*> as ($($component_ty: ident),+), $hue_ty: ident) => {
        impl<$($ty_param,)* T, H: Into<$hue_ty<T>>> From<($($component_ty,)+)> for $ty<$($ty_param,)* T> {
            fn from(components: ($($component_ty,)+)) -> Self {
                Self::from_components(components)
            }
        }

        impl<$($ty_param,)* T> From<$ty<$($ty_param,)* T>> for ($(__replace_generic_hue!($component_ty, $hue_ty),)+) {
            fn from(color: $ty<$($ty_param,)* T>) -> ($(__replace_generic_hue!($component_ty, $hue_ty),)+) {
                color.into_components()
            }
        }

        impl<$($ty_param,)* T, H: Into<$hue_ty<T>>, A> From<($($component_ty,)+ A)> for crate::Alpha<$ty<$($ty_param,)* T>, A> {
            fn from(components: ($($component_ty,)+ A)) -> Self {
                Self::from_components(components)
            }
        }

        impl<$($ty_param,)* T, A> From<crate::Alpha<$ty<$($ty_param,)* T>, A>> for ($(__replace_generic_hue!($component_ty, $hue_ty),)+ A) {
            fn from(color: crate::Alpha<$ty<$($ty_param,)* T>, A>) -> ($(__replace_generic_hue!($component_ty, $hue_ty),)+ A) {
                color.into_components()
            }
        }
    };
}