Skip to content

Commit af295a6

Browse files
committed
Clean up code and make clippy changes
Add default D65 WhitePoint to Lab and Lch Collapse if else statements in color_difference.rs Use !is_empty instead of `... > 0` comparisons in gradient.rs Use strip_prefix instead of manually stripping, avoids manual indexing Remove extraneous clones, into_iters, and closures Simplify matching on single condition to if let Remove format! in favor of to_string Fix deprecated image functions in examples Change some as casts to use direct `from` casts when applicable
1 parent 7b8272e commit af295a6

File tree

15 files changed

+44
-68
lines changed

15 files changed

+44
-68
lines changed

palette/examples/hue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use palette::{FromColor, Hsl, Hue, Lch, Pixel, Srgb};
33
fn main() {
44
let mut image = image::open("example-data/input/fruits.png")
55
.expect("could not open 'example-data/input/fruits.png'")
6-
.to_rgb();
6+
.to_rgb8();
77

88
//Shift hue by 180 degrees as HSL in bottom left part, and as LCh in top
99
//right part. Notice how LCh manages to preserve the apparent lightness of

palette/examples/saturate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use image::{GenericImage, GenericImageView};
55
fn main() {
66
let mut image = image::open("example-data/input/cat.png")
77
.expect("could not open 'example-data/input/cat.png'")
8-
.to_rgb();
8+
.to_rgb8();
99

1010
let width = image.width();
1111
let height = image.height();
@@ -49,7 +49,7 @@ fn main() {
4949
}
5050
}
5151
}
52-
52+
5353
let _ = std::fs::create_dir("example-data/output");
5454
match image.save("example-data/output/saturate.png") {
5555
Ok(()) => println!("see 'example-data/output/saturate.png' for the result"),

palette/src/blend/equations.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ where
9494
Equation::Add => src_color.component_wise(&dst_color, |a, b| a + b),
9595
Equation::Subtract => src_color.component_wise(&dst_color, |a, b| a - b),
9696
Equation::ReverseSubtract => dst_color.component_wise(&src_color, |a, b| a - b),
97-
Equation::Min => source
98-
.color
99-
.component_wise(&destination.color, |a, b| a.min(b)),
100-
Equation::Max => source
101-
.color
102-
.component_wise(&destination.color, |a, b| a.max(b)),
97+
Equation::Min => source.color.component_wise(&destination.color, Float::min),
98+
Equation::Max => source.color.component_wise(&destination.color, Float::max),
10399
};
104100

105101
let alpha = match self.alpha_equation {

palette/src/color_difference.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,23 @@ pub fn get_ciede_difference<T: FloatComponent>(this: &LabColorDiff<T>, other: &L
6060

6161
let delta_h_prime: T = if c_one_prime == T::zero() || c_two_prime == T::zero() {
6262
from_f64(0.0)
63+
} else if h_prime_difference <= from_f64(180.0) {
64+
h_two_prime - h_one_prime
65+
} else if h_two_prime <= h_one_prime {
66+
h_two_prime - h_one_prime + from_f64(360.0)
6367
} else {
64-
if h_prime_difference <= from_f64(180.0) {
65-
h_two_prime - h_one_prime
66-
} else {
67-
if h_two_prime <= h_one_prime {
68-
h_two_prime - h_one_prime + from_f64(360.0)
69-
} else {
70-
h_two_prime - h_one_prime - from_f64(360.0)
71-
}
72-
}
68+
h_two_prime - h_one_prime - from_f64(360.0)
7369
};
7470

7571
let delta_big_h_prime = from_f64::<T>(2.0)
7672
* (c_one_prime * c_two_prime).sqrt()
7773
* (delta_h_prime / from_f64(2.0) * pi_over_180).sin();
7874
let h_bar_prime = if c_one_prime == T::zero() || c_two_prime == T::zero() {
7975
h_one_prime + h_two_prime
76+
} else if h_prime_difference > from_f64(180.0) {
77+
(h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0)
8078
} else {
81-
if h_prime_difference > from_f64(180.0) {
82-
(h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0)
83-
} else {
84-
(h_one_prime + h_two_prime) / from_f64(2.0)
85-
}
79+
(h_one_prime + h_two_prime) / from_f64(2.0)
8680
};
8781

8882
let l_bar = (this.l + other.l) / from_f64(2.0);
@@ -103,13 +97,7 @@ pub fn get_ciede_difference<T: FloatComponent>(this: &LabColorDiff<T>, other: &L
10397
* (-(((h_bar_prime - from_f64(275.0)) / from_f64(25.0))
10498
* ((h_bar_prime - from_f64(275.0)) / from_f64(25.0))))
10599
.exp();
106-
let c_bar_prime_pow_seven = c_bar_prime
107-
* c_bar_prime
108-
* c_bar_prime
109-
* c_bar_prime
110-
* c_bar_prime
111-
* c_bar_prime
112-
* c_bar_prime;
100+
let c_bar_prime_pow_seven = c_bar_prime.powi(7);
113101
let r_c: T = from_f64::<T>(2.0)
114102
* (c_bar_prime_pow_seven / (c_bar_prime_pow_seven + twenty_five_pow_seven)).sqrt();
115103
let r_t = -r_c * (from_f64::<T>(2.0) * delta_theta * pi_over_180).sin();

palette/src/component.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ macro_rules! convert_double_to_uint {
160160
impl IntoComponent<f32> for u8 {
161161
#[inline]
162162
fn into_component(self) -> f32 {
163-
let comp_u = self as u32 + C23;
163+
let comp_u = u32::from(self) + C23;
164164
let comp_f = f32::from_bits(comp_u) - f32::from_bits(C23);
165-
let max_u = core::u8::MAX as u32 + C23;
165+
let max_u = u32::from(core::u8::MAX) + C23;
166166
let max_f = (f32::from_bits(max_u) - f32::from_bits(C23)).recip();
167167
comp_f * max_f
168168
}
@@ -172,9 +172,9 @@ impl IntoComponent<f32> for u8 {
172172
impl IntoComponent<f64> for u8 {
173173
#[inline]
174174
fn into_component(self) -> f64 {
175-
let comp_u = self as u64 + C52;
175+
let comp_u = u64::from(self) + C52;
176176
let comp_f = f64::from_bits(comp_u) - f64::from_bits(C52);
177-
let max_u = core::u8::MAX as u64 + C52;
177+
let max_u = u64::from(core::u8::MAX) + C52;
178178
let max_f = (f64::from_bits(max_u) - f64::from_bits(C52)).recip();
179179
comp_f * max_f
180180
}
@@ -218,7 +218,7 @@ macro_rules! convert_uint_to_uint {
218218
impl IntoComponent<f64> for f32 {
219219
#[inline]
220220
fn into_component(self) -> f64 {
221-
self as f64
221+
f64::from(self)
222222
}
223223
}
224224
convert_float_to_uint!(f32; direct (u8, u16); via f64 (u32, u64, u128););

palette/src/gradient.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl<C: Mix + Clone> Gradient<C> {
3131
C::Scalar: FromF64,
3232
{
3333
let mut points: Vec<_> = colors.into_iter().map(|c| (C::Scalar::zero(), c)).collect();
34-
assert!(points.len() > 0);
34+
assert!(!points.is_empty());
3535
let step_size = C::Scalar::one() / from_f64(max(points.len() - 1, 1) as f64);
3636

3737
for (i, &mut (ref mut p, _)) in points.iter_mut().enumerate() {
@@ -45,7 +45,7 @@ impl<C: Mix + Clone> Gradient<C> {
4545
/// be at least one color and they are expected to be ordered by their
4646
/// position value.
4747
pub fn with_domain(colors: Vec<(C::Scalar, C)>) -> Gradient<C> {
48-
assert!(colors.len() > 0);
48+
assert!(!colors.is_empty());
4949

5050
//Maybe sort the colors?
5151
Gradient(colors)

palette/src/lab.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020

2121
/// CIE L\*a\*b\* (CIELAB) with an alpha component. See the [`Laba`
2222
/// implementation in `Alpha`](crate::Alpha#Laba).
23-
pub type Laba<Wp, T = f32> = Alpha<Lab<Wp, T>, T>;
23+
pub type Laba<Wp = D65, T = f32> = Alpha<Lab<Wp, T>, T>;
2424

2525
/// The CIE L\*a\*b\* (CIELAB) color space.
2626
///

palette/src/lch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020

2121
/// CIE L\*C\*h° with an alpha component. See the [`Lcha` implementation in
2222
/// `Alpha`](crate::Alpha#Lcha).
23-
pub type Lcha<Wp, T = f32> = Alpha<Lch<Wp, T>, T>;
23+
pub type Lcha<Wp = D65, T = f32> = Alpha<Lch<Wp, T>, T>;
2424

2525
/// CIE L\*C\*h°, a polar version of [CIE L\*a\*b\*](crate::Lab).
2626
///

palette/src/relative_contrast.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub trait RelativeContrast {
5252
/// The type of the contrast ratio.
5353
type Scalar: FromF64 + PartialOrd;
5454

55-
/// Calculate contrast ratio between two colors.
55+
/// Calculate the contrast ratio between two colors.
5656
fn get_contrast_ratio(&self, other: &Self) -> Self::Scalar;
5757
/// Verify the contrast between two colors satisfies SC 1.4.3. Contrast
5858
/// is at least 4.5:1 (Level AA).
@@ -81,13 +81,10 @@ pub trait RelativeContrast {
8181
}
8282
}
8383

84-
/// Calculate a ratio between two `luma` values.
84+
/// Calculate the ratio between two `luma` values.
8585
pub fn contrast_ratio<T>(luma1: T, luma2: T) -> T
8686
where
87-
T: Add<Output = T>,
88-
T: Div<Output = T>,
89-
T: FromF64,
90-
T: Component,
87+
T: Component + FromF64 + Add<Output = T> + Div<Output = T>,
9188
{
9289
if luma1 > luma2 {
9390
(luma1 + from_f64(0.05)) / (luma2 + from_f64(0.05))

palette/src/rgb/rgb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ impl<S: RgbStandard> FromStr for Rgb<S, u8> {
10601060
// Parses a color hex code of format '#ff00bb' or '#abc' into a
10611061
// Rgb<S, u8> instance.
10621062
fn from_str(hex: &str) -> Result<Self, Self::Err> {
1063-
let hex_code = if hex.starts_with('#') { &hex[1..] } else { hex };
1063+
let hex_code = hex.strip_prefix('#').map_or(hex, |stripped| stripped);
10641064
match hex_code.len() {
10651065
3 => {
10661066
let red = u8::from_str_radix(&hex_code[..1], 16)?;

0 commit comments

Comments
 (0)