palette/macros/
arithmetics.rs

1macro_rules! impl_color_add {
2    ($self_ty: ident , [$($element: ident),+]) => {
3        impl_color_add!($self_ty<>, [$($element),+]);
4    };
5    ($self_ty: ident < $($ty_param: ident),* > , [$($element: ident),+] $(, $phantom: ident)?) => {
6        impl<$($ty_param,)* T> core::ops::Add<Self> for $self_ty<$($ty_param,)* T>
7        where
8            T: core::ops::Add<Output=T>
9        {
10            type Output = Self;
11
12            fn add(self, other: Self) -> Self::Output {
13                $self_ty {
14                    $($element: self.$element + other.$element,)+
15                    $($phantom: core::marker::PhantomData,)?
16                }
17            }
18        }
19
20        impl<$($ty_param,)* T> core::ops::Add<T> for $self_ty<$($ty_param,)* T>
21        where
22            T: core::ops::Add<Output=T> + Clone
23        {
24            type Output = Self;
25
26            fn add(self, c: T) -> Self::Output {
27                $self_ty {
28                    $($element: self.$element + c.clone(),)+
29                    $($phantom: core::marker::PhantomData,)?
30                }
31            }
32        }
33
34        impl<$($ty_param,)* T> core::ops::AddAssign<Self> for $self_ty<$($ty_param,)* T>
35        where
36            T: core::ops::AddAssign,
37        {
38            fn add_assign(&mut self, other: Self) {
39                $( self.$element += other.$element; )+
40            }
41        }
42
43        impl<$($ty_param,)* T> core::ops::AddAssign<T> for $self_ty<$($ty_param,)* T>
44        where
45            T: core::ops::AddAssign + Clone
46        {
47            fn add_assign(&mut self, c: T) {
48                $( self.$element += c.clone(); )+
49            }
50        }
51
52        impl<$($ty_param,)* T> $crate::num::SaturatingAdd<Self> for $self_ty<$($ty_param,)* T>
53        where
54            T: $crate::num::SaturatingAdd<Output=T>
55        {
56            type Output = Self;
57
58            fn saturating_add(self, other: Self) -> Self::Output {
59                $self_ty {
60                    $($element: self.$element.saturating_add(other.$element),)+
61                    $($phantom: core::marker::PhantomData,)?
62                }
63            }
64        }
65
66        impl<$($ty_param,)* T> $crate::num::SaturatingAdd<T> for $self_ty<$($ty_param,)* T>
67        where
68            T: $crate::num::SaturatingAdd<Output=T> + Clone
69        {
70            type Output = Self;
71
72            fn saturating_add(self, c: T) -> Self::Output {
73                $self_ty {
74                    $($element: self.$element.saturating_add(c.clone()),)+
75                    $($phantom: core::marker::PhantomData,)?
76                }
77            }
78        }
79    };
80}
81
82/// Implement `Sub` and `SubAssign` traits for a color space.
83///
84/// Both scalars and color arithmetic are implemented.
85macro_rules! impl_color_sub {
86    ($self_ty: ident , [$($element: ident),+]) => {
87        impl_color_sub!($self_ty<>, [$($element),+]);
88    };
89    ($self_ty: ident < $($ty_param: ident),* > , [$($element: ident),+] $(, $phantom: ident)?) => {
90        impl<$($ty_param,)* T> core::ops::Sub<Self> for $self_ty<$($ty_param,)* T>
91        where
92            T: core::ops::Sub<Output=T>
93        {
94            type Output = Self;
95
96            fn sub(self, other: Self) -> Self::Output {
97                $self_ty {
98                    $($element: self.$element - other.$element,)+
99                    $($phantom: core::marker::PhantomData,)?
100                }
101            }
102        }
103
104        impl<$($ty_param,)* T> core::ops::Sub<T> for $self_ty<$($ty_param,)* T>
105        where
106            T: core::ops::Sub<Output=T> + Clone
107        {
108            type Output = Self;
109
110            fn sub(self, c: T) -> Self::Output {
111                $self_ty {
112                    $($element: self.$element - c.clone(),)+
113                    $($phantom: core::marker::PhantomData,)?
114                }
115            }
116        }
117
118        impl<$($ty_param,)* T> core::ops::SubAssign<Self> for $self_ty<$($ty_param,)* T>
119        where
120            T: core::ops::SubAssign,
121        {
122            fn sub_assign(&mut self, other: Self) {
123                $( self.$element -= other.$element; )+
124            }
125        }
126
127        impl<$($ty_param,)* T> core::ops::SubAssign<T> for $self_ty<$($ty_param,)* T>
128        where
129            T:  core::ops::SubAssign + Clone
130        {
131            fn sub_assign(&mut self, c: T) {
132                $( self.$element -= c.clone(); )+
133            }
134        }
135
136        impl<$($ty_param,)* T> $crate::num::SaturatingSub<Self> for $self_ty<$($ty_param,)* T>
137        where
138            T: $crate::num::SaturatingSub<Output=T>
139        {
140            type Output = Self;
141
142            fn saturating_sub(self, other: Self) -> Self::Output {
143                $self_ty {
144                    $($element: self.$element.saturating_sub(other.$element),)+
145                    $($phantom: core::marker::PhantomData,)?
146                }
147            }
148        }
149
150        impl<$($ty_param,)* T> $crate::num::SaturatingSub<T> for $self_ty<$($ty_param,)* T>
151        where
152            T: $crate::num::SaturatingSub<Output=T> + Clone
153        {
154            type Output = Self;
155
156            fn saturating_sub(self, c: T) -> Self::Output {
157                $self_ty {
158                    $($element: self.$element.saturating_sub(c.clone()),)+
159                    $($phantom: core::marker::PhantomData,)?
160                }
161            }
162        }
163    };
164}
165
166/// Implement `Mul` and `MulAssign` traits for a color space.
167///
168/// Both scalars and color arithmetic are implemented.
169macro_rules! impl_color_mul {
170    ($self_ty: ident , [$($element: ident),+]) => {
171        impl_color_mul!($self_ty<>, [$($element),+]);
172    };
173    ($self_ty: ident < $($ty_param: ident),* > , [$($element: ident),+] $(, $phantom: ident)?) => {
174        impl<$($ty_param,)* T> core::ops::Mul<Self> for $self_ty<$($ty_param,)* T>
175        where
176            T: core::ops::Mul<Output=T>
177        {
178            type Output = Self;
179
180            fn mul(self, other: Self) -> Self::Output {
181                $self_ty {
182                    $($element: self.$element * other.$element,)+
183                    $($phantom: core::marker::PhantomData,)?
184                }
185            }
186        }
187
188        impl<$($ty_param,)* T> core::ops::Mul<T> for $self_ty<$($ty_param,)* T>
189        where
190            T: core::ops::Mul<Output=T> + Clone
191        {
192            type Output = Self;
193
194            fn mul(self, c: T) -> Self::Output {
195                $self_ty {
196                    $($element: self.$element * c.clone(),)+
197                    $($phantom: core::marker::PhantomData,)?
198                }
199            }
200        }
201
202        impl<$($ty_param,)* T> core::ops::MulAssign<Self> for $self_ty<$($ty_param,)* T>
203        where
204            T: core::ops::MulAssign,
205        {
206            fn mul_assign(&mut self, other: Self) {
207                $( self.$element *= other.$element; )+
208            }
209        }
210
211        impl<$($ty_param,)* T> core::ops::MulAssign<T> for $self_ty<$($ty_param,)* T>
212        where
213            T:  core::ops::MulAssign + Clone
214        {
215            fn mul_assign(&mut self, c: T) {
216                $( self.$element *= c.clone(); )+
217            }
218        }
219    };
220}
221
222/// Implement `Div` and `DivAssign` traits for a color space.
223///
224/// Both scalars and color arithmetic are implemented.
225macro_rules! impl_color_div {
226    ($self_ty: ident , [$($element: ident),+]) => {
227        impl_color_div!($self_ty<>, [$($element),+]);
228    };
229    ($self_ty: ident < $($ty_param: ident),* > , [$($element: ident),+] $(, $phantom: ident)?) => {
230        impl<$($ty_param,)* T> core::ops::Div<Self> for $self_ty<$($ty_param,)* T>
231        where
232            T: core::ops::Div<Output=T>
233        {
234            type Output = Self;
235
236            fn div(self, other: Self) -> Self::Output {
237                $self_ty {
238                    $($element: self.$element / other.$element,)+
239                    $($phantom: core::marker::PhantomData,)?
240                }
241            }
242        }
243
244        impl<$($ty_param,)* T> core::ops::Div<T> for $self_ty<$($ty_param,)* T>
245        where
246            T: core::ops::Div<Output=T> + Clone
247        {
248            type Output = Self;
249
250            fn div(self, c: T) -> Self::Output {
251                $self_ty {
252                    $($element: self.$element / c.clone(),)+
253                    $($phantom: core::marker::PhantomData,)?
254                }
255            }
256        }
257
258        impl<$($ty_param,)* T> core::ops::DivAssign<Self> for $self_ty<$($ty_param,)* T>
259        where
260            T: core::ops::DivAssign,
261        {
262            fn div_assign(&mut self, other: Self) {
263                $( self.$element /= other.$element; )+
264            }
265        }
266
267        impl<$($ty_param,)* T> core::ops::DivAssign<T> for $self_ty<$($ty_param,)* T>
268        where
269            T:  core::ops::DivAssign + Clone
270        {
271            fn div_assign(&mut self, c: T) {
272                $( self.$element /= c.clone(); )+
273            }
274        }
275    };
276}