palette/macros/
lighten_saturate.rs

1macro_rules! _impl_increase_value_trait {
2    (
3        $trait: ident :: {$method: ident, $method_fixed: ident},
4        $assign_trait: ident :: {$assign_method: ident, $assign_method_fixed: ident},
5        $ty: ident <$($ty_param: ident),*>
6        increase {$($component: ident => [$get_min: expr, $get_max: expr]),+}
7        other {$($other_component: ident),*}
8        $(phantom: $phantom: ident)?
9        $(where $($where: tt)+)?
10    ) => {
11        impl<$($ty_param,)* T> crate::$trait for $ty<$($ty_param,)* T>
12        where
13            T: crate::num::Real
14                + crate::num::Zero
15                + crate::num::MinMax
16                + crate::num::Clamp
17                + crate::num::Arithmetics
18                + crate::num::PartialCmp
19                + Clone,
20            T::Mask: crate::bool_mask::LazySelect<T>,
21            $($($where)+)?
22        {
23            type Scalar = T;
24
25            #[inline]
26            fn $method(self, factor: T) -> Self {
27                $(
28                    let difference = lazy_select!{
29                        if factor.gt_eq(&T::zero()) => $get_max - &self.$component,
30                        else => self.$component.clone(),
31                    };
32
33                    let $component = difference.max(T::zero()) * &factor;
34                )+
35
36                $ty {
37                    $($other_component: self.$other_component,)*
38                    $($component: crate::clamp(self.$component + $component, $get_min, $get_max),)+
39                    $($phantom: PhantomData,)?
40                }
41            }
42
43            #[inline]
44            fn $method_fixed(self, amount: T) -> Self {
45                $ty {
46                    $($other_component: self.$other_component,)*
47                    $($component: crate::clamp(self.$component + $get_max * &amount, $get_min, $get_max),)+
48                    $($phantom: PhantomData,)?
49                }
50            }
51        }
52
53        impl<$($ty_param,)* T> crate::$assign_trait for $ty<$($ty_param,)* T>
54        where
55            T: crate::num::Real
56                + crate::num::Zero
57                + crate::num::MinMax
58                + crate::num::ClampAssign
59                + core::ops::AddAssign
60                + crate::num::Arithmetics
61                + crate::num::PartialCmp
62                + Clone,
63            T::Mask: crate::bool_mask::LazySelect<T>,
64            $($($where)+)?
65        {
66            type Scalar = T;
67
68            #[inline]
69            fn $assign_method(&mut self, factor: T) {
70                $(
71                    let difference = lazy_select!{
72                        if factor.gt_eq(&T::zero()) => $get_max - &self.$component,
73                        else => self.$component.clone(),
74                    };
75
76                    self.$component += difference.max(T::zero()) * &factor;
77                    crate::clamp_assign(&mut self.$component, $get_min, $get_max);
78                )+
79            }
80
81            #[inline]
82            fn $assign_method_fixed(&mut self, amount: T) {
83                $(
84                    self.$component += $get_max * &amount;
85                    crate::clamp_assign(&mut self.$component, $get_min, $get_max);
86                )+
87            }
88        }
89    };
90}
91
92macro_rules! impl_lighten {
93    ($ty: ident increase $($input: tt)+) => {
94        impl_lighten!($ty<> increase $($input)+);
95    };
96    ($($input: tt)+) => {
97        _impl_increase_value_trait!(
98            Lighten::{lighten, lighten_fixed},
99            LightenAssign::{lighten_assign, lighten_fixed_assign},
100            $($input)+
101        );
102    };
103}
104
105macro_rules! impl_saturate {
106    ($ty: ident increase $($input: tt)+) => {
107        // add empty generics brackets
108        impl_saturate!($ty<> increase $($input)+);
109    };
110    ($($input: tt)+) => {
111        _impl_increase_value_trait!(
112            Saturate::{saturate, saturate_fixed},
113            SaturateAssign::{saturate_assign, saturate_fixed_assign},
114            $($input)+
115        );
116    };
117}
118
119macro_rules! impl_lighten_hwb {
120    (
121        $ty: ident
122        $(phantom: $phantom: ident)?
123        $(where $($where: tt)+)?
124    ) => {
125        impl_lighten_hwb!($ty<> $(phantom: $phantom)? $(where $($where)+ )?);
126    };
127    (
128        $ty: ident <$($ty_param: ident),*>
129        $(phantom: $phantom: ident)?
130        $(where $($where: tt)+)?
131    ) => {
132        impl<$($ty_param,)* T> crate::Lighten for $ty<$($ty_param,)* T>
133        where
134            T: crate::num::Real
135                + crate::num::Zero
136                + crate::num::MinMax
137                + crate::num::Arithmetics
138                + crate::num::PartialCmp
139                + Clone,
140            T::Mask: LazySelect<T>,
141            $($($where)+)?
142        {
143            type Scalar = T;
144
145            #[inline]
146            fn lighten(self, factor: T) -> Self {
147                let difference_whiteness = lazy_select! {
148                    if factor.gt_eq(&T::zero()) => Self::max_whiteness() - &self.whiteness,
149                    else => self.whiteness.clone(),
150                };
151                let delta_whiteness = difference_whiteness.max(T::zero()) * &factor;
152
153                let difference_blackness = lazy_select! {
154                    if factor.gt_eq(&T::zero()) => self.blackness.clone(),
155                    else => Self::max_blackness() - &self.blackness,
156                };
157                let delta_blackness = difference_blackness.max(T::zero()) * factor;
158
159                Self {
160                    hue: self.hue,
161                    whiteness: (self.whiteness + delta_whiteness).max(Self::min_whiteness()),
162                    blackness: (self.blackness - delta_blackness).max(Self::min_blackness()),
163                    $($phantom: PhantomData,)?
164                }
165            }
166
167            #[inline]
168            fn lighten_fixed(self, amount: T) -> Self {
169                Self {
170                    hue: self.hue,
171                    whiteness: (self.whiteness + Self::max_whiteness() * &amount)
172                        .max(Self::min_whiteness()),
173                    blackness: (self.blackness - Self::max_blackness() * amount).max(Self::min_blackness()),
174                    $($phantom: PhantomData,)?
175                }
176            }
177        }
178
179        impl<$($ty_param,)* T> crate::LightenAssign for $ty<$($ty_param,)* T>
180        where
181            T: crate::num::Real
182                + crate::num::Zero
183                + crate::num::MinMax
184                + crate::num::ClampAssign
185                + core::ops::AddAssign
186                + core::ops::SubAssign
187                + crate::num::Arithmetics
188                + crate::num::PartialCmp
189                + Clone,
190            T::Mask: LazySelect<T>,
191            $($($where)+)?
192        {
193            type Scalar = T;
194
195            #[inline]
196            fn lighten_assign(&mut self, factor: T) {
197                let difference_whiteness = lazy_select! {
198                    if factor.gt_eq(&T::zero()) => Self::max_whiteness() - &self.whiteness,
199                    else => self.whiteness.clone(),
200                };
201                self.whiteness += difference_whiteness.max(T::zero()) * &factor;
202                crate::clamp_min_assign(&mut self.whiteness, Self::min_whiteness());
203
204                let difference_blackness = lazy_select! {
205                    if factor.gt_eq(&T::zero()) => self.blackness.clone(),
206                    else => Self::max_blackness() - &self.blackness,
207                };
208                self.blackness -= difference_blackness.max(T::zero()) * factor;
209                crate::clamp_min_assign(&mut self.blackness, Self::min_blackness());
210            }
211
212            #[inline]
213            fn lighten_fixed_assign(&mut self, amount: T) {
214                self.whiteness += Self::max_whiteness() * &amount;
215                crate::clamp_min_assign(&mut self.whiteness, Self::min_whiteness());
216
217                self.blackness -= Self::max_blackness() * amount;
218                crate::clamp_min_assign(&mut self.blackness, Self::min_blackness());
219            }
220        }
221    };
222}