palette/macros/
color_difference.rs
1macro_rules! impl_euclidean_distance {
2 (
3 $ty: ident
4 {$($component: ident),+}
5 $(where $($where: tt)+)?
6 ) => {
7 impl_euclidean_distance!($ty<> {$($component),+} $(where $($where)+)?);
9 };
10 (
11 $ty: ident <$($ty_param: ident),*>
12 {$($component: ident),+}
13 $(where $($where: tt)+)?
14 ) => {
15 impl<$($ty_param,)* T> crate::color_difference::EuclideanDistance for $ty<$($ty_param,)* T>
16 where
17 T: crate::num::Real + core::ops::Sub<T, Output=T> + core::ops::Add<T, Output=T> + core::ops::Mul<T, Output=T> + Clone,
18 $($($where)+)?
19 {
20 type Scalar = T;
21
22 #[inline]
23 fn distance_squared(self, other: Self) -> Self::Scalar {
24 let difference = self - other;
25 let difference_squared = difference.clone() * difference;
26
27 strip_plus!($(+ difference_squared.$component)+)
28 }
29 }
30 };
31}
32
33macro_rules! impl_hyab {
34 (
35 $ty: ident
36 {$($components: tt)+}
37 $(where $($where: tt)+)?
38 ) => {
39 impl_hyab!($ty<> {$($components)+} $(where $($where)+)?);
41 };
42 (
43 $ty: ident <$($ty_param: ident),*>
44 {lightness: $lightness:ident, chroma1: $chroma1:ident, chroma2: $chroma2:ident $(,)? }
45 $(where $($where: tt)+)?
46 ) => {
47 impl<$($ty_param,)* T> crate::color_difference::HyAb for $ty<$($ty_param,)* T>
48 where
49 T: crate::num::Real + crate::num::Abs + crate::num::Sqrt + core::ops::Sub<T, Output=T> + core::ops::Add<T, Output=T> + core::ops::Mul<T, Output=T> + Clone,
50 $($($where)+)?
51 {
52 type Scalar = T;
53
54 #[inline]
55 fn hybrid_distance(self, other: Self) -> Self::Scalar {
56 let lightness = self.$lightness - other.$lightness;
57 let chroma1 = self.$chroma1 - other.$chroma1;
58 let chroma2 = self.$chroma2 - other.$chroma2;
59
60 lightness.abs() + (chroma1.clone() * chroma1 + chroma2.clone() * chroma2).sqrt()
61 }
62 }
63 };
64}