Skip to content

Commit

Permalink
Add minor optimisations and general clippy cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
JamyGolden committed Sep 15, 2024
1 parent ff86465 commit 5ab4437
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## Other

- Minor optimizations and cleanup

## Packaging


Expand Down
14 changes: 7 additions & 7 deletions src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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")
)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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),
)
Expand All @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 8 additions & 10 deletions src/distinct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::f64 as scalar;

use rand::prelude::*;

use crate::delta_e;
Expand Down Expand Up @@ -217,18 +215,18 @@ impl<R: Rng> SimulatedAnnealing<R> {
/// the sequence).
///
/// See: <https://en.wikipedia.org/wiki/Farthest-first_traversal>
pub fn rearrange_sequence(colors: &mut Vec<Color>, 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] =
Expand Down Expand Up @@ -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,
};
Expand All @@ -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).
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl Color {
})
}

///
pub fn from_hsl(hue: Scalar, saturation: Scalar, lightness: Scalar) -> Color {
Self::from(&HSLA {
h: hue,
Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 5ab4437

Please sign in to comment.