@@ -2,6 +2,7 @@ use core::fmt;
2
2
use core:: ops:: { Add , AddAssign , Deref , DerefMut , Div , DivAssign , Mul , MulAssign , Sub , SubAssign } ;
3
3
4
4
use approx:: { AbsDiffEq , RelativeEq , UlpsEq } ;
5
+ use num_traits:: { One , Zero } ;
5
6
#[ cfg( feature = "random" ) ]
6
7
use rand:: distributions:: uniform:: { SampleBorrow , SampleUniform , Uniform , UniformSampler } ;
7
8
#[ cfg( feature = "random" ) ]
@@ -15,7 +16,7 @@ use crate::encoding::pixel::RawPixel;
15
16
use crate :: float:: Float ;
16
17
use crate :: {
17
18
clamp, clamp_assign, Blend , Clamp , ClampAssign , Component , ComponentWise , GetHue , Hue ,
18
- IsWithinBounds , Mix , Pixel , Saturate , Shade , WithAlpha ,
19
+ IsWithinBounds , Mix , MixAssign , Pixel , Saturate , Shade , WithAlpha ,
19
20
} ;
20
21
21
22
/// An alpha component wrapper for colors.
@@ -110,18 +111,37 @@ impl<C, T> DerefMut for Alpha<C, T> {
110
111
impl < C > Mix for Alpha < C , C :: Scalar >
111
112
where
112
113
C : Mix ,
114
+ C :: Scalar : Zero + One + PartialOrd + Sub < Output = C :: Scalar > + Clone ,
113
115
{
114
116
type Scalar = C :: Scalar ;
115
117
116
118
#[ inline]
117
- fn mix ( mut self , other : Alpha < C , C :: Scalar > , factor : C :: Scalar ) -> Alpha < C , C :: Scalar > {
118
- self . color = self . color . mix ( other. color , factor) ;
119
- self . alpha = self . alpha + factor * ( other. alpha - self . alpha ) ;
119
+ fn mix ( mut self , other : Self , factor : C :: Scalar ) -> Self {
120
+ let factor = clamp ( factor, C :: Scalar :: zero ( ) , C :: Scalar :: one ( ) ) ;
121
+
122
+ self . color = self . color . mix ( other. color , factor. clone ( ) ) ;
123
+ self . alpha = self . alpha . clone ( ) + factor * ( other. alpha - self . alpha ) ;
120
124
121
125
self
122
126
}
123
127
}
124
128
129
+ impl < C > MixAssign for Alpha < C , C :: Scalar >
130
+ where
131
+ C : MixAssign ,
132
+ C :: Scalar : Zero + One + PartialOrd + Sub < Output = C :: Scalar > + AddAssign + Clone ,
133
+ {
134
+ type Scalar = C :: Scalar ;
135
+
136
+ #[ inline]
137
+ fn mix_assign ( & mut self , other : Self , factor : C :: Scalar ) {
138
+ let factor = clamp ( factor, C :: Scalar :: zero ( ) , C :: Scalar :: one ( ) ) ;
139
+
140
+ self . color . mix_assign ( other. color , factor. clone ( ) ) ;
141
+ self . alpha += factor * ( other. alpha - self . alpha . clone ( ) ) ;
142
+ }
143
+ }
144
+
125
145
impl < C : Shade > Shade for Alpha < C , C :: Scalar > {
126
146
type Scalar = C :: Scalar ;
127
147
0 commit comments