From f0c02529ef5fcc4a2c5aedc27f6366afe9fa2215 Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 16 Jun 2021 11:45:50 +0100 Subject: [PATCH] Colour: Avoid divisions by zero --- modules/juce_graphics/colour/juce_Colour.cpp | 24 +++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/modules/juce_graphics/colour/juce_Colour.cpp b/modules/juce_graphics/colour/juce_Colour.cpp index 25c748b836..bf520e2ee8 100644 --- a/modules/juce_graphics/colour/juce_Colour.cpp +++ b/modules/juce_graphics/colour/juce_Colour.cpp @@ -44,7 +44,7 @@ namespace ColourHelpers float hue = 0.0f; - if (hi > 0) + if (hi > 0 && ! approximatelyEqual (hi, lo)) { auto invDiff = 1.0f / (float) (hi - lo); @@ -77,15 +77,21 @@ namespace ColourHelpers auto hi = jmax (r, g, b); auto lo = jmin (r, g, b); - if (hi > 0) - { - lightness = ((float) (hi + lo) / 2.0f) / 255.0f; + if (hi < 0) + return; - if (lightness > 0.0f) - hue = getHue (col); + lightness = ((float) (hi + lo) / 2.0f) / 255.0f; - saturation = ((float) (hi - lo) / 255.0f) / (1.0f - std::abs ((2.0f * lightness) - 1.0f)); - } + if (lightness < 0.0f) + return; + + hue = getHue (col); + + if (1.0f < lightness) + return; + + auto denominator = 1.0f - std::abs ((2.0f * lightness) - 1.0f); + saturation = ((float) (hi - lo) / 255.0f) / denominator; } Colour toColour (Colour original) const noexcept @@ -454,6 +460,7 @@ Colour Colour::withMultipliedLightness (float amount) const noexcept //============================================================================== Colour Colour::brighter (float amount) const noexcept { + jassert (0.0f <= amount && amount <= 1.0f); amount = 1.0f / (1.0f + amount); return Colour ((uint8) (255 - (amount * (255 - getRed()))), @@ -464,6 +471,7 @@ Colour Colour::brighter (float amount) const noexcept Colour Colour::darker (float amount) const noexcept { + jassert (0.0f <= amount && amount <= 1.0f); amount = 1.0f / (1.0f + amount); return Colour ((uint8) (amount * getRed()),