palette/macros/
blend.rs
1macro_rules! impl_premultiply {
2 ($ty: ident {$($component: ident),+} $(phantom: $phantom: ident)? $(where $($where: tt)+)?) => {
3 impl_premultiply!($ty<> {$($component),+} $(phantom: $phantom)? $(where $($where)+)?);
4 };
5 ($ty: ident <$($ty_param: ident),*> {$($component: ident),+} $(phantom: $phantom: ident)? $(where $($where: tt)+)?) => {
6 impl<$($ty_param,)* T> crate::blend::Premultiply for $ty<$($ty_param,)* T>
7 where
8 T: crate::num::Real
9 + crate::stimulus::Stimulus
10 + crate::num::Zero
11 + crate::num::IsValidDivisor
12 + core::ops::Mul<T, Output = T>
13 + core::ops::Div<T, Output = T>
14 + Clone,
15 T::Mask: crate::bool_mask::LazySelect<T> + Clone,
16 $($($where)+)?
17 {
18 type Scalar = T;
19
20 #[inline]
21 fn premultiply(self, alpha: T) -> crate::blend::PreAlpha<Self> {
22 crate::blend::PreAlpha {
23 color: self * alpha.clone(),
24 alpha
25 }
26 }
27
28 #[inline]
29 fn unpremultiply(premultiplied: crate::blend::PreAlpha<Self>) -> (Self, T) {
30 let crate::blend::PreAlpha {
31 color: $ty { $($component,)+ .. },
32 alpha,
33 } = premultiplied;
34
35 let is_valid_divisor = alpha.is_valid_divisor();
36
37 let color = Self {
38 $(
39 $component: lazy_select! {
40 if is_valid_divisor.clone() => $component / alpha.clone(),
41 else => T::zero()
42 },
43 )+
44 $($phantom: core::marker::PhantomData,)?
45 };
46
47 (color, alpha)
48 }
49 }
50
51 impl<$($ty_param,)* T> From<crate::blend::PreAlpha<Self>> for $ty<$($ty_param,)* T>
52 where
53 Self: crate::blend::Premultiply<Scalar = T>,
54 {
55 fn from(premultiplied: crate::blend::PreAlpha<Self>) -> Self {
56 use crate::blend::Premultiply;
57
58 Self::unpremultiply(premultiplied).0
59 }
60 }
61 };
62}