include/tools/color.hxx | 19 +++++++------------ tools/source/generic/color.cxx | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 12 deletions(-)
New commits: commit 74d910231c5591f5e497fc52f2a0132c8f41a271 Author: Heiko Tietze <tietze.he...@gmail.com> AuthorDate: Wed Aug 14 13:49:50 2024 +0200 Commit: Heiko Tietze <heiko.tie...@documentfoundation.org> CommitDate: Fri Aug 16 08:55:54 2024 +0200 Resolves tdf#161766 - WCAG 2.1 compliant luminance for isDark() Alternative solution to I63b85694584cdc6213e7b904ed6402b6c2f2b422 Change-Id: Ifca47de30616295a9965b9313886456313921401 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/171862 Reviewed-by: Heiko Tietze <heiko.tie...@documentfoundation.org> Tested-by: Jenkins diff --git a/include/tools/color.hxx b/include/tools/color.hxx index 653111c50bb4..5d2052e297a4 100644 --- a/include/tools/color.hxx +++ b/include/tools/color.hxx @@ -282,6 +282,11 @@ public: return sal_uInt8((GetBlue() * 29UL + GetGreen() * 151UL + GetRed() * 76UL) >> 8); } + /** Gets the color luminance following WCAG 2.1. + * @return luminance + */ + sal_uInt8 GetWCAGLuminance() const; + /** Increases the color luminance by cLumInc. * @param cLumInc */ @@ -300,22 +305,12 @@ public: /** Comparison with luminance thresholds. * @return is dark */ - bool IsDark() const - { - // tdf#156182, and band aid for follow-up issues - if (mValue == 0x729fcf) // COL_DEFAULT_SHAPE_FILLING - return GetLuminance() <= 62; - else - return GetLuminance() <= 156; - } + bool IsDark() const; /** Comparison with luminance thresholds. * @return is dark */ - bool IsBright() const - { - return GetLuminance() >= 245; - } + bool IsBright() const; /* Color filters */ diff --git a/tools/source/generic/color.cxx b/tools/source/generic/color.cxx index 208a6caee883..0834b0946722 100644 --- a/tools/source/generic/color.cxx +++ b/tools/source/generic/color.cxx @@ -31,6 +31,37 @@ #include <basegfx/color/bcolortools.hxx> #include <basegfx/numeric/ftools.hxx> +static inline double NormalizeRGB(double nValue) +{ + if (nValue < 0.04045) + return nValue/12.92; + else + return pow((nValue+0.055)/1.055, 2.4); +} + +sal_uInt8 Color::GetWCAGLuminance() const +{ + // https://www.w3.org/TR/WCAG21/#dfn-relative-luminance + const double nRed = NormalizeRGB(R/255.0); + const double nGreen = NormalizeRGB(G/255.0); + const double nBlue = NormalizeRGB(B/255.0); + return (nRed * 0.2126 + nGreen * 0.7152 + nBlue * 0.0722) * 255UL; +} + +bool Color::IsDark() const +{ + if (mValue == 0x729fcf) // COL_DEFAULT_SHAPE_FILLING + return GetLuminance() <= 62; + else + return GetWCAGLuminance() <= 87; +} + +bool Color::IsBright() const +{ + return !IsDark(); +// return GetLuminance() >= 245; +} + void Color::IncreaseLuminance(sal_uInt8 cLumInc) { R = sal_uInt8(std::clamp(R + cLumInc, 0, 255));