Skip to content

Commit 49a0494

Browse files
bors[bot]okaneco
andauthored
Merge #211
211: Implement PartialEq/Eq for all colorspaces, Alpha, PreAlpha, and LabHue/RgbHue r=Ogeon a=okaneco Implement PartialEq/Eq for: - Alpha, PreAlpha, Luma, Rgb, Hsl, Hsv, LabHue/RgbHue, Hwb, Lab, Lch, Xyz, Yxy closes #206 Co-authored-by: okaneco <47607823+okaneco@users.noreply.github.com>
2 parents 6795d97 + a1ee6a1 commit 49a0494

File tree

12 files changed

+202
-11
lines changed

12 files changed

+202
-11
lines changed

palette/src/alpha/alpha.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
};
2020

2121
/// An alpha component wrapper for colors.
22-
#[derive(Clone, Copy, Debug, PartialEq)]
22+
#[derive(Clone, Copy, Debug)]
2323
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
2424
#[repr(C)]
2525
pub struct Alpha<C, T> {
@@ -44,6 +44,23 @@ impl<C, T: Component> Alpha<C, T> {
4444
}
4545
}
4646

47+
impl<C, T> PartialEq for Alpha<C, T>
48+
where
49+
T: PartialEq,
50+
C: PartialEq,
51+
{
52+
fn eq(&self, other: &Self) -> bool {
53+
self.color == other.color && self.alpha == other.alpha
54+
}
55+
}
56+
57+
impl<C, T> Eq for Alpha<C, T>
58+
where
59+
T: Eq,
60+
C: Eq,
61+
{
62+
}
63+
4764
impl<C1: WithAlpha<T>, C2, T: Component> FromColorUnclamped<C1> for Alpha<C2, T>
4865
where
4966
C1::Color: IntoColorUnclamped<C2>,

palette/src/blend/pre_alpha.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{clamp, Alpha, Blend, ComponentWise, Mix, Pixel};
2727
///
2828
/// Note that converting to and from premultiplied alpha will cause the alpha
2929
/// component to be clamped to [0.0, 1.0].
30-
#[derive(Clone, Copy, PartialEq, Debug)]
30+
#[derive(Clone, Copy, Debug)]
3131
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3232
#[repr(C)]
3333
pub struct PreAlpha<C, T: Float> {
@@ -40,6 +40,23 @@ pub struct PreAlpha<C, T: Float> {
4040
pub alpha: T,
4141
}
4242

43+
impl<C, T> PartialEq for PreAlpha<C, T>
44+
where
45+
T: Float + PartialEq,
46+
C: PartialEq,
47+
{
48+
fn eq(&self, other: &Self) -> bool {
49+
self.color == other.color && self.alpha == other.alpha
50+
}
51+
}
52+
53+
impl<C, T> Eq for PreAlpha<C, T>
54+
where
55+
T: Float + Eq,
56+
C: Eq,
57+
{
58+
}
59+
4360
impl<C, T> From<Alpha<C, T>> for PreAlpha<C, T>
4461
where
4562
C: ComponentWise<Scalar = T>,

palette/src/hsl.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub type Hsla<S = Srgb, T = f32> = Alpha<Hsl<S, T>, T>;
3535
///
3636
/// See [HSV](crate::Hsv) for a very similar color space, with brightness
3737
/// instead of lightness.
38-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
38+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3939
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
4040
#[palette(
4141
palette_internal,
@@ -158,6 +158,23 @@ where
158158
}
159159
}
160160

161+
impl<S, T> PartialEq for Hsl<S, T>
162+
where
163+
T: FloatComponent + PartialEq,
164+
S: RgbStandard,
165+
{
166+
fn eq(&self, other: &Self) -> bool {
167+
self.hue == other.hue && self.saturation == other.saturation && self.lightness == other.lightness
168+
}
169+
}
170+
171+
impl<S, T> Eq for Hsl<S, T>
172+
where
173+
T: FloatComponent + Eq,
174+
S: RgbStandard,
175+
{
176+
}
177+
161178
///<span id="Hsla"></span>[`Hsla`](crate::Hsla) implementations.
162179
impl<T, A> Alpha<Hsl<Srgb, T>, A>
163180
where

palette/src/hsv.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub type Hsva<S = Srgb, T = f32> = Alpha<Hsv<S, T>, T>;
3232
/// _lightness_. The difference is that, for example, red (100% R, 0% G, 0% B)
3333
/// and white (100% R, 100% G, 100% B) has the same brightness (or value), but
3434
/// not the same lightness.
35-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
35+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3636
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3737
#[palette(
3838
palette_internal,
@@ -155,6 +155,23 @@ where
155155
}
156156
}
157157

158+
impl<S, T> PartialEq for Hsv<S, T>
159+
where
160+
T: FloatComponent + PartialEq,
161+
S: RgbStandard,
162+
{
163+
fn eq(&self, other: &Self) -> bool {
164+
self.hue == other.hue && self.saturation == other.saturation && self.value == other.value
165+
}
166+
}
167+
168+
impl<S, T> Eq for Hsv<S, T>
169+
where
170+
T: FloatComponent + Eq,
171+
S: RgbStandard,
172+
{
173+
}
174+
158175
///<span id="Hsva"></span>[`Hsva`](crate::Hsva) implementations.
159176
impl<T, A> Alpha<Hsv<Srgb, T>, A>
160177
where

palette/src/hues.rs

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ macro_rules! make_hues {
119119
}
120120
}
121121

122+
impl<T: Float + FromF64 + Eq> Eq for $name<T> {}
123+
122124
impl<T: Float> Add<$name<T>> for $name<T> {
123125
type Output = $name<T>;
124126

palette/src/hwb.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub type Hwba<S = Srgb, T = f32> = Alpha<Hwb<S, T>, T>;
3333
///
3434
/// It is very intuitive for humans to use and many color-pickers are based on
3535
/// the HWB color system
36-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
36+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3737
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3838
#[palette(
3939
palette_internal,
@@ -161,6 +161,25 @@ where
161161
}
162162
}
163163

164+
impl<S, T> PartialEq for Hwb<S, T>
165+
where
166+
T: FloatComponent + PartialEq,
167+
S: RgbStandard,
168+
{
169+
fn eq(&self, other: &Self) -> bool {
170+
self.hue == other.hue
171+
&& self.whiteness == other.whiteness
172+
&& self.blackness == other.blackness
173+
}
174+
}
175+
176+
impl<S, T> Eq for Hwb<S, T>
177+
where
178+
T: FloatComponent + Eq,
179+
S: RgbStandard,
180+
{
181+
}
182+
164183
///<span id="Hwba"></span>[`Hwba`](crate::Hwba) implementations.
165184
impl<T, A> Alpha<Hwb<Srgb, T>, A>
166185
where

palette/src/lab.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub type Laba<Wp = D65, T = f32> = Alpha<Lab<Wp, T>, T>;
3333
///
3434
/// The parameters of L\*a\*b\* are quite different, compared to many other
3535
/// color spaces, so manipulating them manually may be unintuitive.
36-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
36+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3737
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3838
#[palette(
3939
palette_internal,
@@ -152,6 +152,23 @@ where
152152
}
153153
}
154154

155+
impl<Wp, T> PartialEq for Lab<Wp, T>
156+
where
157+
T: FloatComponent + PartialEq,
158+
Wp: WhitePoint,
159+
{
160+
fn eq(&self, other: &Self) -> bool {
161+
self.l == other.l && self.a == other.a && self.b == other.b
162+
}
163+
}
164+
165+
impl<Wp, T> Eq for Lab<Wp, T>
166+
where
167+
T: FloatComponent + Eq,
168+
Wp: WhitePoint,
169+
{
170+
}
171+
155172
///<span id="Laba"></span>[`Laba`](crate::Laba) implementations.
156173
impl<T, A> Alpha<Lab<D65, T>, A>
157174
where

palette/src/lch.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub type Lcha<Wp = D65, T = f32> = Alpha<Lch<Wp, T>, T>;
2828
/// it's a cylindrical color space, like [HSL](crate::Hsl) and
2929
/// [HSV](crate::Hsv). This gives it the same ability to directly change
3030
/// the hue and colorfulness of a color, while preserving other visual aspects.
31-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
31+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3232
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3333
#[palette(
3434
palette_internal,
@@ -151,6 +151,23 @@ where
151151
}
152152
}
153153

154+
impl<Wp, T> PartialEq for Lch<Wp, T>
155+
where
156+
T: FloatComponent + PartialEq,
157+
Wp: WhitePoint,
158+
{
159+
fn eq(&self, other: &Self) -> bool {
160+
self.l == other.l && self.chroma == other.chroma && self.hue == other.hue
161+
}
162+
}
163+
164+
impl<Wp, T> Eq for Lch<Wp, T>
165+
where
166+
T: FloatComponent + Eq,
167+
Wp: WhitePoint,
168+
{
169+
}
170+
154171
///<span id="Lcha"></span>[`Lcha`](crate::Lcha) implementations.
155172
impl<T, A> Alpha<Lch<D65, T>, A>
156173
where

palette/src/luma/luma.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub type Lumaa<S = Srgb, T = f32> = Alpha<Luma<S, T>, T>;
3333
/// perceived to be. It's basically the `Y` component of [CIE
3434
/// XYZ](crate::Xyz). The lack of any form of hue representation limits
3535
/// the set of operations that can be performed on it.
36-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
36+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3737
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3838
#[palette(
3939
palette_internal,
@@ -166,6 +166,23 @@ where
166166
}
167167
}
168168

169+
impl<S, T> PartialEq for Luma<S, T>
170+
where
171+
T: Component + PartialEq,
172+
S: LumaStandard,
173+
{
174+
fn eq(&self, other: &Self) -> bool {
175+
self.luma == other.luma
176+
}
177+
}
178+
179+
impl<S, T> Eq for Luma<S, T>
180+
where
181+
T: Component + Eq,
182+
S: LumaStandard,
183+
{
184+
}
185+
169186
///<span id="Lumaa"></span>[`Lumaa`](crate::luma::Lumaa) implementations.
170187
impl<S, T, A> Alpha<Luma<S, T>, A>
171188
where

palette/src/rgb/rgb.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub type Rgba<S = Srgb, T = f32> = Alpha<Rgb<S, T>, T>;
4343
/// linear, meaning that gamma correction is required when converting to and
4444
/// from a displayable RGB, such as sRGB. See the [`pixel`](crate::encoding::pixel)
4545
/// module for encoding formats.
46-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
46+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
4747
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
4848
#[palette(
4949
palette_internal,
@@ -154,6 +154,23 @@ impl<S: RgbStandard, T: Component> Rgb<S, T> {
154154
}
155155
}
156156

157+
impl<S, T> PartialEq for Rgb<S, T>
158+
where
159+
T: Component + PartialEq,
160+
S: RgbStandard,
161+
{
162+
fn eq(&self, other: &Self) -> bool {
163+
self.red == other.red && self.green == other.green && self.blue == other.blue
164+
}
165+
}
166+
167+
impl<S, T> Eq for Rgb<S, T>
168+
where
169+
T: Component + Eq,
170+
S: RgbStandard,
171+
{
172+
}
173+
157174
/// Convenience functions to convert between a packed `u32` and `Rgb`.
158175
///
159176
/// ```

palette/src/xyz.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub type Xyza<Wp = D65, T = f32> = Alpha<Xyz<Wp, T>, T>;
3232
///
3333
/// Conversions and operations on this color space depend on the defined white
3434
/// point
35-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
35+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3636
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3737
#[palette(
3838
palette_internal,
@@ -157,6 +157,23 @@ where
157157
}
158158
}
159159

160+
impl<Wp, T> PartialEq for Xyz<Wp, T>
161+
where
162+
T: FloatComponent + PartialEq,
163+
Wp: WhitePoint,
164+
{
165+
fn eq(&self, other: &Self) -> bool {
166+
self.x == other.x && self.y == other.y && self.z == other.z
167+
}
168+
}
169+
170+
impl<Wp, T> Eq for Xyz<Wp, T>
171+
where
172+
T: FloatComponent + Eq,
173+
Wp: WhitePoint,
174+
{
175+
}
176+
160177
///<span id="Xyza"></span>[`Xyza`](crate::Xyza) implementations.
161178
impl<T, A> Alpha<Xyz<D65, T>, A>
162179
where

palette/src/yxy.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub type Yxya<Wp = D65, T = f32> = Alpha<Yxy<Wp, T>, T>;
2828
/// for the color spaces are a plot of this color space's x and y coordinates.
2929
///
3030
/// Conversions and operations on this color space depend on the white point.
31-
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
31+
#[derive(Debug, Pixel, FromColorUnclamped, WithAlpha)]
3232
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
3333
#[palette(
3434
palette_internal,
@@ -150,6 +150,23 @@ where
150150
}
151151
}
152152

153+
impl<Wp, T> PartialEq for Yxy<Wp, T>
154+
where
155+
T: FloatComponent + PartialEq,
156+
Wp: WhitePoint,
157+
{
158+
fn eq(&self, other: &Self) -> bool {
159+
self.luma == other.luma && self.x == other.x && self.y == other.y
160+
}
161+
}
162+
163+
impl<Wp, T> Eq for Yxy<Wp, T>
164+
where
165+
T: FloatComponent + Eq,
166+
Wp: WhitePoint,
167+
{
168+
}
169+
153170
///<span id="Yxya"></span>[`Yxya`](crate::Yxya) implementations.
154171
impl<T, A> Alpha<Yxy<D65, T>, A>
155172
where

0 commit comments

Comments
 (0)