Skip to content

Commit 2337cf3

Browse files
committed
Relax trait bounds for all color types
1 parent 1a96f24 commit 2337cf3

28 files changed

+1749
-3492
lines changed

palette/examples/random.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55

66
#[cfg(feature = "random")]
77
fn main() {
8-
use palette::{FromColor, Hsl, Hsv, Hwb, Pixel, RgbHue, Srgb};
8+
use palette::{Hsl, Hsv, Hwb, IntoColor, Pixel, RgbHue, Srgb};
99

1010
use image::{GenericImage, GenericImageView, RgbImage};
1111
use rand::Rng;
@@ -19,7 +19,7 @@ fn main() {
1919
let (width, height) = sub_image.dimensions();
2020
for x in 0..width {
2121
for y in 0..height {
22-
let random_color = Srgb::<f32>::new(rng.gen(), rng.gen(), rng.gen());
22+
let random_color: Srgb = Srgb::new(rng.gen(), rng.gen(), rng.gen());
2323
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
2424
}
2525
}
@@ -30,7 +30,7 @@ fn main() {
3030
let (width, height) = sub_image.dimensions();
3131
for x in 0..width {
3232
for y in 0..height {
33-
let random_color = rng.gen::<Srgb>();
33+
let random_color: Srgb = rng.gen();
3434
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
3535
}
3636
}
@@ -42,8 +42,8 @@ fn main() {
4242
let (width, height) = sub_image.dimensions();
4343
for x in 0..width {
4444
for y in 0..height {
45-
let random_color =
46-
Srgb::from_color(Hsv::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()));
45+
let random_color: Srgb =
46+
Hsv::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()).into_color();
4747
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
4848
}
4949
}
@@ -54,7 +54,7 @@ fn main() {
5454
let (width, height) = sub_image.dimensions();
5555
for x in 0..width {
5656
for y in 0..height {
57-
let random_color = Srgb::from_color(rng.gen::<Hsv>());
57+
let random_color: Srgb = rng.gen::<Hsv>().into_color();
5858
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
5959
}
6060
}
@@ -66,8 +66,8 @@ fn main() {
6666
let (width, height) = sub_image.dimensions();
6767
for x in 0..width {
6868
for y in 0..height {
69-
let random_color =
70-
Srgb::from_color(Hsl::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()));
69+
let random_color: Srgb =
70+
Hsl::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()).into_color();
7171
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
7272
}
7373
}
@@ -78,7 +78,7 @@ fn main() {
7878
let (width, height) = sub_image.dimensions();
7979
for x in 0..width {
8080
for y in 0..height {
81-
let random_color = Srgb::from_color(rng.gen::<Hsl>());
81+
let random_color: Srgb = rng.gen::<Hsl>().into_color();
8282
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
8383
}
8484
}
@@ -90,8 +90,8 @@ fn main() {
9090
let (width, height) = sub_image.dimensions();
9191
for x in 0..width {
9292
for y in 0..height {
93-
let random_color =
94-
Srgb::from_color(Hwb::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()));
93+
let random_color: Srgb =
94+
Hwb::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()).into_color();
9595
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
9696
}
9797
}
@@ -102,7 +102,7 @@ fn main() {
102102
let (width, height) = sub_image.dimensions();
103103
for x in 0..width {
104104
for y in 0..height {
105-
let random_color = Srgb::from_color(rng.gen::<Hwb>());
105+
let random_color: Srgb = rng.gen::<Hwb>().into_color();
106106
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
107107
}
108108
}

palette/src/alpha.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::Component;
22

3+
use num_traits::Zero;
34
#[doc(hidden)]
45
pub use palette_derive::WithAlpha;
56

@@ -67,7 +68,7 @@ mod alpha;
6768
///
6869
/// assert_eq!(transparent.alpha, 10);
6970
/// ```
70-
pub trait WithAlpha<A: Component>: Sized {
71+
pub trait WithAlpha<A>: Sized {
7172
/// The opaque color type, without any transparency.
7273
///
7374
/// This is typically `Self`.
@@ -142,7 +143,10 @@ pub trait WithAlpha<A: Component>: Sized {
142143
/// let opaque: Srgba<u8> = color.opaque();
143144
/// assert_eq!(opaque.alpha, 255);
144145
/// ```
145-
fn opaque(self) -> Self::WithAlpha {
146+
fn opaque(self) -> Self::WithAlpha
147+
where
148+
A: Component,
149+
{
146150
self.with_alpha(A::max_intensity())
147151
}
148152

@@ -157,7 +161,10 @@ pub trait WithAlpha<A: Component>: Sized {
157161
/// let transparent: Srgba<u8> = color.transparent();
158162
/// assert_eq!(transparent.alpha, 0);
159163
/// ```
160-
fn transparent(self) -> Self::WithAlpha {
164+
fn transparent(self) -> Self::WithAlpha
165+
where
166+
A: Zero,
167+
{
161168
self.with_alpha(A::zero())
162169
}
163170
}

palette/src/convert.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
//! ```
1818
//! # use palette::rgb::{RgbStandard, RgbSpace};
1919
//! # use palette::convert::FromColorUnclamped;
20-
//! # use palette::{Xyz, Component, FloatComponent};
20+
//! # use palette::{Xyz, FloatComponent};
2121
//! #
2222
//! #[palette(
2323
//! component = "T",
2424
//! rgb_standard = "S",
25-
//! white_point = "<S::Space as RgbSpace>::WhitePoint",
2625
//! )]
2726
//! #[derive(FromColorUnclamped)]
2827
//! #[repr(C)]
29-
//! struct ExampleType<S: RgbStandard, T: Component> {
28+
//! struct ExampleType<S, T> {
3029
//! // ...
3130
//! #[palette(alpha)]
3231
//! alpha: T,
@@ -73,6 +72,9 @@
7372
//! or a best effort to convert between standards, but sometimes it has to be set
7473
//! to a specific type. This also accepts type parameters.
7574
//!
75+
//! * `luma_standard = "some::rgb_standard::Type"`: Sets the Luma standard
76+
//! type that should be used when deriving, similar to `rgb_standard`.
77+
//!
7678
//! ## Field Attributes
7779
//!
7880
//! * `alpha`: Specifies field as the color's transparency value.

palette/src/equality.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
22

33
use crate::float::Float;
4-
use crate::white_point::WhitePoint;
54
use crate::{
65
from_f64, FloatComponent, FromF64, Hsluv, Lab, LabHue, Lch, Lchuv, Luv, LuvHue, OklabHue,
76
RgbHue, Xyz, Yxy,
87
};
98

109
macro_rules! impl_eq {
1110
( $self_ty: ident , [$($element: ident),+]) => {
11+
12+
impl<Wp, T> PartialEq for $self_ty<Wp, T>
13+
where
14+
T: FloatComponent + PartialEq,
15+
{
16+
fn eq(&self, other: &Self) -> bool {
17+
$( self.$element == other.$element )&&+
18+
}
19+
}
20+
21+
impl<S, T> Eq for $self_ty<S, T> where T: FloatComponent + Eq {}
22+
1223
impl<Wp, T> AbsDiffEq for $self_ty<Wp, T>
1324
where T: FloatComponent + AbsDiffEq,
14-
T::Epsilon: Copy + FloatComponent,
15-
Wp: WhitePoint + PartialEq
25+
T::Epsilon: Clone + FloatComponent,
1626
{
1727
type Epsilon = T::Epsilon;
1828

@@ -21,44 +31,42 @@ macro_rules! impl_eq {
2131
}
2232

2333
fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool {
24-
$( self.$element.abs_diff_eq(&other.$element, epsilon) )&&+
34+
$( self.$element.abs_diff_eq(&other.$element, epsilon.clone()) )&&+
2535
}
2636
fn abs_diff_ne(&self, other: &Self, epsilon: T::Epsilon) -> bool {
27-
$( self.$element.abs_diff_ne(&other.$element, epsilon) )||+
37+
$( self.$element.abs_diff_ne(&other.$element, epsilon.clone()) )||+
2838
}
2939
}
3040

3141
impl<Wp, T> RelativeEq for $self_ty<Wp, T>
3242
where T: FloatComponent + RelativeEq,
33-
T::Epsilon: Copy + FloatComponent,
34-
Wp: WhitePoint + PartialEq
43+
T::Epsilon: Clone + FloatComponent,
3544
{
3645
fn default_max_relative() -> T::Epsilon {
3746
T::default_max_relative()
3847
}
3948

4049
fn relative_eq(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
41-
$( self.$element.relative_eq(&other.$element, epsilon, max_relative) )&&+
50+
$( self.$element.relative_eq(&other.$element, epsilon.clone(), max_relative) )&&+
4251
}
4352
fn relative_ne(&self, other: &Self, epsilon: T::Epsilon, max_relative: T::Epsilon) -> bool {
44-
$( self.$element.relative_ne(&other.$element, epsilon, max_relative) )||+
53+
$( self.$element.relative_ne(&other.$element, epsilon.clone(), max_relative) )||+
4554
}
4655
}
4756

4857
impl<Wp, T> UlpsEq for $self_ty<Wp, T>
4958
where T: FloatComponent + UlpsEq,
50-
T::Epsilon: Copy + FloatComponent,
51-
Wp: WhitePoint + PartialEq
59+
T::Epsilon: Clone + FloatComponent,
5260
{
5361
fn default_max_ulps() -> u32 {
5462
T::default_max_ulps()
5563
}
5664

5765
fn ulps_eq(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
58-
$( self.$element.ulps_eq(&other.$element, epsilon, max_ulps) )&&+
66+
$( self.$element.ulps_eq(&other.$element, epsilon.clone(), max_ulps) )&&+
5967
}
6068
fn ulps_ne(&self, other: &Self, epsilon: T::Epsilon, max_ulps: u32) -> bool {
61-
$( self.$element.ulps_ne(&other.$element, epsilon, max_ulps) )||+
69+
$( self.$element.ulps_ne(&other.$element, epsilon.clone(), max_ulps) )||+
6270
}
6371
}
6472
}

0 commit comments

Comments
 (0)