From 9ac4e29a20902df8cb3cf23d3dfe4cbb27bc7735 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Thu, 14 Nov 2019 14:12:59 +0100 Subject: [PATCH] Move helper functions to more public location Change-Id: I12a2000bfc82c194d12fb5c25302a06f782aa605 Reviewed-by: David Schulz --- src/libs/utils/stylehelper.cpp | 35 ++++++++++++++++++++ src/libs/utils/stylehelper.h | 2 ++ src/plugins/texteditor/highlighter.cpp | 46 +++----------------------- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/src/libs/utils/stylehelper.cpp b/src/libs/utils/stylehelper.cpp index 388da7eeba9..76123da3b50 100644 --- a/src/libs/utils/stylehelper.cpp +++ b/src/libs/utils/stylehelper.cpp @@ -573,4 +573,39 @@ QList StyleHelper::availableImageResolutions(const QString &fileName) return result; } +static double luminance(const QColor &color) +{ + // calculate the luminance based on + // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef + auto val = [](const double &colorVal) { + return colorVal < 0.03928 ? colorVal / 12.92 : std::pow((colorVal + 0.055) / 1.055, 2.4); + }; + + static QHash cache; + QHash::iterator it = cache.find(color.rgb()); + if (it == cache.end()) { + it = cache.insert(color.rgb(), 0.2126 * val(color.redF()) + + 0.7152 * val(color.greenF()) + + 0.0722 * val(color.blueF())); + } + return it.value(); +} + +static double contrastRatio(const QColor &color1, const QColor &color2) +{ + // calculate the contrast ratio based on + // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef + auto contrast = (luminance(color1) + 0.05) / (luminance(color2) + 0.05); + if (contrast < 1) + return 1 / contrast; + return contrast; +} + +bool StyleHelper::isReadableOn(const QColor &background, const QColor &foreground) +{ + // following the W3C Recommendation on contrast for large Text + // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef + return contrastRatio(background, foreground) > 3; +} + } // namespace Utils diff --git a/src/libs/utils/stylehelper.h b/src/libs/utils/stylehelper.h index 6c9a1c5f5d7..d5a40f6eb4d 100644 --- a/src/libs/utils/stylehelper.h +++ b/src/libs/utils/stylehelper.h @@ -97,6 +97,8 @@ public: static QString imageFileWithResolution(const QString &fileName, int dpr); static QList availableImageResolutions(const QString &fileName); + static bool isReadableOn(const QColor &background, const QColor &foreground); + private: static QColor m_baseColor; static QColor m_requestedBaseColor; diff --git a/src/plugins/texteditor/highlighter.cpp b/src/plugins/texteditor/highlighter.cpp index 19f0656a152..dc7d492592c 100644 --- a/src/plugins/texteditor/highlighter.cpp +++ b/src/plugins/texteditor/highlighter.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -46,8 +47,6 @@ #include #include -#include - using namespace TextEditor; static const char kDefinitionForMimeType[] = "definitionForMimeType"; @@ -338,41 +337,6 @@ void Highlighter::highlightBlock(const QString &text) formatSpaces(text); } -static double luminance(const QColor &color) -{ - // calculate the luminance based on - // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef - auto val = [](const double &colorVal) { - return colorVal < 0.03928 ? colorVal / 12.92 : std::pow((colorVal + 0.055) / 1.055, 2.4); - }; - - static QHash cache; - QHash::iterator it = cache.find(color.rgb()); - if (it == cache.end()) { - it = cache.insert(color.rgb(), 0.2126 * val(color.redF()) - + 0.7152 * val(color.greenF()) - + 0.0722 * val(color.blueF())); - } - return it.value(); -} - -static double contrastRatio(const QColor &color1, const QColor &color2) -{ - // calculate the contrast ratio based on - // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef - auto contrast = (luminance(color1) + 0.05) / (luminance(color2) + 0.05); - if (contrast < 1) - return 1 / contrast; - return contrast; -} - -static bool isReadableOn(const QColor &background, const QColor &foreground) -{ - // following the W3C Recommendation on contrast for large Text - // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef - return contrastRatio(background, foreground) > 3; -} - void Highlighter::applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format) { const KSyntaxHighlighting::Theme defaultTheme; @@ -382,18 +346,18 @@ void Highlighter::applyFormat(int offset, int length, const KSyntaxHighlighting: const QColor textColor = format.textColor(defaultTheme); if (format.hasBackgroundColor(defaultTheme)) { const QColor backgroundColor = format.hasBackgroundColor(defaultTheme); - if (isReadableOn(backgroundColor, textColor)) { + if (Utils::StyleHelper::isReadableOn(backgroundColor, textColor)) { qformat.setForeground(textColor); qformat.setBackground(backgroundColor); - } else if (isReadableOn(qformat.background().color(), textColor)) { + } else if (Utils::StyleHelper::isReadableOn(qformat.background().color(), textColor)) { qformat.setForeground(textColor); } - } else if (isReadableOn(qformat.background().color(), textColor)) { + } else if (Utils::StyleHelper::isReadableOn(qformat.background().color(), textColor)) { qformat.setForeground(textColor); } } else if (format.hasBackgroundColor(defaultTheme)) { const QColor backgroundColor = format.hasBackgroundColor(defaultTheme); - if (isReadableOn(backgroundColor, qformat.foreground().color())) + if (Utils::StyleHelper::isReadableOn(backgroundColor, qformat.foreground().color())) qformat.setBackground(backgroundColor); }