palette/macros/
mix.rs

1macro_rules! impl_mix {
2    ($ty: ident $(where $($where: tt)+)?) => {
3        impl_mix!($ty<> $(where $($where)+)?);
4    };
5    ($ty: ident <$($ty_param: ident),*> $(where $($where: tt)+)?) => {
6        impl<$($ty_param,)* T> crate::Mix for $ty<$($ty_param,)* T>
7        where
8            T: crate::num::Real
9                + crate::num::Zero
10                + crate::num::One
11                + crate::num::Arithmetics
12                + crate::num::Clamp
13                + Clone,
14            $($($where)+)?
15        {
16            type Scalar = T;
17
18            #[inline]
19            fn mix(self, other: Self, factor: T) -> Self {
20                let factor = crate::clamp(factor, T::zero(), T::one());
21                self.clone() + (other - self) * factor
22            }
23        }
24
25        impl<$($ty_param,)* T> crate::MixAssign for $ty<$($ty_param,)* T>
26        where
27            T: crate::num::Real
28                + crate::num::Zero
29                + crate::num::One
30                + core::ops::AddAssign
31                + crate::num::Arithmetics
32                + crate::num::Clamp
33                + Clone,
34            $($($where)+)?
35        {
36            type Scalar = T;
37
38            #[inline]
39            fn mix_assign(&mut self, other: Self, factor: T) {
40                let factor = crate::clamp(factor, T::zero(), T::one());
41                *self += (other - self.clone()) * factor;
42            }
43        }
44    };
45}
46
47macro_rules! impl_mix_hue {
48    ($ty: ident {$($other_field: ident),*} $(phantom: $phantom: ident)?) => {
49        impl_mix_hue!($ty<> {$($other_field),*} $(phantom: $phantom)?);
50    };
51    ($ty: ident <$($ty_param: ident),*> {$($other_field: ident),*} $(phantom: $phantom: ident)?) => {
52        impl<$($ty_param,)* T> crate::Mix for $ty<$($ty_param,)* T>
53        where
54            T: crate::angle::RealAngle
55                + crate::angle::SignedAngle
56                + crate::num::Zero
57                + crate::num::One
58                + crate::num::Clamp
59                + crate::num::Arithmetics
60                + Clone,
61        {
62            type Scalar = T;
63
64            #[inline]
65            fn mix(self, other: Self, factor: T) -> Self {
66                let factor = crate::clamp(factor, T::zero(), T::one());
67                let hue = (other.hue - self.hue.clone()).into_degrees();
68                $(
69                    let $other_field = other.$other_field - &self.$other_field;
70                )*
71
72                $ty {
73                    $(
74                        $other_field: self.$other_field + $other_field * &factor,
75                    )*
76                    hue: self.hue + hue * factor,
77                    $($phantom: PhantomData)?
78                }
79            }
80        }
81
82        impl<$($ty_param,)* T> crate::MixAssign for $ty<$($ty_param,)* T>
83        where
84            T: crate::angle::RealAngle
85                + crate::angle::SignedAngle
86                + crate::num::Zero
87                + crate::num::One
88                + crate::num::Clamp
89                + core::ops::AddAssign
90                + crate::num::Arithmetics
91                + Clone,
92        {
93            type Scalar = T;
94
95            #[inline]
96            fn mix_assign(&mut self, other: Self, factor: T) {
97                let factor = crate::clamp(factor, T::zero(), T::one());
98                let hue = (other.hue - self.hue.clone()).into_degrees();
99                $(
100                    let $other_field = other.$other_field - &self.$other_field;
101                )*
102
103                $(
104                    self.$other_field += $other_field * &factor;
105                )*
106                self.hue += hue * factor;
107            }
108        }
109    };
110}