mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#3919 Implement a better threshold check for contrast colors
This commit is contained in:
parent
6754f837d4
commit
68fb72311e
@ -1,5 +1,6 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
|
// Copyright (C) 2018- Equinor ASA
|
||||||
// Copyright (C) 2017 Statoil ASA
|
// Copyright (C) 2017 Statoil ASA
|
||||||
//
|
//
|
||||||
// ResInsight is free software: you can redistribute it and/or modify
|
// ResInsight is free software: you can redistribute it and/or modify
|
||||||
@ -21,11 +22,13 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
/// Uses W3.org relative luminance calculation taking into account the different luminance of the different colors
|
||||||
|
/// https://www.w3.org/TR/WCAG20-TECHS/G18.html
|
||||||
|
/// Luminance is between [0, 1] so anything above 0.5 is considered in the bright half of the spectrum.
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
bool RiaColorTools::isBrightnessAboveThreshold(cvf::Color3f backgroundColor)
|
bool RiaColorTools::isBrightnessAboveThreshold(cvf::Color3f backgroundColor)
|
||||||
{
|
{
|
||||||
if (backgroundColor.r() + backgroundColor.g() + backgroundColor.b() > 1.5f)
|
if (relativeLuminance(backgroundColor) > 0.5)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -55,9 +58,8 @@ cvf::Color3f RiaColorTools::computeOffsetColor(cvf::Color3f color, float offsetF
|
|||||||
gridB = color.b() + (1.0f - color.b()) * offsetFactor;
|
gridB = color.b() + (1.0f - color.b()) * offsetFactor;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cvf::Color3f(cvf::Math::clamp(gridR, 0.0f, 1.0f),
|
return cvf::Color3f(
|
||||||
cvf::Math::clamp(gridG, 0.0f, 1.0f),
|
cvf::Math::clamp(gridR, 0.0f, 1.0f), cvf::Math::clamp(gridG, 0.0f, 1.0f), cvf::Math::clamp(gridB, 0.0f, 1.0f));
|
||||||
cvf::Math::clamp(gridB, 0.0f, 1.0f));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
@ -106,3 +108,38 @@ QColor RiaColorTools::toQColor(cvf::Color4f color)
|
|||||||
{
|
{
|
||||||
return toQColor(color.toColor3f(), color.a());
|
return toQColor(color.toColor3f(), color.a());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
float RiaColorTools::contrastRatio(cvf::Color3f color1, cvf::Color3f color2)
|
||||||
|
{
|
||||||
|
float L1 = relativeLuminance(color1);
|
||||||
|
float L2 = relativeLuminance(color2);
|
||||||
|
|
||||||
|
float Lmin = std::min(L1, L2);
|
||||||
|
float Lmax = std::max(L1, L2);
|
||||||
|
|
||||||
|
return (Lmax + 0.05) / (Lmin + 0.05);
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
float RiaColorTools::relativeLuminance(cvf::Color3f backgroundColor)
|
||||||
|
{
|
||||||
|
float R = calculateNonLinearColorValue(backgroundColor.r());
|
||||||
|
float G = calculateNonLinearColorValue(backgroundColor.g());
|
||||||
|
float B = calculateNonLinearColorValue(backgroundColor.b());
|
||||||
|
|
||||||
|
double luminance = 0.2126 * R + 0.7152 * G + 0.0722 * B;
|
||||||
|
return luminance;
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
float RiaColorTools::calculateNonLinearColorValue(float colorFraction)
|
||||||
|
{
|
||||||
|
return colorFraction <= 0.03928 ? colorFraction / 12.92 : std::pow((colorFraction + 0.055) / 1.055, 2.4);
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
//
|
//
|
||||||
|
// Copyright (C) 2018- Equinor ASA
|
||||||
// Copyright (C) 2017 Statoil ASA
|
// Copyright (C) 2017 Statoil ASA
|
||||||
//
|
//
|
||||||
// ResInsight is free software: you can redistribute it and/or modify
|
// ResInsight is free software: you can redistribute it and/or modify
|
||||||
@ -38,4 +39,8 @@ public:
|
|||||||
static cvf::Color3f constrastColor(cvf::Color3f backgroundColor);
|
static cvf::Color3f constrastColor(cvf::Color3f backgroundColor);
|
||||||
static QColor toQColor(cvf::Color3f color, float alpha = 1.0f);
|
static QColor toQColor(cvf::Color3f color, float alpha = 1.0f);
|
||||||
static QColor toQColor(cvf::Color4f color);
|
static QColor toQColor(cvf::Color4f color);
|
||||||
|
static float contrastRatio(cvf::Color3f color1, cvf::Color3f color2);
|
||||||
|
private:
|
||||||
|
static float relativeLuminance(cvf::Color3f backgroundColor);
|
||||||
|
static float calculateNonLinearColorValue(float colorFraction);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user