From fb101db0accdb2132be3e1c7b9e806ee38d77bf8 Mon Sep 17 00:00:00 2001 From: gered Date: Mon, 18 Jul 2022 21:21:40 -0400 Subject: [PATCH] stop searching for color matches if this color is an exact match probably very rarely ever an optimization in real-world use, but it still seems like the logical thing to do --- libretrogd/src/graphics/palette.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libretrogd/src/graphics/palette.rs b/libretrogd/src/graphics/palette.rs index 79c0f29..2b3d2fd 100644 --- a/libretrogd/src/graphics/palette.rs +++ b/libretrogd/src/graphics/palette.rs @@ -483,16 +483,20 @@ impl Palette { for (index, color) in self.colors.iter().enumerate() { let (this_r, this_g, this_b) = from_rgb32(*color); - // this comparison method is using the sRGB Euclidean formula described here: - // https://en.wikipedia.org/wiki/Color_difference + if r == this_r && g == this_g && b == this_b { + return index as u8; + } else { + // this comparison method is using the sRGB Euclidean formula described here: + // https://en.wikipedia.org/wiki/Color_difference - let distance = abs_diff(this_r, r) as u32 - + abs_diff(this_g, g) as u32 - + abs_diff(this_b, b) as u32; + let distance = abs_diff(this_r, r) as u32 + + abs_diff(this_g, g) as u32 + + abs_diff(this_b, b) as u32; - if distance < closest_distance { - closest = index as u8; - closest_distance = distance; + if distance < closest_distance { + closest = index as u8; + closest_distance = distance; + } } }