Browse Source

Added method Colour::getPerceivedBrightness()

tags/2021-05-28
jules 11 years ago
parent
commit
1a75ceb9aa
2 changed files with 18 additions and 18 deletions
  1. +12
    -18
      modules/juce_graphics/colour/juce_Colour.cpp
  2. +6
    -0
      modules/juce_graphics/colour/juce_Colour.h

+ 12
- 18
modules/juce_graphics/colour/juce_Colour.cpp View File

@@ -29,19 +29,6 @@ namespace ColourHelpers
return n <= 0.0f ? 0 : (n >= 1.0f ? 255 : static_cast<uint8> (n * 255.996f));
}
// This is an adjusted brightness value, based on the way the human
// eye responds to different colour channels..
static float getPerceivedBrightness (Colour c) noexcept
{
const float r = c.getFloatRed();
const float g = c.getFloatGreen();
const float b = c.getFloatBlue();
return std::sqrt (r * r * 0.241f
+ g * g * 0.691f
+ b * b * 0.068f);
}
//==============================================================================
struct HSB
{
@@ -333,6 +320,13 @@ Colour Colour::withHue (float h) const noexcept { ColourHelpers::HSB hs
Colour Colour::withSaturation (float s) const noexcept { ColourHelpers::HSB hsb (*this); hsb.saturation = s; return hsb.toColour (*this); }
Colour Colour::withBrightness (float v) const noexcept { ColourHelpers::HSB hsb (*this); hsb.brightness = v; return hsb.toColour (*this); }
float Colour::getPerceivedBrightness() const noexcept
{
return std::sqrt (0.241f * square (getFloatRed())
+ 0.691f * square (getFloatGreen())
+ 0.068f * square (getFloatBlue()));
}
//==============================================================================
Colour Colour::withRotatedHue (const float amountToRotate) const noexcept
{
@@ -386,9 +380,9 @@ Colour Colour::greyLevel (const float brightness) noexcept
//==============================================================================
Colour Colour::contrasting (const float amount) const noexcept
{
return overlaidWith ((ColourHelpers::getPerceivedBrightness (*this) >= 0.5f
? Colours::black
: Colours::white).withAlpha (amount));
return overlaidWith (getPerceivedBrightness() >= 0.5f
? Colours::black
: Colours::white).withAlpha (amount);
}
Colour Colour::contrasting (Colour target, float minContrast) const noexcept
@@ -409,8 +403,8 @@ Colour Colour::contrasting (Colour target, float minContrast) const noexcept
Colour Colour::contrasting (Colour colour1,
Colour colour2) noexcept
{
const float b1 = ColourHelpers::getPerceivedBrightness (colour1);
const float b2 = ColourHelpers::getPerceivedBrightness (colour2);
const float b1 = colour1.getPerceivedBrightness();
const float b2 = colour2.getPerceivedBrightness();
float best = 0.0f;
float bestDist = 0.0f;


+ 6
- 0
modules/juce_graphics/colour/juce_Colour.h View File

@@ -243,6 +243,12 @@ public:
*/
float getBrightness() const noexcept;
/** Returns a skewed brightness value, adjusted to better reflect the way the human
eye responds to different colour channels. This makes it better than getBrightness()
for comparing differences in brightness.
*/
float getPerceivedBrightness() const noexcept;
/** Returns the colour's hue, saturation and brightness components all at once.
The values returned are in the range 0.0 to 1.0
*/


Loading…
Cancel
Save