1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-11 23:54:18 +00:00

Added method Colour::getPerceivedBrightness()

This commit is contained in:
jules 2014-04-07 22:07:45 +01:00
parent 05afb39185
commit 1a75ceb9aa
2 changed files with 18 additions and 18 deletions

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;

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
*/