palette/macros/
convert.rs

1/// Check that traits for converting to and from XYZ have been implemented.
2#[cfg(test)]
3macro_rules! test_convert_into_from_xyz {
4    ($ty:ty) => {
5        #[test]
6        fn convert_from_xyz() {
7            use crate::FromColor;
8
9            let _: $ty = <$ty>::from_color(crate::Xyz::<crate::white_point::D65, f32>::default());
10        }
11
12        #[test]
13        fn convert_into_xyz() {
14            use crate::FromColor;
15
16            let _: crate::Xyz = crate::Xyz::from_color(<$ty>::default());
17        }
18    };
19}
20
21macro_rules! impl_tuple_conversion {
22    ($ty: ident as ($($component_ty: ident),+)) => {
23        impl_tuple_conversion!($ty<> as ($($component_ty),+));
24    };
25    ($ty: ident <$($ty_param: ident),*> as ($($component_ty: ident),+)) => {
26        impl<$($ty_param,)* T> From<($($component_ty,)+)> for $ty<$($ty_param,)* T> {
27            fn from(components: ($($component_ty,)+)) -> Self {
28                Self::from_components(components)
29            }
30        }
31
32        impl<$($ty_param,)* T> From<$ty<$($ty_param,)* T>> for ($($component_ty,)+) {
33            fn from(color: $ty<$($ty_param,)* T>) -> ($($component_ty,)+) {
34                color.into_components()
35            }
36        }
37
38        impl<$($ty_param,)* T, A> From<($($component_ty,)+ A)> for crate::Alpha<$ty<$($ty_param,)* T>, A> {
39            fn from(components: ($($component_ty,)+ A)) -> Self {
40                Self::from_components(components)
41            }
42        }
43
44        impl<$($ty_param,)* T, A> From<crate::Alpha<$ty<$($ty_param,)* T>, A>> for ($($component_ty,)+ A) {
45            fn from(color: crate::Alpha<$ty<$($ty_param,)* T>, A>) -> ($($component_ty,)+ A) {
46                color.into_components()
47            }
48        }
49    };
50}
51
52macro_rules! __replace_generic_hue {
53    (H, $hue_ty: ident) => {$hue_ty<T>};
54    ($other: ident, $hue_ty: ident) => {$other};
55}
56
57macro_rules! impl_tuple_conversion_hue {
58    ($ty: ident as ($($component_ty: ident),+), $hue_ty: ident) => {
59        impl_tuple_conversion_hue!($ty<> as ($($component_ty),+), $hue_ty);
60    };
61    ($ty: ident <$($ty_param: ident),*> as ($($component_ty: ident),+), $hue_ty: ident) => {
62        impl<$($ty_param,)* T, H: Into<$hue_ty<T>>> From<($($component_ty,)+)> for $ty<$($ty_param,)* T> {
63            fn from(components: ($($component_ty,)+)) -> Self {
64                Self::from_components(components)
65            }
66        }
67
68        impl<$($ty_param,)* T> From<$ty<$($ty_param,)* T>> for ($(__replace_generic_hue!($component_ty, $hue_ty),)+) {
69            fn from(color: $ty<$($ty_param,)* T>) -> ($(__replace_generic_hue!($component_ty, $hue_ty),)+) {
70                color.into_components()
71            }
72        }
73
74        impl<$($ty_param,)* T, H: Into<$hue_ty<T>>, A> From<($($component_ty,)+ A)> for crate::Alpha<$ty<$($ty_param,)* T>, A> {
75            fn from(components: ($($component_ty,)+ A)) -> Self {
76                Self::from_components(components)
77            }
78        }
79
80        impl<$($ty_param,)* T, A> From<crate::Alpha<$ty<$($ty_param,)* T>, A>> for ($(__replace_generic_hue!($component_ty, $hue_ty),)+ A) {
81            fn from(color: crate::Alpha<$ty<$($ty_param,)* T>, A>) -> ($(__replace_generic_hue!($component_ty, $hue_ty),)+ A) {
82                color.into_components()
83            }
84        }
85    };
86}