diff options
author | Heiko Tietze <tietze.heiko@gmail.com> | 2024-08-14 13:49:50 +0200 |
---|---|---|
committer | Heiko Tietze <heiko.tietze@documentfoundation.org> | 2024-08-16 08:55:54 +0200 |
commit | 74d910231c5591f5e497fc52f2a0132c8f41a271 (patch) | |
tree | 3f7324a5c17dc1ad0ddca6d6ae908f50af1607e0 /tools/source | |
parent | 63fbb71f362b16eaa7b7d8617ae3b7e482b41492 (diff) |
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.tietze@documentfoundation.org>
Tested-by: Jenkins
Diffstat (limited to 'tools/source')
-rw-r--r-- | tools/source/generic/color.cxx | 31 |
1 files changed, 31 insertions, 0 deletions
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)); |