From 2d1fc54763f0ff71c145a994cddaf5a0b72f2e2e Mon Sep 17 00:00:00 2001 From: redtide Date: Fri, 11 Aug 2023 19:50:24 +0200 Subject: [PATCH] Removed KGuiAddons dependency --- CMakeLists.txt | 3 +- src/contrastpreview.cpp | 12 ++++++-- src/hcycolorspace.h | 61 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 src/hcycolorspace.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2457da3..d150477 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,6 @@ find_package(Qt${QT_VERSION_MAJOR} CONFIG REQUIRED Core LinguistTools Widgets) # # Other dependencies # -find_package(KF${QT_VERSION_MAJOR}GuiAddons CONFIG REQUIRED) find_package(KF${QT_VERSION_MAJOR}WidgetsAddons CONFIG REQUIRED) find_package(ECM REQUIRED NO_MODULE) list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH}) @@ -39,6 +38,7 @@ set(PROJECT_SOURCES src/componenteditor.cpp src/contrastpreview.h src/contrastpreview.cpp + src/hcycolorspace.h src/hsvcolorspace.h src/hsvcolorspace.cpp src/imagegradientselector.h @@ -77,7 +77,6 @@ target_include_directories(${PROJECT_NAME} PRIVATE target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Core Qt::Widgets - KF${QT_VERSION_MAJOR}::GuiAddons KF${QT_VERSION_MAJOR}::WidgetsAddons ) if (UNIX AND NOT APPLE) diff --git a/src/contrastpreview.cpp b/src/contrastpreview.cpp index d017e78..be57917 100644 --- a/src/contrastpreview.cpp +++ b/src/contrastpreview.cpp @@ -1,6 +1,5 @@ #include "contrastpreview.h" - -#include +#include "hcycolorspace.h" #include #include @@ -49,7 +48,14 @@ void ContrastPreview::updatePreview() void ContrastPreview::updateRatioLabel() { - qreal ratio = KColorUtils::contrastRatio(mBackgroundColor, mForegroundColor); + hcy::color_comp_t lumaBg = hcy::luminance(mBackgroundColor.redF(), + mBackgroundColor.greenF(), + mBackgroundColor.blueF()); + hcy::color_comp_t lumaFg = hcy::luminance(mForegroundColor.redF(), + mForegroundColor.greenF(), + mForegroundColor.blueF()); + qreal ratio = hcy::contrast_ratio(lumaBg, lumaFg); + QString level; if (ratio < ACCEPTABLE_CONTRAST_RATIO) { level = tr("Bad"); diff --git a/src/hcycolorspace.h b/src/hcycolorspace.h new file mode 100644 index 0000000..e9b050a --- /dev/null +++ b/src/hcycolorspace.h @@ -0,0 +1,61 @@ + +/* + SPDX-License-Identifier: CC0-1.0 + + You should have received a copy of the CC0 legalcode along with this work. + If not, see . +*/ +#include + +#include +#include + +#include + +namespace hcy { + +#if QT_VERSION < 0x060000 + typedef qreal color_comp_t; // double + static constexpr qreal color_comp_min { 0.0 }; + static constexpr qreal color_comp_max { 1.0 }; +#else + typedef float color_comp_t; + static constexpr float color_comp_min { 0.0f }; + static constexpr float color_comp_max { 1.0f }; +#endif + +#if 0 +// https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf +// https://www.w3.org/TR/AERT/#color-contrast +static constexpr std::array ry = { 0.299, 0.587, 0.114 }; +#else +// https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.709-6-201506-I!!PDF-E.pdf +// https://www.w3.org/WAI/GL/wiki/Relative_luminance#Definition_as_Stated_in_WCAG_2.x +// https://en.wikipedia.org/wiki/Luma_(video) +// https://en.wikipedia.org/wiki/Relative_luminance +static constexpr std::array ry = { 0.2126, 0.7152, 0.0722 }; +#endif + +// https://stackoverflow.com/questions/596216/formula-to-determine-perceived-brightness-of-rgb-color +static constexpr color_comp_t relative_luminance(color_comp_t r, color_comp_t g, color_comp_t b) +{ + return ry[0] * r + ry[1] * g + ry[2] * b; +} + +// https://www.w3.org/TR/WCAG/#dfn-contrast-ratio +static constexpr color_comp_t contrast_ratio(color_comp_t a, color_comp_t b) +{ + return (a > b) ? (a + 0.05) / (b + 0.05) : (b + 0.05) / (a + 0.05); +} + +static constexpr color_comp_t gamma_correction(color_comp_t color_component) +{ + color_comp_t color_comp = std::clamp(color_component, color_comp_min, color_comp_max); + return (color_comp <= 0.04045) ? (color_comp / 12.92) : std::pow((color_comp + 0.055) / 1.055, 2.4); +} + +static const color_comp_t luminance(color_comp_t r, color_comp_t g, color_comp_t b) +{ + return relative_luminance(gamma_correction(r), gamma_correction(g), gamma_correction(b)); +} +} // namespace hcy