Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/contrastpreview.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "contrastpreview.h"

#include <KColorUtils>
#include "hcycolorspace.h"

#include <QLabel>
#include <QLocale>
Expand Down Expand Up @@ -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");
Expand Down
61 changes: 61 additions & 0 deletions src/hcycolorspace.h
Original file line number Diff line number Diff line change
@@ -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 <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <QtGlobal>

#include <algorithm>
#include <array>

#include <math.h>

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<color_comp_t, 3> 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<color_comp_t, 3> 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