From a42229e8929a9afbdb0a5c0c63a4d234d0525d9f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 28 May 2018 00:01:39 +0200 Subject: [PATCH 1/5] Bump image crate to 0.19 --- palette/Cargo.toml | 2 +- palette/examples/color_scheme.rs | 24 +++++++----- palette/examples/gradient.rs | 52 +++++++++++++++++++------ palette/examples/readme_examples.rs | 42 ++++++++++++++------ palette/examples/saturate.rs | 49 ++++++++++++++--------- palette/examples/shade.rs | 60 +++++++++++++++++++++++------ 6 files changed, 166 insertions(+), 63 deletions(-) diff --git a/palette/Cargo.toml b/palette/Cargo.toml index 940fd16d0..222de2c12 100644 --- a/palette/Cargo.toml +++ b/palette/Cargo.toml @@ -36,7 +36,7 @@ features = ["serde_derive"] optional = true [dev-dependencies] -image = "0.18" +image = "0.19" clap = "2" csv = "1.0.0-beta.3" serde = "1" diff --git a/palette/examples/color_scheme.rs b/palette/examples/color_scheme.rs index bec3faf2a..9c8e14096 100644 --- a/palette/examples/color_scheme.rs +++ b/palette/examples/color_scheme.rs @@ -190,15 +190,21 @@ fn blit_shades> + 'static>( .into_format() .into_raw(); - for (x, y, pixel) in canvas.pixels_mut() { - if y < height / 2 { - pixel.data = primary; - } else if x < width / 3 { - pixel.data = light; - } else if x < (width / 3) * 2 { - pixel.data = dark1; - } else { - pixel.data = dark2; + + for x in 0..width { + for y in 0..height { + canvas.put_pixel(x, y, image::Rgb { + data: + if y < height / 2 { + primary + } else if x < width / 3 { + light + } else if x < (width / 3) * 2 { + dark1 + } else { + dark2 + } + }); } } } diff --git a/palette/examples/gradient.rs b/palette/examples/gradient.rs index 946e638c9..d3a78215a 100644 --- a/palette/examples/gradient.rs +++ b/palette/examples/gradient.rs @@ -49,20 +49,50 @@ fn main() { let c3 = Srgb::from_linear(c3.into()).into_format().into_raw(); let c4 = Srgb::from_linear(c4.into()).into_format().into_raw(); - for (_, _, pixel) in image.sub_image(i as u32, 0, 1, 31).pixels_mut() { - pixel.data = c1 - } - for (_, _, pixel) in image.sub_image(i as u32, 32, 1, 31).pixels_mut() { - pixel.data = c2; + { + let mut sub_image = image.sub_image(i as u32, 0, 1, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { + data: c1 + }); + } + } } - - for (_, _, pixel) in image.sub_image(i as u32, 65, 1, 31).pixels_mut() { - pixel.data = c3; + { + let mut sub_image = image.sub_image(i as u32, 32, 1, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { + data: c2 + }); + } + } } - - for (_, _, pixel) in image.sub_image(i as u32, 97, 1, 31).pixels_mut() { - pixel.data = c4; + { + let mut sub_image = image.sub_image(i as u32, 65, 1, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { + data: c3 + }); + } + } + } + { + let mut sub_image = image.sub_image(i as u32, 97, 1, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { + data: c4 + }); + } + } } } diff --git a/palette/examples/readme_examples.rs b/palette/examples/readme_examples.rs index 9ba341c3b..f8ce309ae 100644 --- a/palette/examples/readme_examples.rs +++ b/palette/examples/readme_examples.rs @@ -66,8 +66,12 @@ mod gradients { fn display_colors(filename: &str, colors: &[Srgb]) { let mut image = RgbImage::new(colors.len() as u32 * 64, 64); for (i, &color) in colors.iter().enumerate() { - for (_, _, pixel) in image.sub_image(i as u32 * 64, 0, 64, 64).pixels_mut() { - pixel.data = *color.as_raw(); + let mut sub_image = image.sub_image(i as u32 * 64, 0, 64, 64); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { data: *color.as_raw() }); + } } } @@ -86,17 +90,31 @@ fn display_gradients + Clone, B: Mix + Clone> LinSrgb: From, { let mut image = RgbImage::new(256, 64); - - for (x, _, pixel) in image.sub_image(0, 0, 256, 32).pixels_mut() { - pixel.data = Srgb::from_linear(grad1.get(x as f32 / 255.0).into()) - .into_format() - .into_raw(); + { + let mut sub_image = image.sub_image(0, 0, 256, 32); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { + data: Srgb::from_linear(grad1.get(x as f32 / 255.0).into()) + .into_format() + .into_raw() + }); + } + } } - - for (x, _, pixel) in image.sub_image(0, 32, 256, 32).pixels_mut() { - pixel.data = Srgb::from_linear(grad2.get(x as f32 / 255.0).into()) - .into_format() - .into_raw(); + { + let mut sub_image = image.sub_image(0, 32, 256, 32); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { + data: Srgb::from_linear(grad2.get(x as f32 / 255.0).into()) + .into_format() + .into_raw() + }); + } + } } match image.save(filename) { diff --git a/palette/examples/saturate.rs b/palette/examples/saturate.rs index ed5d9cdea..8c7b57980 100644 --- a/palette/examples/saturate.rs +++ b/palette/examples/saturate.rs @@ -15,27 +15,40 @@ fn main() { //Increase the saturation by 80% (!) as HSL in the left half, and as LCh //in the right half. Notice the strong yellow tone in the HSL part. - for (_, _, pixel) in image.sub_image(0, 0, width / 2, height).pixels_mut() { - let color: Hsl = Srgb::from_raw(&pixel.data) - .into_format() - .into_linear() - .into(); - - let saturated = color.saturate(0.8); - pixel.data = Srgb::from_linear(saturated.into()).into_format().into_raw(); + { + let mut sub_image = image.sub_image(0, 0, width / 2, height); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + let color: Hsl = Srgb::from_raw(&sub_image.get_pixel(x, y).data) + .into_format() + .into_linear() + .into(); + + let saturated = color.saturate(0.8); + sub_image.put_pixel(x, y, image::Rgb { + data: Srgb::from_linear(saturated.into()).into_format().into_raw() + }); + } + } } - for (_, _, pixel) in image - .sub_image(width / 2, 0, width / 2, height) - .pixels_mut() { - let color: Lch = Srgb::from_raw(&pixel.data) - .into_format() - .into_linear() - .into(); - - let saturated = color.saturate(0.8); - pixel.data = Srgb::from_linear(saturated.into()).into_format().into_raw(); + let mut sub_image = image.sub_image(width / 2, 0, width / 2, height); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + let color: Lch = Srgb::from_raw(&sub_image.get_pixel(x, y).data) + .into_format() + .into_linear() + .into(); + + let saturated = color.saturate(0.8); + sub_image.put_pixel(x, y, image::Rgb { + data: Srgb::from_linear(saturated.into()).into_format().into_raw() + }); + } + } } match image.save("examples/saturate.png") { diff --git a/palette/examples/shade.rs b/palette/examples/shade.rs index e083c342e..81f44972f 100644 --- a/palette/examples/shade.rs +++ b/palette/examples/shade.rs @@ -21,12 +21,24 @@ fn main() { .into_format() .into_raw(); - for (_, _, pixel) in image.sub_image(i as u32 * 20, 0, 20, 31).pixels_mut() { - pixel.data = rgb1; + { + let mut sub_image = image.sub_image(i as u32 * 20, 0, 20, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { data: rgb1 }); + } + } } - for (_, _, pixel) in image.sub_image(i as u32 * 20, 32, 20, 31).pixels_mut() { - pixel.data = rgb2; + { + let mut sub_image = image.sub_image(i as u32 * 20, 32, 20, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { data: rgb2 }); + } + } } let lab1 = Srgb::from_linear(lab.darken(0.05 * i as f32).into()) @@ -36,12 +48,24 @@ fn main() { .into_format() .into_raw(); - for (_, _, pixel) in image.sub_image(i as u32 * 20, 65, 20, 31).pixels_mut() { - pixel.data = lab1; + { + let mut sub_image = image.sub_image(i as u32 * 20, 65, 20, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { data: lab1 }); + } + } } - for (_, _, pixel) in image.sub_image(i as u32 * 20, 97, 20, 31).pixels_mut() { - pixel.data = lab2; + { + let mut sub_image = image.sub_image(i as u32 * 20, 97, 20, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { data: lab2 }); + } + } } let hsv1 = Srgb::from_linear(hsv.darken(0.05 * i as f32).into()) @@ -51,12 +75,24 @@ fn main() { .into_format() .into_raw(); - for (_, _, pixel) in image.sub_image(i as u32 * 20, 130, 20, 31).pixels_mut() { - pixel.data = hsv1; + { + let mut sub_image = image.sub_image(i as u32 * 20, 130, 20, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { data: hsv1 }); + } + } } - for (_, _, pixel) in image.sub_image(i as u32 * 20, 162, 20, 31).pixels_mut() { - pixel.data = hsv2; + { + let mut sub_image = image.sub_image(i as u32 * 20, 162, 20, 31); + let (width, height) = sub_image.dimensions(); + for x in 0..width { + for y in 0..height { + sub_image.put_pixel(x, y, image::Rgb { data: hsv2 }); + } + } } } From b81bd828ebb6341d4c7ac44b046932f0bbd0815b Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 28 May 2018 00:15:40 +0200 Subject: [PATCH 2/5] Fix space between scopes --- palette/examples/gradient.rs | 3 +++ palette/examples/readme_examples.rs | 1 + 2 files changed, 4 insertions(+) diff --git a/palette/examples/gradient.rs b/palette/examples/gradient.rs index d3a78215a..91af39d38 100644 --- a/palette/examples/gradient.rs +++ b/palette/examples/gradient.rs @@ -61,6 +61,7 @@ fn main() { } } } + { let mut sub_image = image.sub_image(i as u32, 32, 1, 31); let (width, height) = sub_image.dimensions(); @@ -72,6 +73,7 @@ fn main() { } } } + { let mut sub_image = image.sub_image(i as u32, 65, 1, 31); let (width, height) = sub_image.dimensions(); @@ -83,6 +85,7 @@ fn main() { } } } + { let mut sub_image = image.sub_image(i as u32, 97, 1, 31); let (width, height) = sub_image.dimensions(); diff --git a/palette/examples/readme_examples.rs b/palette/examples/readme_examples.rs index f8ce309ae..905ba26e5 100644 --- a/palette/examples/readme_examples.rs +++ b/palette/examples/readme_examples.rs @@ -103,6 +103,7 @@ fn display_gradients + Clone, B: Mix + Clone> } } } + { let mut sub_image = image.sub_image(0, 32, 256, 32); let (width, height) = sub_image.dimensions(); From f845b7ca297701a8fc7020a8ff73b3fcfacc1ff6 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 28 May 2018 10:48:02 +0200 Subject: [PATCH 3/5] Update approx crate to 0.2 --- palette/Cargo.toml | 2 +- palette/src/alpha.rs | 35 ++++++++++--- palette/src/blend/pre_alpha.rs | 35 ++++++++++--- palette/src/equality.rs | 96 ++++++++++++++++++++++++---------- palette/src/gradient.rs | 42 ++++++++++++--- palette/src/hsl.rs | 47 ++++++++++++----- palette/src/hsv.rs | 47 ++++++++++++----- palette/src/hwb.rs | 64 ++++++++++++++++++----- palette/src/lib.rs | 42 +++++++++++---- palette/src/luma/luma.rs | 36 ++++++++++--- palette/src/rgb/rgb.rs | 47 ++++++++++++----- 11 files changed, 376 insertions(+), 117 deletions(-) diff --git a/palette/Cargo.toml b/palette/Cargo.toml index 222de2c12..a9a799d6b 100644 --- a/palette/Cargo.toml +++ b/palette/Cargo.toml @@ -23,7 +23,7 @@ strict = [] [dependencies] palette_derive = {version = "0.4.0", path = "../palette_derive"} num-traits = "0.2" -approx = "0.1" +approx = "0.2" [dependencies.phf] version = "0.7" diff --git a/palette/src/alpha.rs b/palette/src/alpha.rs index 5912034bf..6d73374c5 100644 --- a/palette/src/alpha.rs +++ b/palette/src/alpha.rs @@ -3,7 +3,7 @@ use std::fmt; use num_traits::Float; -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use {clamp, Blend, Component, ComponentWise, GetHue, Hue, Limited, Mix, Pixel, Saturate, Shade}; use blend::PreAlpha; @@ -159,10 +159,10 @@ impl Default for Alpha { } } -impl ApproxEq for Alpha +impl AbsDiffEq for Alpha where - C: ApproxEq, - T: ApproxEq, + C: AbsDiffEq, + T: AbsDiffEq, T::Epsilon: Clone, { type Epsilon = T::Epsilon; @@ -171,12 +171,20 @@ where T::default_epsilon() } - fn default_max_relative() -> Self::Epsilon { - T::default_max_relative() + fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { + self.color.abs_diff_eq(&other.color, epsilon.clone()) + && self.alpha.abs_diff_eq(&other.alpha, epsilon) } +} - fn default_max_ulps() -> u32 { - T::default_max_ulps() +impl RelativeEq for Alpha + where + C: RelativeEq, + T: RelativeEq, + T::Epsilon: Clone, +{ + fn default_max_relative() -> Self::Epsilon { + T::default_max_relative() } fn relative_eq( @@ -189,6 +197,17 @@ where .relative_eq(&other.color, epsilon.clone(), max_relative.clone()) && self.alpha.relative_eq(&other.alpha, epsilon, max_relative) } +} + +impl UlpsEq for Alpha + where + C: UlpsEq, + T: UlpsEq, + T::Epsilon: Clone, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() + } fn ulps_eq(&self, other: &Alpha, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.color.ulps_eq(&other.color, epsilon.clone(), max_ulps) diff --git a/palette/src/blend/pre_alpha.rs b/palette/src/blend/pre_alpha.rs index cabed73aa..88e44b21f 100644 --- a/palette/src/blend/pre_alpha.rs +++ b/palette/src/blend/pre_alpha.rs @@ -1,5 +1,5 @@ use std::ops::{Add, Deref, DerefMut, Div, Mul, Sub}; -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num_traits::Float; use {clamp, Alpha, Blend, ComponentWise, Mix, Pixel}; @@ -139,10 +139,10 @@ impl Default for PreAlpha { } } -impl ApproxEq for PreAlpha +impl AbsDiffEq for PreAlpha where - C: ApproxEq, - T: ApproxEq + Float, + C: AbsDiffEq , + T: AbsDiffEq + Float, T::Epsilon: Copy, { type Epsilon = T::Epsilon; @@ -151,12 +151,20 @@ where T::default_epsilon() } - fn default_max_relative() -> Self::Epsilon { - T::default_max_relative() + fn abs_diff_eq(&self, other: &PreAlpha, epsilon: Self::Epsilon) -> bool { + self.color.abs_diff_eq(&other.color, epsilon) + && self.alpha.abs_diff_eq(&other.alpha, epsilon) } +} - fn default_max_ulps() -> u32 { - T::default_max_ulps() +impl RelativeEq for PreAlpha + where + C: RelativeEq , + T: RelativeEq + Float, + T::Epsilon: Copy, +{ + fn default_max_relative() -> Self::Epsilon { + T::default_max_relative() } fn relative_eq( @@ -168,6 +176,17 @@ where self.color.relative_eq(&other.color, epsilon, max_relative) && self.alpha.relative_eq(&other.alpha, epsilon, max_relative) } +} + +impl UlpsEq for PreAlpha + where + C: UlpsEq , + T: UlpsEq + Float, + T::Epsilon: Copy, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() + } fn ulps_eq(&self, other: &PreAlpha, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.color.ulps_eq(&other.color, epsilon, max_ulps) diff --git a/palette/src/equality.rs b/palette/src/equality.rs index 6de2f7d28..9472b2210 100644 --- a/palette/src/equality.rs +++ b/palette/src/equality.rs @@ -1,38 +1,61 @@ use num_traits::Float; -use approx::ApproxEq; + +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use {cast, Component, Lab, LabHue, Lch, RgbHue, Xyz, Yxy}; use white_point::WhitePoint; macro_rules! impl_eq { ( $self_ty: ident , [$($element: ident),+]) => { - impl ApproxEq for $self_ty - where T: Component + Float + ApproxEq, + impl AbsDiffEq for $self_ty + where T: Component + Float + AbsDiffEq, T::Epsilon: Copy + Float, - Wp: WhitePoint + Wp: WhitePoint + PartialEq { - type Epsilon = ::Epsilon; + type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } - fn default_max_relative() -> Self::Epsilon { - T::default_max_relative() + + fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { + $( self.$element.abs_diff_eq(&other.$element, epsilon) )&&+ } - fn default_max_ulps() -> u32 { - T::default_max_ulps() + fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool { + $( self.$element.abs_diff_ne(&other.$element, epsilon) )&&+ } - fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { + } + + impl RelativeEq for $self_ty + where T: Component + Float + RelativeEq, + T::Epsilon: Copy + Float, + Wp: WhitePoint + PartialEq + { + fn default_max_relative() -> T::Epsilon { + T::default_max_relative() + } + + fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { $( self.$element.relative_eq(&other.$element, epsilon, max_relative) )&&+ } - fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool{ - $( self.$element.ulps_eq(&other.$element, epsilon, max_ulps) )&&+ + fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { + $( self.$element.relative_ne(&other.$element, epsilon, max_relative) )&&+ } + } - fn relative_ne(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { - $( self.$element.relative_ne(&other.$element, epsilon, max_relative) )&&+ + impl UlpsEq for $self_ty + where T: Component + Float + UlpsEq, + T::Epsilon: Copy + Float, + Wp: WhitePoint + PartialEq + { + fn default_max_ulps() -> u32 { + T::default_max_ulps() } - fn ulps_ne(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { + + fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { + $( self.$element.ulps_eq(&other.$element, epsilon, max_ulps) )&&+ + } + fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { $( self.$element.ulps_ne(&other.$element, epsilon, max_ulps) )&&+ } } @@ -54,39 +77,58 @@ impl_eq!(Lch, [l, chroma, hue]); // ulps. Because of this we loose some precision for values close to 0.0. macro_rules! impl_eq_hue { ( $self_ty: ident ) => { - impl ApproxEq for $self_ty - where ::Epsilon: Float + impl AbsDiffEq for $self_ty + where T::Epsilon: Float { - type Epsilon = ::Epsilon; + type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() * cast(180.0) } + + fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { + let diff: T = (*self - *other).to_degrees(); + T::abs_diff_eq(&diff, &T::zero(), epsilon) + } + fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool { + let diff: T = (*self - *other).to_degrees(); + T::abs_diff_ne(&diff, &T::zero(), epsilon) + } + } + + impl RelativeEq for $self_ty + where T::Epsilon: Float + { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() * cast(180.0) } - fn default_max_ulps() -> u32 { - T::default_max_ulps() * 180 - } - fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { + + fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { let diff: T = (*self - *other).to_degrees(); T::relative_eq(&diff, &T::zero(), epsilon, max_relative) } - fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool{ + fn relative_ne(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { let diff: T = (*self - *other).to_degrees(); - T::ulps_eq(&diff, &T::zero(), epsilon, max_ulps) + T::relative_ne(&diff, &T::zero(), epsilon, max_relative) } + } - fn relative_ne(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { + impl UlpsEq for $self_ty + where T::Epsilon: Float + { + fn default_max_ulps() -> u32 { + T::default_max_ulps() * 180 + } + + fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { let diff: T = (*self - *other).to_degrees(); - T::relative_ne(&diff, &T::zero(), epsilon, max_relative) + T::ulps_eq(&diff, &T::zero(), epsilon, max_ulps) } fn ulps_ne(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { let diff: T = (*self - *other).to_degrees(); T::ulps_ne(&diff, &T::zero(), epsilon, max_ulps) } } - } } diff --git a/palette/src/gradient.rs b/palette/src/gradient.rs index 3d12436f5..f29c715ca 100644 --- a/palette/src/gradient.rs +++ b/palette/src/gradient.rs @@ -2,7 +2,7 @@ use num_traits::{Float, One, Zero}; use std::cmp::max; -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use cast; @@ -287,9 +287,9 @@ impl From<::std::ops::RangeFull> for Range { } } -impl ApproxEq for Range +impl AbsDiffEq for Range where - T: ApproxEq + Float, + T: AbsDiffEq + Float, T::Epsilon: Copy, { type Epsilon = T::Epsilon; @@ -298,12 +298,30 @@ where T::default_epsilon() } - fn default_max_relative() -> Self::Epsilon { - T::default_max_relative() + fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { + let from = match (self.from, other.from) { + (Some(s), Some(o)) => s.abs_diff_eq(&o, epsilon), + (None, None) => true, + _ => false, + }; + + let to = match (self.to, other.to) { + (Some(s), Some(o)) => s.abs_diff_eq(&o, epsilon), + (None, None) => true, + _ => false, + }; + + from && to } +} - fn default_max_ulps() -> u32 { - T::default_max_ulps() +impl RelativeEq for Range + where + T: RelativeEq + Float, + T::Epsilon: Copy, +{ + fn default_max_relative() -> Self::Epsilon { + T::default_max_relative() } fn relative_eq( @@ -326,6 +344,16 @@ where from && to } +} + +impl UlpsEq for Range + where + T: UlpsEq + Float, + T::Epsilon: Copy, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() + } fn ulps_eq(&self, other: &Range, epsilon: Self::Epsilon, max_ulps: u32) -> bool { let from = match (self.from, other.from) { diff --git a/palette/src/hsl.rs b/palette/src/hsl.rs index fac549c79..1a1d56a53 100644 --- a/palette/src/hsl.rs +++ b/palette/src/hsl.rs @@ -1,4 +1,4 @@ -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num_traits::Float; use std::any::TypeId; @@ -518,23 +518,35 @@ where } } -impl ApproxEq for Hsl +impl AbsDiffEq for Hsl where - T: Component + Float + ApproxEq, + T: Component + Float + AbsDiffEq, T::Epsilon: Copy + Float, - S: RgbSpace, + S: RgbSpace + PartialEq, { - type Epsilon = ::Epsilon; + type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } + + fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { + self.hue.abs_diff_eq(&other.hue, epsilon) && + self.saturation.abs_diff_eq(&other.saturation, epsilon) && + self.lightness.abs_diff_eq(&other.lightness, epsilon) + } +} + +impl RelativeEq for Hsl + where + T: Component + Float + RelativeEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, +{ fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } - fn default_max_ulps() -> u32 { - T::default_max_ulps() - } + #[cfg_attr(rustfmt, rustfmt_skip)] fn relative_eq( &self, @@ -543,15 +555,26 @@ where max_relative: Self::Epsilon, ) -> bool { self.hue.relative_eq(&other.hue, epsilon, max_relative) && - self.saturation.relative_eq(&other.saturation, epsilon, max_relative) && - self.lightness.relative_eq(&other.lightness, epsilon, max_relative) + self.saturation.relative_eq(&other.saturation, epsilon, max_relative) && + self.lightness.relative_eq(&other.lightness, epsilon, max_relative) + } +} + +impl UlpsEq for Hsl + where + T: Component + Float + UlpsEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() } #[cfg_attr(rustfmt, rustfmt_skip)] fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.hue.ulps_eq(&other.hue, epsilon, max_ulps) && - self.saturation.ulps_eq(&other.saturation, epsilon, max_ulps) && - self.lightness.ulps_eq(&other.lightness, epsilon, max_ulps) + self.saturation.ulps_eq(&other.saturation, epsilon, max_ulps) && + self.lightness.ulps_eq(&other.lightness, epsilon, max_ulps) } } diff --git a/palette/src/hsv.rs b/palette/src/hsv.rs index d44d54a5a..01717e560 100644 --- a/palette/src/hsv.rs +++ b/palette/src/hsv.rs @@ -1,4 +1,4 @@ -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num_traits::Float; use std::any::TypeId; @@ -529,23 +529,35 @@ where } } -impl ApproxEq for Hsv +impl AbsDiffEq for Hsv where - T: Component + Float + ApproxEq, + T: Component + Float + AbsDiffEq, T::Epsilon: Copy + Float, - S: RgbSpace, + S: RgbSpace + PartialEq, { - type Epsilon = ::Epsilon; + type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } + + fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { + self.hue.abs_diff_eq(&other.hue, epsilon) && + self.saturation.abs_diff_eq(&other.saturation, epsilon) && + self.value.abs_diff_eq(&other.value, epsilon) + } +} + +impl RelativeEq for Hsv + where + T: Component + Float + RelativeEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, +{ fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } - fn default_max_ulps() -> u32 { - T::default_max_ulps() - } + #[cfg_attr(rustfmt, rustfmt_skip)] fn relative_eq( &self, @@ -554,15 +566,26 @@ where max_relative: Self::Epsilon, ) -> bool { self.hue.relative_eq(&other.hue, epsilon, max_relative) && - self.saturation.relative_eq(&other.saturation, epsilon, max_relative) && - self.value.relative_eq(&other.value, epsilon, max_relative) + self.saturation.relative_eq(&other.saturation, epsilon, max_relative) && + self.value.relative_eq(&other.value, epsilon, max_relative) + } +} + +impl UlpsEq for Hsv + where + T: Component + Float + UlpsEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() } #[cfg_attr(rustfmt, rustfmt_skip)] fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.hue.ulps_eq(&other.hue, epsilon, max_ulps) && - self.saturation.ulps_eq(&other.saturation, epsilon, max_ulps) && - self.value.ulps_eq(&other.value, epsilon, max_ulps) + self.saturation.ulps_eq(&other.saturation, epsilon, max_ulps) && + self.value.ulps_eq(&other.value, epsilon, max_ulps) } } diff --git a/palette/src/hwb.rs b/palette/src/hwb.rs index 7333ae328..fcacfc435 100644 --- a/palette/src/hwb.rs +++ b/palette/src/hwb.rs @@ -1,4 +1,4 @@ -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num_traits::Float; use std::any::TypeId; @@ -446,23 +446,48 @@ where } } -impl ApproxEq for Hwb +impl AbsDiffEq for Hwb where - T: Component + Float + ApproxEq, + T: Component + Float + AbsDiffEq, T::Epsilon: Copy + Float, - S: RgbSpace, + S: RgbSpace + PartialEq, { - type Epsilon = ::Epsilon; + type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } + + fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { + let equal_shade = self + .whiteness + .abs_diff_eq(&other.whiteness, epsilon) + && self + .blackness + .abs_diff_eq(&other.blackness, epsilon); + + // The hue doesn't matter that much when the color is gray, and may fluctuate + // due to precision errors. This is a blunt tool, but works for now. + let is_gray = self.blackness + self.whiteness >= T::one() + || other.blackness + other.whiteness >= T::one(); + if is_gray { + equal_shade + } else { + self.hue.abs_diff_eq(&other.hue, epsilon) && equal_shade + } + } +} + +impl RelativeEq for Hwb + where + T: Component + Float + RelativeEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, +{ fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } - fn default_max_ulps() -> u32 { - T::default_max_ulps() - } + fn relative_eq( &self, other: &Self, @@ -473,8 +498,8 @@ where .whiteness .relative_eq(&other.whiteness, epsilon, max_relative) && self - .blackness - .relative_eq(&other.blackness, epsilon, max_relative); + .blackness + .relative_eq(&other.blackness, epsilon, max_relative); // The hue doesn't matter that much when the color is gray, and may fluctuate // due to precision errors. This is a blunt tool, but works for now. @@ -486,10 +511,25 @@ where self.hue.relative_eq(&other.hue, epsilon, max_relative) && equal_shade } } +} + +impl UlpsEq for Hwb + where + T: Component + Float + UlpsEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() + } fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { - let equal_shade = self.whiteness.ulps_eq(&other.whiteness, epsilon, max_ulps) - && self.blackness.ulps_eq(&other.blackness, epsilon, max_ulps); + let equal_shade = self + .whiteness + .ulps_eq(&other.whiteness, epsilon, max_ulps) + && self + .blackness + .ulps_eq(&other.blackness, epsilon, max_ulps); // The hue doesn't matter that much when the color is gray, and may fluctuate // due to precision errors. This is a blunt tool, but works for now. diff --git a/palette/src/lib.rs b/palette/src/lib.rs index 1a358d3d7..2841f4b97 100644 --- a/palette/src/lib.rs +++ b/palette/src/lib.rs @@ -158,7 +158,7 @@ extern crate serde_json; use num_traits::{Float, NumCast, ToPrimitive, Zero}; -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use blend::PreAlpha; use encoding::Linear; @@ -394,7 +394,7 @@ macro_rules! make_color { ///It's not recommended to use `Color` when full control is necessary, ///but it can easily be converted to a fixed color space in those ///cases. - #[derive(Debug)] + #[derive(Debug, PartialEq)] pub enum Color where T: Float + Component, S: RgbSpace, @@ -516,10 +516,11 @@ macro_rules! make_color { } } - impl ApproxEq for Color - where T: Float + Component + ApproxEq, + impl AbsDiffEq for Color + where T: Float + Component + AbsDiffEq, T::Epsilon: Float, - S: RgbSpace, + S: RgbSpace + PartialEq, + ::WhitePoint: PartialEq, { type Epsilon = T::Epsilon; @@ -527,12 +528,22 @@ macro_rules! make_color { T::default_epsilon() } - fn default_max_relative() -> Self::Epsilon { - T::default_max_relative() + fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { + match (*self, *other) { + $((Color::$variant(ref s), Color::$variant(ref o)) => s.abs_diff_eq(o, epsilon),)+ + _ => false + } } + } - fn default_max_ulps() -> u32 { - T::default_max_ulps() + impl RelativeEq for Color + where T: Float + Component + RelativeEq, + T::Epsilon: Float, + S: RgbSpace + PartialEq, + ::WhitePoint: PartialEq, + { + fn default_max_relative() -> Self::Epsilon { + T::default_max_relative() } fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { @@ -541,8 +552,19 @@ macro_rules! make_color { _ => false } } + } + + impl UlpsEq for Color + where T: Float + Component + UlpsEq, + T::Epsilon: Float, + S: RgbSpace + PartialEq, + ::WhitePoint: PartialEq, + { + fn default_max_ulps() -> u32 { + T::default_max_ulps() + } - fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool{ + fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { match (*self, *other) { $((Color::$variant(ref s), Color::$variant(ref o)) => s.ulps_eq(o, epsilon, max_ulps),)+ _ => false diff --git a/palette/src/luma/luma.rs b/palette/src/luma/luma.rs index 9cd32b7dc..4b1492c37 100644 --- a/palette/src/luma/luma.rs +++ b/palette/src/luma/luma.rs @@ -2,7 +2,7 @@ use std::fmt; use std::marker::PhantomData; use std::ops::{Add, Div, Mul, Sub}; -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num_traits::Float; @@ -553,23 +553,32 @@ where } } -impl ApproxEq for Luma +impl AbsDiffEq for Luma where - T: Component + ApproxEq, + T: Component + AbsDiffEq, T::Epsilon: Copy, - S: LumaStandard, + S: LumaStandard + PartialEq, { - type Epsilon = ::Epsilon; + type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } + + fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { + self.luma.abs_diff_eq(&other.luma, epsilon) + } +} + +impl RelativeEq for Luma + where + T: Component + RelativeEq, + T::Epsilon: Copy, + S: LumaStandard + PartialEq, +{ fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } - fn default_max_ulps() -> u32 { - T::default_max_ulps() - } fn relative_eq( &self, @@ -579,6 +588,17 @@ where ) -> bool { self.luma.relative_eq(&other.luma, epsilon, max_relative) } +} + +impl UlpsEq for Luma + where + T: Component + UlpsEq, + T::Epsilon: Copy, + S: LumaStandard + PartialEq, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() + } fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.luma.ulps_eq(&other.luma, epsilon, max_ulps) diff --git a/palette/src/rgb/rgb.rs b/palette/src/rgb/rgb.rs index 7f789d8cf..3e50c96d2 100644 --- a/palette/src/rgb/rgb.rs +++ b/palette/src/rgb/rgb.rs @@ -3,7 +3,7 @@ use std::fmt; use std::marker::PhantomData; use std::ops::{Add, Div, Mul, Sub}; -use approx::ApproxEq; +use approx::{AbsDiffEq, RelativeEq, UlpsEq}; use num_traits::Float; use alpha::Alpha; @@ -716,23 +716,35 @@ where } } -impl ApproxEq for Rgb +impl AbsDiffEq for Rgb where - T: Component + ApproxEq, + T: Component + AbsDiffEq, T::Epsilon: Copy, - S: RgbStandard, + S: RgbStandard + PartialEq, { - type Epsilon = ::Epsilon; + type Epsilon = T::Epsilon; fn default_epsilon() -> Self::Epsilon { T::default_epsilon() } + + fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { + self.red.abs_diff_eq(&other.red, epsilon) && + self.green.abs_diff_eq(&other.green, epsilon) && + self.blue.abs_diff_eq(&other.blue, epsilon) + } +} + +impl RelativeEq for Rgb + where + T: Component + RelativeEq, + T::Epsilon: Copy, + S: RgbStandard + PartialEq, +{ fn default_max_relative() -> Self::Epsilon { T::default_max_relative() } - fn default_max_ulps() -> u32 { - T::default_max_ulps() - } + #[cfg_attr(rustfmt, rustfmt_skip)] fn relative_eq( &self, @@ -741,15 +753,26 @@ where max_relative: Self::Epsilon, ) -> bool { self.red.relative_eq(&other.red, epsilon, max_relative) && - self.green.relative_eq(&other.green, epsilon, max_relative) && - self.blue.relative_eq(&other.blue, epsilon, max_relative) + self.green.relative_eq(&other.green, epsilon, max_relative) && + self.blue.relative_eq(&other.blue, epsilon, max_relative) + } +} + +impl UlpsEq for Rgb + where + T: Component + UlpsEq, + T::Epsilon: Copy, + S: RgbStandard + PartialEq, +{ + fn default_max_ulps() -> u32 { + T::default_max_ulps() } #[cfg_attr(rustfmt, rustfmt_skip)] fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { self.red.ulps_eq(&other.red, epsilon, max_ulps) && - self.green.ulps_eq(&other.green, epsilon, max_ulps) && - self.blue.ulps_eq(&other.blue, epsilon, max_ulps) + self.green.ulps_eq(&other.green, epsilon, max_ulps) && + self.blue.ulps_eq(&other.blue, epsilon, max_ulps) } } From de893352b4f2e859b7163e8a46ac776791809947 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 28 May 2018 10:57:01 +0200 Subject: [PATCH 4/5] Fix indentation --- palette/src/alpha.rs | 16 ++++++++-------- palette/src/blend/pre_alpha.rs | 16 ++++++++-------- palette/src/gradient.rs | 12 ++++++------ palette/src/hsl.rs | 16 ++++++++-------- palette/src/hsv.rs | 16 ++++++++-------- palette/src/hwb.rs | 16 ++++++++-------- palette/src/lib.rs | 24 ++++++++++++------------ palette/src/luma/luma.rs | 16 ++++++++-------- palette/src/rgb/rgb.rs | 16 ++++++++-------- 9 files changed, 74 insertions(+), 74 deletions(-) diff --git a/palette/src/alpha.rs b/palette/src/alpha.rs index 6d73374c5..fe0f20120 100644 --- a/palette/src/alpha.rs +++ b/palette/src/alpha.rs @@ -178,10 +178,10 @@ where } impl RelativeEq for Alpha - where - C: RelativeEq, - T: RelativeEq, - T::Epsilon: Clone, +where + C: RelativeEq, + T: RelativeEq, + T::Epsilon: Clone, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -200,10 +200,10 @@ impl RelativeEq for Alpha } impl UlpsEq for Alpha - where - C: UlpsEq, - T: UlpsEq, - T::Epsilon: Clone, +where + C: UlpsEq, + T: UlpsEq, + T::Epsilon: Clone, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/blend/pre_alpha.rs b/palette/src/blend/pre_alpha.rs index 88e44b21f..ac1338eb7 100644 --- a/palette/src/blend/pre_alpha.rs +++ b/palette/src/blend/pre_alpha.rs @@ -158,10 +158,10 @@ where } impl RelativeEq for PreAlpha - where - C: RelativeEq , - T: RelativeEq + Float, - T::Epsilon: Copy, +where + C: RelativeEq , + T: RelativeEq + Float, + T::Epsilon: Copy, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -179,10 +179,10 @@ impl RelativeEq for PreAlpha } impl UlpsEq for PreAlpha - where - C: UlpsEq , - T: UlpsEq + Float, - T::Epsilon: Copy, +where + C: UlpsEq , + T: UlpsEq + Float, + T::Epsilon: Copy, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/gradient.rs b/palette/src/gradient.rs index f29c715ca..bffadbc86 100644 --- a/palette/src/gradient.rs +++ b/palette/src/gradient.rs @@ -316,9 +316,9 @@ where } impl RelativeEq for Range - where - T: RelativeEq + Float, - T::Epsilon: Copy, +where + T: RelativeEq + Float, + T::Epsilon: Copy, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -347,9 +347,9 @@ impl RelativeEq for Range } impl UlpsEq for Range - where - T: UlpsEq + Float, - T::Epsilon: Copy, +where + T: UlpsEq + Float, + T::Epsilon: Copy, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/hsl.rs b/palette/src/hsl.rs index 1a1d56a53..325b7f35f 100644 --- a/palette/src/hsl.rs +++ b/palette/src/hsl.rs @@ -538,10 +538,10 @@ where } impl RelativeEq for Hsl - where - T: Component + Float + RelativeEq, - T::Epsilon: Copy + Float, - S: RgbSpace + PartialEq, +where + T: Component + Float + RelativeEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -561,10 +561,10 @@ impl RelativeEq for Hsl } impl UlpsEq for Hsl - where - T: Component + Float + UlpsEq, - T::Epsilon: Copy + Float, - S: RgbSpace + PartialEq, +where + T: Component + Float + UlpsEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/hsv.rs b/palette/src/hsv.rs index 01717e560..0a1de52a2 100644 --- a/palette/src/hsv.rs +++ b/palette/src/hsv.rs @@ -549,10 +549,10 @@ where } impl RelativeEq for Hsv - where - T: Component + Float + RelativeEq, - T::Epsilon: Copy + Float, - S: RgbSpace + PartialEq, +where + T: Component + Float + RelativeEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -572,10 +572,10 @@ impl RelativeEq for Hsv } impl UlpsEq for Hsv - where - T: Component + Float + UlpsEq, - T::Epsilon: Copy + Float, - S: RgbSpace + PartialEq, +where + T: Component + Float + UlpsEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/hwb.rs b/palette/src/hwb.rs index fcacfc435..c8ada0428 100644 --- a/palette/src/hwb.rs +++ b/palette/src/hwb.rs @@ -479,10 +479,10 @@ where } impl RelativeEq for Hwb - where - T: Component + Float + RelativeEq, - T::Epsilon: Copy + Float, - S: RgbSpace + PartialEq, +where + T: Component + Float + RelativeEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -514,10 +514,10 @@ impl RelativeEq for Hwb } impl UlpsEq for Hwb - where - T: Component + Float + UlpsEq, - T::Epsilon: Copy + Float, - S: RgbSpace + PartialEq, +where + T: Component + Float + UlpsEq, + T::Epsilon: Copy + Float, + S: RgbSpace + PartialEq, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/lib.rs b/palette/src/lib.rs index 2841f4b97..ae67fccfc 100644 --- a/palette/src/lib.rs +++ b/palette/src/lib.rs @@ -517,10 +517,10 @@ macro_rules! make_color { } impl AbsDiffEq for Color - where T: Float + Component + AbsDiffEq, - T::Epsilon: Float, - S: RgbSpace + PartialEq, - ::WhitePoint: PartialEq, + where T: Float + Component + AbsDiffEq, + T::Epsilon: Float, + S: RgbSpace + PartialEq, + ::WhitePoint: PartialEq, { type Epsilon = T::Epsilon; @@ -537,10 +537,10 @@ macro_rules! make_color { } impl RelativeEq for Color - where T: Float + Component + RelativeEq, - T::Epsilon: Float, - S: RgbSpace + PartialEq, - ::WhitePoint: PartialEq, + where T: Float + Component + RelativeEq, + T::Epsilon: Float, + S: RgbSpace + PartialEq, + ::WhitePoint: PartialEq, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -555,10 +555,10 @@ macro_rules! make_color { } impl UlpsEq for Color - where T: Float + Component + UlpsEq, - T::Epsilon: Float, - S: RgbSpace + PartialEq, - ::WhitePoint: PartialEq, + where T: Float + Component + UlpsEq, + T::Epsilon: Float, + S: RgbSpace + PartialEq, + ::WhitePoint: PartialEq, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/luma/luma.rs b/palette/src/luma/luma.rs index 4b1492c37..bef0cb8fd 100644 --- a/palette/src/luma/luma.rs +++ b/palette/src/luma/luma.rs @@ -571,10 +571,10 @@ where } impl RelativeEq for Luma - where - T: Component + RelativeEq, - T::Epsilon: Copy, - S: LumaStandard + PartialEq, +where + T: Component + RelativeEq, + T::Epsilon: Copy, + S: LumaStandard + PartialEq, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -591,10 +591,10 @@ impl RelativeEq for Luma } impl UlpsEq for Luma - where - T: Component + UlpsEq, - T::Epsilon: Copy, - S: LumaStandard + PartialEq, +where + T: Component + UlpsEq, + T::Epsilon: Copy, + S: LumaStandard + PartialEq, { fn default_max_ulps() -> u32 { T::default_max_ulps() diff --git a/palette/src/rgb/rgb.rs b/palette/src/rgb/rgb.rs index 3e50c96d2..e74c6fcf4 100644 --- a/palette/src/rgb/rgb.rs +++ b/palette/src/rgb/rgb.rs @@ -736,10 +736,10 @@ where } impl RelativeEq for Rgb - where - T: Component + RelativeEq, - T::Epsilon: Copy, - S: RgbStandard + PartialEq, +where + T: Component + RelativeEq, + T::Epsilon: Copy, + S: RgbStandard + PartialEq, { fn default_max_relative() -> Self::Epsilon { T::default_max_relative() @@ -759,10 +759,10 @@ impl RelativeEq for Rgb } impl UlpsEq for Rgb - where - T: Component + UlpsEq, - T::Epsilon: Copy, - S: RgbStandard + PartialEq, +where + T: Component + UlpsEq, + T::Epsilon: Copy, + S: RgbStandard + PartialEq, { fn default_max_ulps() -> u32 { T::default_max_ulps() From e42909453c013f2c760db226ac850065141a28a0 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 28 May 2018 21:38:21 +0200 Subject: [PATCH 5/5] Fix logic mistake in *_ne functions --- palette/src/equality.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/palette/src/equality.rs b/palette/src/equality.rs index 9472b2210..5e6a57439 100644 --- a/palette/src/equality.rs +++ b/palette/src/equality.rs @@ -22,7 +22,7 @@ macro_rules! impl_eq { $( self.$element.abs_diff_eq(&other.$element, epsilon) )&&+ } fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool { - $( self.$element.abs_diff_ne(&other.$element, epsilon) )&&+ + $( self.$element.abs_diff_ne(&other.$element, epsilon) )||+ } } @@ -39,7 +39,7 @@ macro_rules! impl_eq { $( self.$element.relative_eq(&other.$element, epsilon, max_relative) )&&+ } fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool { - $( self.$element.relative_ne(&other.$element, epsilon, max_relative) )&&+ + $( self.$element.relative_ne(&other.$element, epsilon, max_relative) )||+ } } @@ -56,7 +56,7 @@ macro_rules! impl_eq { $( self.$element.ulps_eq(&other.$element, epsilon, max_ulps) )&&+ } fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool { - $( self.$element.ulps_ne(&other.$element, epsilon, max_ulps) )&&+ + $( self.$element.ulps_ne(&other.$element, epsilon, max_ulps) )||+ } } }