Browse Source

Colour: Avoid divisions by zero

v6.1.6
reuk 4 years ago
parent
commit
f0c02529ef
No known key found for this signature in database GPG Key ID: 9ADCD339CFC98A11
1 changed files with 16 additions and 8 deletions
  1. +16
    -8
      modules/juce_graphics/colour/juce_Colour.cpp

+ 16
- 8
modules/juce_graphics/colour/juce_Colour.cpp View File

@@ -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()),


Loading…
Cancel
Save