diff --git a/src/ImageSharp/Common/Constants.cs b/src/ImageSharp/Common/Constants.cs index 41f2bce24..b7cfddcb6 100644 --- a/src/ImageSharp/Common/Constants.cs +++ b/src/ImageSharp/Common/Constants.cs @@ -9,8 +9,13 @@ namespace SixLabors.ImageSharp internal static class Constants { /// - /// The epsilon for comparing floating point numbers. + /// The epsilon value for comparing floating point numbers. /// - public static readonly float Epsilon = 0.001f; + public static readonly float Epsilon = 0.001F; + + /// + /// The epsilon squared value for comparing floating point numbers. + /// + public static readonly float EpsilonSquared = Epsilon * Epsilon; } } diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs index b2c7436ae..5153ab46b 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs @@ -144,20 +144,24 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers int colorIndex = 0; float leastDistance = float.MaxValue; var vector = pixel.ToVector4(); + float epsilon = Constants.EpsilonSquared; for (int index = 0; index < colorPalette.Length; index++) { ref TPixel candidate = ref colorPalette[index]; float distance = Vector4.DistanceSquared(vector, candidate.ToVector4()); - if (distance < leastDistance) + // Greater... Move on. + if (!(distance < leastDistance)) { - colorIndex = index; - leastDistance = distance; + continue; } - // If it's an exact match, exit the loop - if (distance == 0) + colorIndex = index; + leastDistance = distance; + + // And if it's an exact match, exit the loop + if (distance < epsilon) { break; }