Skip to content

Commit

Permalink
Derive PartialEq by hand to avoid ColorSpace: PartialEq bound
Browse files Browse the repository at this point in the history
There's no need for each `ColorSpace` to have to impl `PartialEq`
since they're zero-sized types.
  • Loading branch information
waywardmonkeys committed Dec 10, 2024
1 parent 1f8f23f commit 7ef7aea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This release has an [MSRV][] of 1.82.
* Support for the `ACEScg` colorspace. ([#54][] by [@MightyBurger][])
* `DynamicColor` gets `with_alpha` and `multiply_alpha`. ([#71][] by [@waywardmonkeys][])
* `DynamicColor` now impls `Hash` and `PartialEq`. ([#75][] by [@waywardmonkeys][])
* `AlphaColor`, `OpaqueColor`, and `PremulColor` now impl `PartialEq`. ([#76][] by [@waywardmonkeys][])
* `AlphaColor`, `OpaqueColor`, and `PremulColor` now impl `PartialEq`. ([#76][], [#86][] by [@waywardmonkeys][])
* `HueDirection` now impls `PartialEq`. ([#79][] by [@waywardmonkeys][])
* `ColorSpaceTag` and `HueDirection` now have bytemuck support. ([#81][] by [@waywardmonkeys][])

Expand Down Expand Up @@ -61,7 +61,8 @@ This is the initial release.
[#78]: https://github.com/linebender/color/pull/78
[#79]: https://github.com/linebender/color/pull/79
[#80]: https://github.com/linebender/color/pull/80
[#81]: https://github.com/linebender/color/pull/80
[#81]: https://github.com/linebender/color/pull/81
[#86]: https://github.com/linebender/color/pull/86

[Unreleased]: https://github.com/linebender/color/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/linebender/color/releases/tag/v0.1.0
Expand Down
26 changes: 23 additions & 3 deletions color/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::floatfuncs::FloatFuncs;
/// major motivation for including these is to enable weighted sums, including
/// for spline interpolation. For cylindrical color spaces, hue fixup should
/// be applied before interpolation.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(transparent)]
pub struct OpaqueColor<CS> {
Expand All @@ -38,7 +38,7 @@ pub struct OpaqueColor<CS> {
/// A color in a color space known at compile time, with an alpha channel.
///
/// See [`OpaqueColor`] for a discussion of arithmetic traits and interpolation.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(transparent)]
pub struct AlphaColor<CS> {
Expand All @@ -61,7 +61,7 @@ pub struct AlphaColor<CS> {
/// give undesirable results.
///
/// See [`OpaqueColor`] for a discussion of arithmetic traits and interpolation.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(transparent)]
pub struct PremulColor<CS> {
Expand Down Expand Up @@ -651,6 +651,26 @@ impl<CS: ColorSpace> From<OpaqueColor<CS>> for PremulColor<CS> {
}
}

// Partial equality - Hand derive to avoid needing ColorSpace to be PartialEq

impl<CS: ColorSpace> PartialEq for AlphaColor<CS> {
fn eq(&self, other: &Self) -> bool {
self.components == other.components
}
}

impl<CS: ColorSpace> PartialEq for OpaqueColor<CS> {
fn eq(&self, other: &Self) -> bool {
self.components == other.components
}
}

impl<CS: ColorSpace> PartialEq for PremulColor<CS> {
fn eq(&self, other: &Self) -> bool {
self.components == other.components
}
}

/// Multiply components by a scalar.
impl<CS: ColorSpace> core::ops::Mul<f32> for OpaqueColor<CS> {
type Output = Self;
Expand Down

0 comments on commit 7ef7aea

Please # to comment.