Skip to content

Commit

Permalink
Merge #118
Browse files Browse the repository at this point in the history
118: Implement assign ops r=Ogeon a=Veykril

Closes #94.

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
  • Loading branch information
bors[bot] and Veykril committed Sep 18, 2018
2 parents 961b58e + 70ea4b3 commit d1acf02
Show file tree
Hide file tree
Showing 12 changed files with 886 additions and 60 deletions.
58 changes: 57 additions & 1 deletion palette/src/alpha.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
use core::ops::{Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use core::fmt;

use float::Float;
Expand Down Expand Up @@ -237,6 +237,20 @@ impl<T: Add + Clone, C: Add<T>> Add<T> for Alpha<C, T> {
}
}

impl<C: AddAssign, T: Float + AddAssign> AddAssign for Alpha<C, T> {
fn add_assign(&mut self, other: Alpha<C, T>) {
self.color += other.color;
self.alpha += other.alpha;
}
}

impl<T: AddAssign + Copy, C: AddAssign<T>> AddAssign<T> for Alpha<C, T> {
fn add_assign(&mut self, c: T) {
self.color += c;
self.alpha += c;
}
}

impl<C: Sub, T: Float> Sub for Alpha<C, T> {
type Output = Alpha<C::Output, <T as Sub>::Output>;

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

impl<C: SubAssign, T: Float + SubAssign> SubAssign for Alpha<C, T> {
fn sub_assign(&mut self, other: Alpha<C, T>) {
self.color -= other.color;
self.alpha -= other.alpha;
}
}

impl<T: SubAssign + Copy, C: SubAssign<T>> SubAssign<T> for Alpha<C, T> {
fn sub_assign(&mut self, c: T) {
self.color -= c;
self.alpha -= c;
}
}

impl<C: Mul, T: Float> Mul for Alpha<C, T> {
type Output = Alpha<C::Output, <T as Mul>::Output>;

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

impl<C: MulAssign, T: Float + MulAssign> MulAssign for Alpha<C, T> {
fn mul_assign(&mut self, other: Alpha<C, T>) {
self.color *= other.color;
self.alpha *= other.alpha;
}
}

impl<T: MulAssign + Copy, C: MulAssign<T>> MulAssign<T> for Alpha<C, T> {
fn mul_assign(&mut self, c: T) {
self.color *= c;
self.alpha *= c;
}
}

impl<C: Div, T: Float> Div for Alpha<C, T> {
type Output = Alpha<C::Output, <T as Div>::Output>;

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

impl<C: DivAssign, T: Float + DivAssign> DivAssign for Alpha<C, T> {
fn div_assign(&mut self, other: Alpha<C, T>) {
self.color /= other.color;
self.alpha /= other.alpha;
}
}

impl<T: DivAssign + Copy, C: DivAssign<T>> DivAssign<T> for Alpha<C, T> {
fn div_assign(&mut self, c: T) {
self.color /= c;
self.alpha /= c;
}
}

impl<C, T, P> AsRef<P> for Alpha<C, T>
where
C: Pixel<T>,
Expand Down
75 changes: 66 additions & 9 deletions palette/src/blend/pre_alpha.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
use core::ops::{Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
use float::Float;

Expand Down Expand Up @@ -197,7 +197,7 @@ where
impl<C: Add, T: Float> Add for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn add(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
fn add(self, other: PreAlpha<C, T>) -> Self::Output {
PreAlpha {
color: self.color + other.color,
alpha: self.alpha + other.alpha,
Expand All @@ -208,18 +208,32 @@ impl<C: Add, T: Float> Add for PreAlpha<C, T> {
impl<T: Float, C: Add<T>> Add<T> for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn add(self, c: T) -> PreAlpha<C::Output, T> {
fn add(self, c: T) -> Self::Output {
PreAlpha {
color: self.color + c,
alpha: self.alpha + c,
}
}
}

impl<C: AddAssign , T: Float + AddAssign> AddAssign for PreAlpha<C, T> {
fn add_assign(&mut self, other: PreAlpha<C, T>) {
self.color += other.color;
self.alpha += other.alpha;
}
}

impl<T: Float + AddAssign, C: AddAssign<T>> AddAssign<T> for PreAlpha<C, T> {
fn add_assign(&mut self, c: T) {
self.color += c;
self.alpha += c;
}
}

impl<C: Sub, T: Float> Sub for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn sub(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
fn sub(self, other: PreAlpha<C, T>) -> Self::Output {
PreAlpha {
color: self.color - other.color,
alpha: self.alpha - other.alpha,
Expand All @@ -230,18 +244,33 @@ impl<C: Sub, T: Float> Sub for PreAlpha<C, T> {
impl<T: Float, C: Sub<T>> Sub<T> for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn sub(self, c: T) -> PreAlpha<C::Output, T> {
fn sub(self, c: T) -> Self::Output {
PreAlpha {
color: self.color - c,
alpha: self.alpha - c,
}
}
}

impl<C: SubAssign , T: Float + SubAssign> SubAssign for PreAlpha<C, T> {
fn sub_assign(&mut self, other: PreAlpha<C, T>) {
self.color -= other.color;
self.alpha -= other.alpha;
}
}

impl<T: Float + SubAssign, C: SubAssign<T>> SubAssign<T> for PreAlpha<C, T> {
fn sub_assign(&mut self, c: T) {
self.color -= c;
self.alpha -= c;
}
}


impl<C: Mul, T: Float> Mul for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn mul(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
fn mul(self, other: PreAlpha<C, T>) -> Self::Output {
PreAlpha {
color: self.color * other.color,
alpha: self.alpha * other.alpha,
Expand All @@ -252,18 +281,32 @@ impl<C: Mul, T: Float> Mul for PreAlpha<C, T> {
impl<T: Float, C: Mul<T>> Mul<T> for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn mul(self, c: T) -> PreAlpha<C::Output, T> {
fn mul(self, c: T) -> Self::Output {
PreAlpha {
color: self.color * c,
alpha: self.alpha * c,
}
}
}

impl<C: MulAssign , T: Float + MulAssign> MulAssign for PreAlpha<C, T> {
fn mul_assign(&mut self, other: PreAlpha<C, T>) {
self.color *= other.color;
self.alpha *= other.alpha;
}
}

impl<T: Float + MulAssign, C: MulAssign<T>> MulAssign<T> for PreAlpha<C, T> {
fn mul_assign(&mut self, c: T) {
self.color *= c;
self.alpha *= c;
}
}

impl<C: Div, T: Float> Div for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn div(self, other: PreAlpha<C, T>) -> PreAlpha<C::Output, T> {
fn div(self, other: PreAlpha<C, T>) -> Self::Output {
PreAlpha {
color: self.color / other.color,
alpha: self.alpha / other.alpha,
Expand All @@ -274,14 +317,28 @@ impl<C: Div, T: Float> Div for PreAlpha<C, T> {
impl<T: Float, C: Div<T>> Div<T> for PreAlpha<C, T> {
type Output = PreAlpha<C::Output, T>;

fn div(self, c: T) -> PreAlpha<C::Output, T> {
fn div(self, c: T) -> Self::Output {
PreAlpha {
color: self.color / c,
alpha: self.alpha / c,
}
}
}

impl<C: DivAssign , T: Float + DivAssign> DivAssign for PreAlpha<C, T> {
fn div_assign(&mut self, other: PreAlpha<C, T>) {
self.color /= other.color;
self.alpha /= other.alpha;
}
}

impl<T: Float + DivAssign, C: DivAssign<T>> DivAssign<T> for PreAlpha<C, T> {
fn div_assign(&mut self, c: T) {
self.color /= c;
self.alpha /= c;
}
}

impl<C, T, P> AsRef<P> for PreAlpha<C, T>
where
C: Pixel<T>,
Expand Down
58 changes: 53 additions & 5 deletions palette/src/hsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use float::Float;

use core::any::TypeId;
use core::marker::PhantomData;
use core::ops::{Add, Sub};
use core::ops::{Add, AddAssign, Sub, SubAssign};

use encoding::pixel::RawPixel;
use encoding::{Linear, Srgb};
Expand Down Expand Up @@ -435,7 +435,7 @@ where
{
type Output = Hsl<S, T>;

fn add(self, other: Hsl<S, T>) -> Hsl<S, T> {
fn add(self, other: Hsl<S, T>) -> Self::Output {
Hsl {
hue: self.hue + other.hue,
saturation: self.saturation + other.saturation,
Expand All @@ -452,7 +452,7 @@ where
{
type Output = Hsl<S, T>;

fn add(self, c: T) -> Hsl<S, T> {
fn add(self, c: T) -> Self::Output {
Hsl {
hue: self.hue + c,
saturation: self.saturation + c,
Expand All @@ -462,14 +462,38 @@ where
}
}

impl<S, T> AddAssign<Hsl<S, T>> for Hsl<S, T>
where
T: Component + Float + AddAssign,
S: RgbSpace,
{
fn add_assign(&mut self, other: Hsl<S, T>) {
self.hue += other.hue;
self.saturation += other.saturation;
self.lightness += other.lightness;
}
}

impl<S, T> AddAssign<T> for Hsl<S, T>
where
T: Component + Float + AddAssign,
S: RgbSpace,
{
fn add_assign(&mut self, c: T) {
self.hue += c;
self.saturation += c;
self.lightness += c;
}
}

impl<S, T> Sub<Hsl<S, T>> for Hsl<S, T>
where
T: Component + Float,
S: RgbSpace,
{
type Output = Hsl<S, T>;

fn sub(self, other: Hsl<S, T>) -> Hsl<S, T> {
fn sub(self, other: Hsl<S, T>) -> Self::Output {
Hsl {
hue: self.hue - other.hue,
saturation: self.saturation - other.saturation,
Expand All @@ -486,7 +510,7 @@ where
{
type Output = Hsl<S, T>;

fn sub(self, c: T) -> Hsl<S, T> {
fn sub(self, c: T) -> Self::Output {
Hsl {
hue: self.hue - c,
saturation: self.saturation - c,
Expand All @@ -496,6 +520,30 @@ where
}
}

impl<S, T> SubAssign<Hsl<S, T>> for Hsl<S, T>
where
T: Component + Float + SubAssign,
S: RgbSpace,
{
fn sub_assign(&mut self, other: Hsl<S, T>) {
self.hue -= other.hue;
self.saturation -= other.saturation;
self.lightness -= other.lightness;
}
}

impl<S, T> SubAssign<T> for Hsl<S, T>
where
T: Component + Float + SubAssign,
S: RgbSpace,
{
fn sub_assign(&mut self, c: T) {
self.hue -= c;
self.saturation -= c;
self.lightness -= c;
}
}

impl<S, T, P> AsRef<P> for Hsl<S, T>
where
T: Component + Float,
Expand Down
Loading

0 comments on commit d1acf02

Please sign in to comment.