Skip to content

Commit 2abed01

Browse files
bors[bot]Veykril
andcommitted
Merge #118
118: Implement assign ops r=Ogeon a=Veykril Closes #94. Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 961b58e + 70ea4b3 commit 2abed01

File tree

12 files changed

+886
-60
lines changed

12 files changed

+886
-60
lines changed

palette/src/alpha.rs

+57-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
1+
use core::ops::{Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
22
use core::fmt;
33

44
use float::Float;
@@ -237,6 +237,20 @@ impl<T: Add + Clone, C: Add<T>> Add<T> for Alpha<C, T> {
237237
}
238238
}
239239

240+
impl<C: AddAssign, T: Float + AddAssign> AddAssign for Alpha<C, T> {
241+
fn add_assign(&mut self, other: Alpha<C, T>) {
242+
self.color += other.color;
243+
self.alpha += other.alpha;
244+
}
245+
}
246+
247+
impl<T: AddAssign + Copy, C: AddAssign<T>> AddAssign<T> for Alpha<C, T> {
248+
fn add_assign(&mut self, c: T) {
249+
self.color += c;
250+
self.alpha += c;
251+
}
252+
}
253+
240254
impl<C: Sub, T: Float> Sub for Alpha<C, T> {
241255
type Output = Alpha<C::Output, <T as Sub>::Output>;
242256

@@ -259,6 +273,20 @@ impl<T: Sub + Clone, C: Sub<T>> Sub<T> for Alpha<C, T> {
259273
}
260274
}
261275

276+
impl<C: SubAssign, T: Float + SubAssign> SubAssign for Alpha<C, T> {
277+
fn sub_assign(&mut self, other: Alpha<C, T>) {
278+
self.color -= other.color;
279+
self.alpha -= other.alpha;
280+
}
281+
}
282+
283+
impl<T: SubAssign + Copy, C: SubAssign<T>> SubAssign<T> for Alpha<C, T> {
284+
fn sub_assign(&mut self, c: T) {
285+
self.color -= c;
286+
self.alpha -= c;
287+
}
288+
}
289+
262290
impl<C: Mul, T: Float> Mul for Alpha<C, T> {
263291
type Output = Alpha<C::Output, <T as Mul>::Output>;
264292

@@ -281,6 +309,20 @@ impl<T: Mul + Clone, C: Mul<T>> Mul<T> for Alpha<C, T> {
281309
}
282310
}
283311

312+
impl<C: MulAssign, T: Float + MulAssign> MulAssign for Alpha<C, T> {
313+
fn mul_assign(&mut self, other: Alpha<C, T>) {
314+
self.color *= other.color;
315+
self.alpha *= other.alpha;
316+
}
317+
}
318+
319+
impl<T: MulAssign + Copy, C: MulAssign<T>> MulAssign<T> for Alpha<C, T> {
320+
fn mul_assign(&mut self, c: T) {
321+
self.color *= c;
322+
self.alpha *= c;
323+
}
324+
}
325+
284326
impl<C: Div, T: Float> Div for Alpha<C, T> {
285327
type Output = Alpha<C::Output, <T as Div>::Output>;
286328

@@ -303,6 +345,20 @@ impl<T: Div + Clone, C: Div<T>> Div<T> for Alpha<C, T> {
303345
}
304346
}
305347

348+
impl<C: DivAssign, T: Float + DivAssign> DivAssign for Alpha<C, T> {
349+
fn div_assign(&mut self, other: Alpha<C, T>) {
350+
self.color /= other.color;
351+
self.alpha /= other.alpha;
352+
}
353+
}
354+
355+
impl<T: DivAssign + Copy, C: DivAssign<T>> DivAssign<T> for Alpha<C, T> {
356+
fn div_assign(&mut self, c: T) {
357+
self.color /= c;
358+
self.alpha /= c;
359+
}
360+
}
361+
306362
impl<C, T, P> AsRef<P> for Alpha<C, T>
307363
where
308364
C: Pixel<T>,

palette/src/blend/pre_alpha.rs

+66-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
1+
use core::ops::{Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
22
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
33
use float::Float;
44

@@ -197,7 +197,7 @@ where
197197
impl<C: Add, T: Float> Add for PreAlpha<C, T> {
198198
type Output = PreAlpha<C::Output, T>;
199199

200-
fn add(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
200+
fn add(self, other: PreAlpha<C, T>) -> Self::Output {
201201
PreAlpha {
202202
color: self.color + other.color,
203203
alpha: self.alpha + other.alpha,
@@ -208,18 +208,32 @@ impl<C: Add, T: Float> Add for PreAlpha<C, T> {
208208
impl<T: Float, C: Add<T>> Add<T> for PreAlpha<C, T> {
209209
type Output = PreAlpha<C::Output, T>;
210210

211-
fn add(self, c: T) -> PreAlpha<C::Output, T> {
211+
fn add(self, c: T) -> Self::Output {
212212
PreAlpha {
213213
color: self.color + c,
214214
alpha: self.alpha + c,
215215
}
216216
}
217217
}
218218

219+
impl<C: AddAssign , T: Float + AddAssign> AddAssign for PreAlpha<C, T> {
220+
fn add_assign(&mut self, other: PreAlpha<C, T>) {
221+
self.color += other.color;
222+
self.alpha += other.alpha;
223+
}
224+
}
225+
226+
impl<T: Float + AddAssign, C: AddAssign<T>> AddAssign<T> for PreAlpha<C, T> {
227+
fn add_assign(&mut self, c: T) {
228+
self.color += c;
229+
self.alpha += c;
230+
}
231+
}
232+
219233
impl<C: Sub, T: Float> Sub for PreAlpha<C, T> {
220234
type Output = PreAlpha<C::Output, T>;
221235

222-
fn sub(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
236+
fn sub(self, other: PreAlpha<C, T>) -> Self::Output {
223237
PreAlpha {
224238
color: self.color - other.color,
225239
alpha: self.alpha - other.alpha,
@@ -230,18 +244,33 @@ impl<C: Sub, T: Float> Sub for PreAlpha<C, T> {
230244
impl<T: Float, C: Sub<T>> Sub<T> for PreAlpha<C, T> {
231245
type Output = PreAlpha<C::Output, T>;
232246

233-
fn sub(self, c: T) -> PreAlpha<C::Output, T> {
247+
fn sub(self, c: T) -> Self::Output {
234248
PreAlpha {
235249
color: self.color - c,
236250
alpha: self.alpha - c,
237251
}
238252
}
239253
}
240254

255+
impl<C: SubAssign , T: Float + SubAssign> SubAssign for PreAlpha<C, T> {
256+
fn sub_assign(&mut self, other: PreAlpha<C, T>) {
257+
self.color -= other.color;
258+
self.alpha -= other.alpha;
259+
}
260+
}
261+
262+
impl<T: Float + SubAssign, C: SubAssign<T>> SubAssign<T> for PreAlpha<C, T> {
263+
fn sub_assign(&mut self, c: T) {
264+
self.color -= c;
265+
self.alpha -= c;
266+
}
267+
}
268+
269+
241270
impl<C: Mul, T: Float> Mul for PreAlpha<C, T> {
242271
type Output = PreAlpha<C::Output, T>;
243272

244-
fn mul(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
273+
fn mul(self, other: PreAlpha<C, T>) -> Self::Output {
245274
PreAlpha {
246275
color: self.color * other.color,
247276
alpha: self.alpha * other.alpha,
@@ -252,18 +281,32 @@ impl<C: Mul, T: Float> Mul for PreAlpha<C, T> {
252281
impl<T: Float, C: Mul<T>> Mul<T> for PreAlpha<C, T> {
253282
type Output = PreAlpha<C::Output, T>;
254283

255-
fn mul(self, c: T) -> PreAlpha<C::Output, T> {
284+
fn mul(self, c: T) -> Self::Output {
256285
PreAlpha {
257286
color: self.color * c,
258287
alpha: self.alpha * c,
259288
}
260289
}
261290
}
262291

292+
impl<C: MulAssign , T: Float + MulAssign> MulAssign for PreAlpha<C, T> {
293+
fn mul_assign(&mut self, other: PreAlpha<C, T>) {
294+
self.color *= other.color;
295+
self.alpha *= other.alpha;
296+
}
297+
}
298+
299+
impl<T: Float + MulAssign, C: MulAssign<T>> MulAssign<T> for PreAlpha<C, T> {
300+
fn mul_assign(&mut self, c: T) {
301+
self.color *= c;
302+
self.alpha *= c;
303+
}
304+
}
305+
263306
impl<C: Div, T: Float> Div for PreAlpha<C, T> {
264307
type Output = PreAlpha<C::Output, T>;
265308

266-
fn div(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
309+
fn div(self, other: PreAlpha<C, T>) -> Self::Output {
267310
PreAlpha {
268311
color: self.color / other.color,
269312
alpha: self.alpha / other.alpha,
@@ -274,14 +317,28 @@ impl<C: Div, T: Float> Div for PreAlpha<C, T> {
274317
impl<T: Float, C: Div<T>> Div<T> for PreAlpha<C, T> {
275318
type Output = PreAlpha<C::Output, T>;
276319

277-
fn div(self, c: T) -> PreAlpha<C::Output, T> {
320+
fn div(self, c: T) -> Self::Output {
278321
PreAlpha {
279322
color: self.color / c,
280323
alpha: self.alpha / c,
281324
}
282325
}
283326
}
284327

328+
impl<C: DivAssign , T: Float + DivAssign> DivAssign for PreAlpha<C, T> {
329+
fn div_assign(&mut self, other: PreAlpha<C, T>) {
330+
self.color /= other.color;
331+
self.alpha /= other.alpha;
332+
}
333+
}
334+
335+
impl<T: Float + DivAssign, C: DivAssign<T>> DivAssign<T> for PreAlpha<C, T> {
336+
fn div_assign(&mut self, c: T) {
337+
self.color /= c;
338+
self.alpha /= c;
339+
}
340+
}
341+
285342
impl<C, T, P> AsRef<P> for PreAlpha<C, T>
286343
where
287344
C: Pixel<T>,

palette/src/hsl.rs

+53-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use float::Float;
33

44
use core::any::TypeId;
55
use core::marker::PhantomData;
6-
use core::ops::{Add, Sub};
6+
use core::ops::{Add, AddAssign, Sub, SubAssign};
77

88
use encoding::pixel::RawPixel;
99
use encoding::{Linear, Srgb};
@@ -435,7 +435,7 @@ where
435435
{
436436
type Output = Hsl<S, T>;
437437

438-
fn add(self, other: Hsl<S, T>) -> Hsl<S, T> {
438+
fn add(self, other: Hsl<S, T>) -> Self::Output {
439439
Hsl {
440440
hue: self.hue + other.hue,
441441
saturation: self.saturation + other.saturation,
@@ -452,7 +452,7 @@ where
452452
{
453453
type Output = Hsl<S, T>;
454454

455-
fn add(self, c: T) -> Hsl<S, T> {
455+
fn add(self, c: T) -> Self::Output {
456456
Hsl {
457457
hue: self.hue + c,
458458
saturation: self.saturation + c,
@@ -462,14 +462,38 @@ where
462462
}
463463
}
464464

465+
impl<S, T> AddAssign<Hsl<S, T>> for Hsl<S, T>
466+
where
467+
T: Component + Float + AddAssign,
468+
S: RgbSpace,
469+
{
470+
fn add_assign(&mut self, other: Hsl<S, T>) {
471+
self.hue += other.hue;
472+
self.saturation += other.saturation;
473+
self.lightness += other.lightness;
474+
}
475+
}
476+
477+
impl<S, T> AddAssign<T> for Hsl<S, T>
478+
where
479+
T: Component + Float + AddAssign,
480+
S: RgbSpace,
481+
{
482+
fn add_assign(&mut self, c: T) {
483+
self.hue += c;
484+
self.saturation += c;
485+
self.lightness += c;
486+
}
487+
}
488+
465489
impl<S, T> Sub<Hsl<S, T>> for Hsl<S, T>
466490
where
467491
T: Component + Float,
468492
S: RgbSpace,
469493
{
470494
type Output = Hsl<S, T>;
471495

472-
fn sub(self, other: Hsl<S, T>) -> Hsl<S, T> {
496+
fn sub(self, other: Hsl<S, T>) -> Self::Output {
473497
Hsl {
474498
hue: self.hue - other.hue,
475499
saturation: self.saturation - other.saturation,
@@ -486,7 +510,7 @@ where
486510
{
487511
type Output = Hsl<S, T>;
488512

489-
fn sub(self, c: T) -> Hsl<S, T> {
513+
fn sub(self, c: T) -> Self::Output {
490514
Hsl {
491515
hue: self.hue - c,
492516
saturation: self.saturation - c,
@@ -496,6 +520,30 @@ where
496520
}
497521
}
498522

523+
impl<S, T> SubAssign<Hsl<S, T>> for Hsl<S, T>
524+
where
525+
T: Component + Float + SubAssign,
526+
S: RgbSpace,
527+
{
528+
fn sub_assign(&mut self, other: Hsl<S, T>) {
529+
self.hue -= other.hue;
530+
self.saturation -= other.saturation;
531+
self.lightness -= other.lightness;
532+
}
533+
}
534+
535+
impl<S, T> SubAssign<T> for Hsl<S, T>
536+
where
537+
T: Component + Float + SubAssign,
538+
S: RgbSpace,
539+
{
540+
fn sub_assign(&mut self, c: T) {
541+
self.hue -= c;
542+
self.saturation -= c;
543+
self.lightness -= c;
544+
}
545+
}
546+
499547
impl<S, T, P> AsRef<P> for Hsl<S, T>
500548
where
501549
T: Component + Float,

0 commit comments

Comments
 (0)