From 5ab44373bc998f96407f9f2dc6488ec5a7bc21e3 Mon Sep 17 00:00:00 2001 From: Jamy Golden Date: Sun, 15 Sep 2024 13:22:21 +0200 Subject: [PATCH] Add minor optimisations and general clippy cleanup --- CHANGELOG.md | 2 ++ src/cli/cli.rs | 14 +++++++------- src/cli/commands/distinct.rs | 2 +- src/distinct.rs | 18 ++++++++---------- src/lib.rs | 6 ++++-- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ec26ff8..775a4ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ## Other +- Minor optimizations and cleanup + ## Packaging diff --git a/src/cli/cli.rs b/src/cli/cli.rs index df68c6e9..4d8d609a 100644 --- a/src/cli/cli.rs +++ b/src/cli/cli.rs @@ -38,7 +38,7 @@ pub fn build_cli() -> Command<'static> { .short('s') .value_name("name") .help("The colorspace in which to interpolate") - .possible_values(&["Lab", "LCh", "RGB", "HSL", "OkLab"]) + .possible_values(["Lab", "LCh", "RGB", "HSL", "OkLab"]) .ignore_case(true) .default_value("Lab"); @@ -95,7 +95,7 @@ pub fn build_cli() -> Command<'static> { \n\ Default strategy: 'vivid'\n ", ) - .possible_values(&["vivid", "rgb", "gray", "lch_hue"]) + .possible_values(["vivid", "rgb", "gray", "lch_hue"]) .hide_default_value(true) .hide_possible_values(true) .default_value("vivid"), @@ -131,7 +131,7 @@ pub fn build_cli() -> Command<'static> { .help("Distance metric to compute mutual color distances. The CIEDE2000 is \ more accurate, but also much slower.") .takes_value(true) - .possible_values(&["CIEDE2000", "CIE76"]) + .possible_values(["CIEDE2000", "CIE76"]) .value_name("name") .default_value("CIE76") ) @@ -211,7 +211,7 @@ pub fn build_cli() -> Command<'static> { .help("Output format type. Note that the 'ansi-*-escapecode' formats print \ ansi escape sequences to the terminal that will not be visible \ unless something else is printed in addition.") - .possible_values(&["rgb", "rgb-float", "hex", + .possible_values(["rgb", "rgb-float", "hex", "hsl", "hsl-hue", "hsl-saturation", "hsl-lightness", "hsv", "hsv-hue", "hsv-saturation", "hsv-value", "lch", "lch-lightness", "lch-chroma", "lch-hue", @@ -340,7 +340,7 @@ pub fn build_cli() -> Command<'static> { Arg::new("type") .help("The type of colorblindness that should be simulated (protanopia, \ deuteranopia, tritanopia)") - .possible_values(&["prot", "deuter", "trit"]) + .possible_values(["prot", "deuter", "trit"]) .ignore_case(true) .required(true), ) @@ -355,7 +355,7 @@ pub fn build_cli() -> Command<'static> { .arg( Arg::new("property") .help("The property that should be changed") - .possible_values(&["lightness", "hue", "chroma", + .possible_values(["lightness", "hue", "chroma", "lab-a", "lab-b", "oklab-l", "oklab-a", "oklab-b", "red", "green", "blue", @@ -491,7 +491,7 @@ pub fn build_cli() -> Command<'static> { .short('m') .value_name("mode") .help("Specify the terminal color mode: 24bit, 8bit, off, *auto*") - .possible_values(&["24bit", "8bit", "off", "auto"]) + .possible_values(["24bit", "8bit", "off", "auto"]) .default_value(if output_vt100::try_init().is_ok() {"auto"} else {"off"}) .hide_possible_values(true) .hide_default_value(true) diff --git a/src/cli/commands/distinct.rs b/src/cli/commands/distinct.rs index 6e3a7608..1a7f017e 100644 --- a/src/cli/commands/distinct.rs +++ b/src/cli/commands/distinct.rs @@ -71,7 +71,7 @@ fn print_distance_matrix( DistanceMetric::CIEDE2000 => c1.distance_delta_e_ciede2000(c2), }; - let mut min = std::f64::MAX; + let mut min = f64::MAX; let mut max = 0.0; for i in 0..count { for j in 0..count { diff --git a/src/distinct.rs b/src/distinct.rs index 3e951ba3..bdcb7638 100644 --- a/src/distinct.rs +++ b/src/distinct.rs @@ -1,5 +1,3 @@ -use core::f64 as scalar; - use rand::prelude::*; use crate::delta_e; @@ -217,18 +215,18 @@ impl SimulatedAnnealing { /// the sequence). /// /// See: -pub fn rearrange_sequence(colors: &mut Vec, metric: DistanceMetric) { +pub fn rearrange_sequence(colors: &mut [Color], metric: DistanceMetric) { let distance = |c1: &Color, c2: &Color| match metric { DistanceMetric::CIE76 => c1.distance_delta_e_cie76(c2), DistanceMetric::CIEDE2000 => c1.distance_delta_e_ciede2000(c2), }; // vector where the i-th element contains the minimum distance to the colors from 0 to i-1. - let mut min_distances = vec![i32::max_value(); colors.len()]; + let mut min_distances = vec![i32::MAX; colors.len()]; for i in 1..colors.len() { let mut max_i = colors.len(); - let mut max_d = i32::min_value(); + let mut max_d = i32::MIN; for j in i..colors.len() { min_distances[j] = @@ -290,10 +288,10 @@ pub fn distinct_colors( impl DistanceResult { fn new(lab_values: &[Lab], distance_metric: DistanceMetric, num_fixed_colors: usize) -> Self { let mut result = DistanceResult { - closest_distances: vec![(scalar::MAX, std::usize::MAX); lab_values.len()], - closest_pair: (std::usize::MAX, std::usize::MAX), + closest_distances: vec![(Scalar::MAX, usize::MAX); lab_values.len()], + closest_pair: (usize::MAX, usize::MAX), mean_closest_distance: 0.0, - min_closest_distance: scalar::MAX, + min_closest_distance: Scalar::MAX, distance_metric, num_fixed_colors, }; @@ -314,7 +312,7 @@ impl DistanceResult { } fn update_distances(&mut self, lab_values: &[Lab], color: usize, changed: bool) { - self.closest_distances[color] = (scalar::MAX, std::usize::MAX); + self.closest_distances[color] = (Scalar::MAX, usize::MAX); // we need to recalculate distances for nodes where the previous min dist was with // changed_color but it's not anymore (potentially). @@ -349,7 +347,7 @@ impl DistanceResult { fn update_totals(&mut self) { self.mean_closest_distance = 0.0; - self.min_closest_distance = scalar::MAX; + self.min_closest_distance = Scalar::MAX; let mut closest_pair_set = false; diff --git a/src/lib.rs b/src/lib.rs index 95da193f..127015d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,6 @@ impl Color { }) } - /// pub fn from_hsl(hue: Scalar, saturation: Scalar, lightness: Scalar) -> Color { Self::from(&HSLA { h: hue, @@ -1978,7 +1977,10 @@ mod tests { .add_stop(Color::gray(), Fraction::from(0.0)) .add_stop(Color::blue(), Fraction::from(1.0)); - assert_eq!(color_scale.color_stops.get(0).unwrap().color, Color::gray()); + assert_eq!( + color_scale.color_stops.first().unwrap().color, + Color::gray() + ); assert_eq!(color_scale.color_stops.get(1).unwrap().color, Color::red()); assert_eq!(color_scale.color_stops.get(2).unwrap().color, Color::blue()); }