From af295a65ce5b8c356bd832385ca8ab9e898dd4b3 Mon Sep 17 00:00:00 2001 From: okaneco <47607823+okaneco@users.noreply.github.com> Date: Thu, 19 Nov 2020 16:02:28 -0500 Subject: [PATCH] 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 --- palette/examples/hue.rs | 2 +- palette/examples/saturate.rs | 4 +-- palette/src/blend/equations.rs | 8 ++--- palette/src/color_difference.rs | 30 ++++++------------- palette/src/component.rs | 10 +++---- palette/src/gradient.rs | 4 +-- palette/src/lab.rs | 2 +- palette/src/lch.rs | 2 +- palette/src/relative_contrast.rs | 9 ++---- palette/src/rgb/rgb.rs | 2 +- palette_derive/src/alpha/with_alpha.rs | 2 +- .../src/convert/from_color_unclamped.rs | 4 +-- palette_derive/src/encoding/pixel.rs | 27 ++++++++--------- palette_derive/src/meta/mod.rs | 2 +- palette_derive/src/util.rs | 4 +-- 15 files changed, 44 insertions(+), 68 deletions(-) diff --git a/palette/examples/hue.rs b/palette/examples/hue.rs index 7657f92ba..75146ecf0 100644 --- a/palette/examples/hue.rs +++ b/palette/examples/hue.rs @@ -3,7 +3,7 @@ use palette::{FromColor, Hsl, Hue, Lch, Pixel, Srgb}; fn main() { let mut image = image::open("example-data/input/fruits.png") .expect("could not open 'example-data/input/fruits.png'") - .to_rgb(); + .to_rgb8(); //Shift hue by 180 degrees as HSL in bottom left part, and as LCh in top //right part. Notice how LCh manages to preserve the apparent lightness of diff --git a/palette/examples/saturate.rs b/palette/examples/saturate.rs index 55192421f..bb7c8a854 100644 --- a/palette/examples/saturate.rs +++ b/palette/examples/saturate.rs @@ -5,7 +5,7 @@ use image::{GenericImage, GenericImageView}; fn main() { let mut image = image::open("example-data/input/cat.png") .expect("could not open 'example-data/input/cat.png'") - .to_rgb(); + .to_rgb8(); let width = image.width(); let height = image.height(); @@ -49,7 +49,7 @@ fn main() { } } } - + let _ = std::fs::create_dir("example-data/output"); match image.save("example-data/output/saturate.png") { Ok(()) => println!("see 'example-data/output/saturate.png' for the result"), diff --git a/palette/src/blend/equations.rs b/palette/src/blend/equations.rs index 24c7347f4..c1aadbe77 100644 --- a/palette/src/blend/equations.rs +++ b/palette/src/blend/equations.rs @@ -94,12 +94,8 @@ where Equation::Add => src_color.component_wise(&dst_color, |a, b| a + b), Equation::Subtract => src_color.component_wise(&dst_color, |a, b| a - b), Equation::ReverseSubtract => dst_color.component_wise(&src_color, |a, b| a - b), - Equation::Min => source - .color - .component_wise(&destination.color, |a, b| a.min(b)), - Equation::Max => source - .color - .component_wise(&destination.color, |a, b| a.max(b)), + Equation::Min => source.color.component_wise(&destination.color, Float::min), + Equation::Max => source.color.component_wise(&destination.color, Float::max), }; let alpha = match self.alpha_equation { diff --git a/palette/src/color_difference.rs b/palette/src/color_difference.rs index f6adabd3e..f6d134591 100644 --- a/palette/src/color_difference.rs +++ b/palette/src/color_difference.rs @@ -60,16 +60,12 @@ pub fn get_ciede_difference(this: &LabColorDiff, other: &L let delta_h_prime: T = if c_one_prime == T::zero() || c_two_prime == T::zero() { from_f64(0.0) + } else if h_prime_difference <= from_f64(180.0) { + h_two_prime - h_one_prime + } else if h_two_prime <= h_one_prime { + h_two_prime - h_one_prime + from_f64(360.0) } else { - if h_prime_difference <= from_f64(180.0) { - h_two_prime - h_one_prime - } else { - if h_two_prime <= h_one_prime { - h_two_prime - h_one_prime + from_f64(360.0) - } else { - h_two_prime - h_one_prime - from_f64(360.0) - } - } + h_two_prime - h_one_prime - from_f64(360.0) }; let delta_big_h_prime = from_f64::(2.0) @@ -77,12 +73,10 @@ pub fn get_ciede_difference(this: &LabColorDiff, other: &L * (delta_h_prime / from_f64(2.0) * pi_over_180).sin(); let h_bar_prime = if c_one_prime == T::zero() || c_two_prime == T::zero() { h_one_prime + h_two_prime + } else if h_prime_difference > from_f64(180.0) { + (h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0) } else { - if h_prime_difference > from_f64(180.0) { - (h_one_prime + h_two_prime + from_f64(360.0)) / from_f64(2.0) - } else { - (h_one_prime + h_two_prime) / from_f64(2.0) - } + (h_one_prime + h_two_prime) / from_f64(2.0) }; let l_bar = (this.l + other.l) / from_f64(2.0); @@ -103,13 +97,7 @@ pub fn get_ciede_difference(this: &LabColorDiff, other: &L * (-(((h_bar_prime - from_f64(275.0)) / from_f64(25.0)) * ((h_bar_prime - from_f64(275.0)) / from_f64(25.0)))) .exp(); - let c_bar_prime_pow_seven = c_bar_prime - * c_bar_prime - * c_bar_prime - * c_bar_prime - * c_bar_prime - * c_bar_prime - * c_bar_prime; + let c_bar_prime_pow_seven = c_bar_prime.powi(7); let r_c: T = from_f64::(2.0) * (c_bar_prime_pow_seven / (c_bar_prime_pow_seven + twenty_five_pow_seven)).sqrt(); let r_t = -r_c * (from_f64::(2.0) * delta_theta * pi_over_180).sin(); diff --git a/palette/src/component.rs b/palette/src/component.rs index 38cf96295..1b1068c09 100644 --- a/palette/src/component.rs +++ b/palette/src/component.rs @@ -160,9 +160,9 @@ macro_rules! convert_double_to_uint { impl IntoComponent for u8 { #[inline] fn into_component(self) -> f32 { - let comp_u = self as u32 + C23; + let comp_u = u32::from(self) + C23; let comp_f = f32::from_bits(comp_u) - f32::from_bits(C23); - let max_u = core::u8::MAX as u32 + C23; + let max_u = u32::from(core::u8::MAX) + C23; let max_f = (f32::from_bits(max_u) - f32::from_bits(C23)).recip(); comp_f * max_f } @@ -172,9 +172,9 @@ impl IntoComponent for u8 { impl IntoComponent for u8 { #[inline] fn into_component(self) -> f64 { - let comp_u = self as u64 + C52; + let comp_u = u64::from(self) + C52; let comp_f = f64::from_bits(comp_u) - f64::from_bits(C52); - let max_u = core::u8::MAX as u64 + C52; + let max_u = u64::from(core::u8::MAX) + C52; let max_f = (f64::from_bits(max_u) - f64::from_bits(C52)).recip(); comp_f * max_f } @@ -218,7 +218,7 @@ macro_rules! convert_uint_to_uint { impl IntoComponent for f32 { #[inline] fn into_component(self) -> f64 { - self as f64 + f64::from(self) } } convert_float_to_uint!(f32; direct (u8, u16); via f64 (u32, u64, u128);); diff --git a/palette/src/gradient.rs b/palette/src/gradient.rs index 5a3ea53e3..6c36e802d 100644 --- a/palette/src/gradient.rs +++ b/palette/src/gradient.rs @@ -31,7 +31,7 @@ impl Gradient { C::Scalar: FromF64, { let mut points: Vec<_> = colors.into_iter().map(|c| (C::Scalar::zero(), c)).collect(); - assert!(points.len() > 0); + assert!(!points.is_empty()); let step_size = C::Scalar::one() / from_f64(max(points.len() - 1, 1) as f64); for (i, &mut (ref mut p, _)) in points.iter_mut().enumerate() { @@ -45,7 +45,7 @@ impl Gradient { /// be at least one color and they are expected to be ordered by their /// position value. pub fn with_domain(colors: Vec<(C::Scalar, C)>) -> Gradient { - assert!(colors.len() > 0); + assert!(!colors.is_empty()); //Maybe sort the colors? Gradient(colors) diff --git a/palette/src/lab.rs b/palette/src/lab.rs index 643da4f67..c587757b0 100644 --- a/palette/src/lab.rs +++ b/palette/src/lab.rs @@ -20,7 +20,7 @@ use crate::{ /// CIE L\*a\*b\* (CIELAB) with an alpha component. See the [`Laba` /// implementation in `Alpha`](crate::Alpha#Laba). -pub type Laba = Alpha, T>; +pub type Laba = Alpha, T>; /// The CIE L\*a\*b\* (CIELAB) color space. /// diff --git a/palette/src/lch.rs b/palette/src/lch.rs index 1de2813aa..541c60468 100644 --- a/palette/src/lch.rs +++ b/palette/src/lch.rs @@ -20,7 +20,7 @@ use crate::{ /// CIE L\*C\*h° with an alpha component. See the [`Lcha` implementation in /// `Alpha`](crate::Alpha#Lcha). -pub type Lcha = Alpha, T>; +pub type Lcha = Alpha, T>; /// CIE L\*C\*h°, a polar version of [CIE L\*a\*b\*](crate::Lab). /// diff --git a/palette/src/relative_contrast.rs b/palette/src/relative_contrast.rs index 9c817d38d..f5799b88c 100644 --- a/palette/src/relative_contrast.rs +++ b/palette/src/relative_contrast.rs @@ -52,7 +52,7 @@ pub trait RelativeContrast { /// The type of the contrast ratio. type Scalar: FromF64 + PartialOrd; - /// Calculate contrast ratio between two colors. + /// Calculate the contrast ratio between two colors. fn get_contrast_ratio(&self, other: &Self) -> Self::Scalar; /// Verify the contrast between two colors satisfies SC 1.4.3. Contrast /// is at least 4.5:1 (Level AA). @@ -81,13 +81,10 @@ pub trait RelativeContrast { } } -/// Calculate a ratio between two `luma` values. +/// Calculate the ratio between two `luma` values. pub fn contrast_ratio(luma1: T, luma2: T) -> T where - T: Add, - T: Div, - T: FromF64, - T: Component, + T: Component + FromF64 + Add + Div, { if luma1 > luma2 { (luma1 + from_f64(0.05)) / (luma2 + from_f64(0.05)) diff --git a/palette/src/rgb/rgb.rs b/palette/src/rgb/rgb.rs index 49f8f1fbb..9ea8134d7 100644 --- a/palette/src/rgb/rgb.rs +++ b/palette/src/rgb/rgb.rs @@ -1060,7 +1060,7 @@ impl FromStr for Rgb { // Parses a color hex code of format '#ff00bb' or '#abc' into a // Rgb instance. fn from_str(hex: &str) -> Result { - let hex_code = if hex.starts_with('#') { &hex[1..] } else { hex }; + let hex_code = hex.strip_prefix('#').map_or(hex, |stripped| stripped); match hex_code.len() { 3 => { let red = u8::from_str_radix(&hex_code[..1], 16)?; diff --git a/palette_derive/src/alpha/with_alpha.rs b/palette_derive/src/alpha/with_alpha.rs index cf587bff6..a2c305d79 100644 --- a/palette_derive/src/alpha/with_alpha.rs +++ b/palette_derive/src/alpha/with_alpha.rs @@ -20,7 +20,7 @@ pub fn derive(item: TokenStream) -> ::std::result::Result Result> { - let included_colors = COLOR_TYPES - .into_iter() - .filter(|&&color| !skip.contains(color)); + let included_colors = COLOR_TYPES.iter().filter(|&&color| !skip.contains(color)); let linear_path = util::path(&["encoding", "Linear"], meta.internal); let mut parameters = Vec::new(); diff --git a/palette_derive/src/encoding/pixel.rs b/palette_derive/src/encoding/pixel.rs index 2918b9a87..72961c3a9 100644 --- a/palette_derive/src/encoding/pixel.rs +++ b/palette_derive/src/encoding/pixel.rs @@ -111,7 +111,7 @@ pub fn derive(tokens: TokenStream) -> std::result::Result std::result::Result { - let items = match meta::parse_tuple_attribute(attribute.tokens.clone()) { - Ok(items) => items, - Err(error) => { - errors.push(error); - continue; - } - }; + if let Some("repr") = attribute_name.as_deref() { + let items = match meta::parse_tuple_attribute(attribute.tokens.clone()) { + Ok(items) => items, + Err(error) => { + errors.push(error); + continue; + } + }; - let contains_c = items.into_iter().find(|item: &Ident| item == "C").is_some(); + let contains_c = items.iter().any(|item: &Ident| item == "C"); - if contains_c { - return Ok(true); - } + if contains_c { + return Ok(true); } - _ => {} } } diff --git a/palette_derive/src/meta/mod.rs b/palette_derive/src/meta/mod.rs index bb34ca31c..28f52bf74 100644 --- a/palette_derive/src/meta/mod.rs +++ b/palette_derive/src/meta/mod.rs @@ -157,7 +157,7 @@ pub fn parse_tuple_attribute(tts: TokenStream) -> Result> { Ok(tuple) } - parse_generic_tuple.parse2(tts.clone()) + parse_generic_tuple.parse2(tts) } fn parse_meta_list(buffer: &ParseBuffer) -> syn::Result> { diff --git a/palette_derive/src/util.rs b/palette_derive/src/util.rs index 4ef228226..bb5d8accf 100644 --- a/palette_derive/src/util.rs +++ b/palette_derive/src/util.rs @@ -5,7 +5,7 @@ use syn::{parse_quote, Ident, Type}; pub fn path<'a, P: AsRef<[&'a str]>>(path: P, internal: bool) -> TokenStream { let path = path .as_ref() - .into_iter() + .iter() .map(|&ident| Ident::new(ident, Span::call_site())); if internal { @@ -18,7 +18,7 @@ pub fn path<'a, P: AsRef<[&'a str]>>(path: P, internal: bool) -> TokenStream { pub fn path_type(path: &[&str], internal: bool) -> Type { let path = path - .into_iter() + .iter() .map(|&ident| Ident::new(ident, Span::call_site())); if internal {