Skip to content

Commit 5670a2d

Browse files
committed
Auto merge of #52 - Ogeon:missing_approx_eq, r=Ogeon
Add missing ApproxEq implementations #51 didn't implement `ApproxEq` for `Alpha` and `Color`, so this PR adds those implementations.
2 parents 2ee6064 + d8ec3be commit 5670a2d

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

src/alpha.rs

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::ops::{Deref, DerefMut, Add, Sub, Mul, Div};
22

33
use num::Float;
44

5+
use approx::ApproxEq;
6+
57
use {Mix, Shade, GetHue, Hue, Saturate, Limited, clamp};
68

79
///An alpha component wrapper for colors.
@@ -113,6 +115,36 @@ impl<C: Default, T: Float> Default for Alpha<C, T> {
113115
}
114116
}
115117

118+
impl<C, T> ApproxEq for Alpha<C, T> where
119+
C: ApproxEq<Epsilon=T::Epsilon>,
120+
T: ApproxEq + Float,
121+
T::Epsilon: Copy,
122+
{
123+
type Epsilon = T::Epsilon;
124+
125+
fn default_epsilon() -> Self::Epsilon {
126+
T::default_epsilon()
127+
}
128+
129+
fn default_max_relative() -> Self::Epsilon {
130+
T::default_max_relative()
131+
}
132+
133+
fn default_max_ulps() -> u32 {
134+
T::default_max_ulps()
135+
}
136+
137+
fn relative_eq(&self, other: &Alpha<C, T>, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
138+
self.color.relative_eq(&other.color, epsilon, max_relative) &&
139+
self.alpha.relative_eq(&other.alpha, epsilon, max_relative)
140+
}
141+
142+
fn ulps_eq(&self, other: &Alpha<C, T>, epsilon: Self::Epsilon, max_ulps: u32) -> bool{
143+
self.color.ulps_eq(&other.color, epsilon, max_ulps) &&
144+
self.alpha.ulps_eq(&other.alpha, epsilon, max_ulps)
145+
}
146+
}
147+
116148
impl<C: Add, T: Float> Add for Alpha<C, T> {
117149
type Output = Alpha<C::Output, T>;
118150

src/equality.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use num::Float;
22
use approx::ApproxEq;
33

44
use {Xyz, Yxy, Lab, Lch, Rgb, Hsl, Hsv, Hwb, Luma, LabHue, RgbHue, flt};
5+
use pixel::{Srgb, GammaRgb};
56

67
macro_rules! impl_eq {
78
( $self_ty: ident , [$($element: ident),+]) => {
@@ -41,10 +42,12 @@ impl_eq!( Yxy, [y, x, luma] );
4142
impl_eq!( Lab, [l, a, b] );
4243
impl_eq!( Rgb, [red, blue, green] );
4344
impl_eq!( Luma, [luma] );
44-
impl_eq!( Lch, [l, chroma] );
45+
impl_eq!( Lch, [l, chroma, hue] );
4546
impl_eq!( Hsl, [hue, saturation, lightness] );
4647
impl_eq!( Hsv, [hue, saturation, value] );
4748
impl_eq!( Hwb, [hue, whiteness, blackness] );
49+
impl_eq!( Srgb, [red, blue, green, alpha] );
50+
impl_eq!( GammaRgb, [red, blue, green, alpha, gamma] );
4851

4952
// For hues diffence is calculated and compared to zero. However due to the way floating point's
5053
// work this is not so simple

src/gradient.rs

+52
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use num::{Float, One, Zero, NumCast};
44
use std::cmp::max;
5+
use approx::ApproxEq;
56

67
use flt;
78

@@ -276,6 +277,57 @@ impl<T: Float> From<::std::ops::RangeFull> for Range<T> {
276277
}
277278
}
278279

280+
impl<T> ApproxEq for Range<T> where
281+
T: ApproxEq + Float,
282+
T::Epsilon: Copy,
283+
{
284+
type Epsilon = T::Epsilon;
285+
286+
fn default_epsilon() -> Self::Epsilon {
287+
T::default_epsilon()
288+
}
289+
290+
fn default_max_relative() -> Self::Epsilon {
291+
T::default_max_relative()
292+
}
293+
294+
fn default_max_ulps() -> u32 {
295+
T::default_max_ulps()
296+
}
297+
298+
fn relative_eq(&self, other: &Range<T>, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
299+
let from = match (self.from, other.from) {
300+
(Some(s), Some(o)) => s.relative_eq(&o, epsilon, max_relative),
301+
(None, None) => true,
302+
_ => false,
303+
};
304+
305+
let to = match (self.to, other.to) {
306+
(Some(s), Some(o)) => s.relative_eq(&o, epsilon, max_relative),
307+
(None, None) => true,
308+
_ => false,
309+
};
310+
311+
from && to
312+
}
313+
314+
fn ulps_eq(&self, other: &Range<T>, epsilon: Self::Epsilon, max_ulps: u32) -> bool{
315+
let from = match (self.from, other.from) {
316+
(Some(s), Some(o)) => s.ulps_eq(&o, epsilon, max_ulps),
317+
(None, None) => true,
318+
_ => false,
319+
};
320+
321+
let to = match (self.to, other.to) {
322+
(Some(s), Some(o)) => s.ulps_eq(&o, epsilon, max_ulps),
323+
(None, None) => true,
324+
_ => false,
325+
};
326+
327+
from && to
328+
}
329+
}
330+
279331
#[derive(Clone)]
280332
enum MaybeSlice<'a, C: Mix + Clone + 'a> {
281333
NotSlice(&'a Gradient<C>),

src/lib.rs

+35
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ extern crate phf;
5353

5454
use num::{Float, ToPrimitive, NumCast};
5555

56+
use approx::ApproxEq;
57+
5658
use pixel::{Srgb, GammaRgb};
5759

5860
pub use gradient::Gradient;
@@ -336,6 +338,39 @@ macro_rules! make_color {
336338
}
337339
}
338340

341+
impl<T> ApproxEq for Color<T> where
342+
T: Float + ApproxEq,
343+
T::Epsilon: Copy + Float,
344+
{
345+
type Epsilon = T::Epsilon;
346+
347+
fn default_epsilon() -> Self::Epsilon {
348+
T::default_epsilon()
349+
}
350+
351+
fn default_max_relative() -> Self::Epsilon {
352+
T::default_max_relative()
353+
}
354+
355+
fn default_max_ulps() -> u32 {
356+
T::default_max_ulps()
357+
}
358+
359+
fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
360+
match (*self, *other) {
361+
$((Color::$variant(ref s), Color::$variant(ref o)) => s.relative_eq(o, epsilon, max_relative),)+
362+
_ => false
363+
}
364+
}
365+
366+
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool{
367+
match (*self, *other) {
368+
$((Color::$variant(ref s), Color::$variant(ref o)) => s.ulps_eq(o, epsilon, max_ulps),)+
369+
_ => false
370+
}
371+
}
372+
}
373+
339374
$(
340375
impl<T:Float> From<$variant<T>> for Color<T> {
341376
fn from(color: $variant<T>) -> Color<T> {

0 commit comments

Comments
 (0)