forked from qt-creator/qt-creator
Move helper functions to more public location
Change-Id: I12a2000bfc82c194d12fb5c25302a06f782aa605 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -573,4 +573,39 @@ QList<int> StyleHelper::availableImageResolutions(const QString &fileName)
|
|||||||
return result;
|
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<QRgb, double> cache;
|
||||||
|
QHash<QRgb, double>::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
|
} // namespace Utils
|
||||||
|
@@ -97,6 +97,8 @@ public:
|
|||||||
static QString imageFileWithResolution(const QString &fileName, int dpr);
|
static QString imageFileWithResolution(const QString &fileName, int dpr);
|
||||||
static QList<int> availableImageResolutions(const QString &fileName);
|
static QList<int> availableImageResolutions(const QString &fileName);
|
||||||
|
|
||||||
|
static bool isReadableOn(const QColor &background, const QColor &foreground);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static QColor m_baseColor;
|
static QColor m_baseColor;
|
||||||
static QColor m_requestedBaseColor;
|
static QColor m_requestedBaseColor;
|
||||||
|
@@ -35,6 +35,7 @@
|
|||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
#include <coreplugin/messagemanager.h>
|
#include <coreplugin/messagemanager.h>
|
||||||
#include <utils/mimetypes/mimedatabase.h>
|
#include <utils/mimetypes/mimedatabase.h>
|
||||||
|
#include <utils/stylehelper.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <DefinitionDownloader>
|
#include <DefinitionDownloader>
|
||||||
@@ -46,8 +47,6 @@
|
|||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QMetaEnum>
|
#include <QMetaEnum>
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
|
|
||||||
static const char kDefinitionForMimeType[] = "definitionForMimeType";
|
static const char kDefinitionForMimeType[] = "definitionForMimeType";
|
||||||
@@ -338,41 +337,6 @@ void Highlighter::highlightBlock(const QString &text)
|
|||||||
formatSpaces(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<QRgb, double> cache;
|
|
||||||
QHash<QRgb, double>::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)
|
void Highlighter::applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format)
|
||||||
{
|
{
|
||||||
const KSyntaxHighlighting::Theme defaultTheme;
|
const KSyntaxHighlighting::Theme defaultTheme;
|
||||||
@@ -382,18 +346,18 @@ void Highlighter::applyFormat(int offset, int length, const KSyntaxHighlighting:
|
|||||||
const QColor textColor = format.textColor(defaultTheme);
|
const QColor textColor = format.textColor(defaultTheme);
|
||||||
if (format.hasBackgroundColor(defaultTheme)) {
|
if (format.hasBackgroundColor(defaultTheme)) {
|
||||||
const QColor backgroundColor = format.hasBackgroundColor(defaultTheme);
|
const QColor backgroundColor = format.hasBackgroundColor(defaultTheme);
|
||||||
if (isReadableOn(backgroundColor, textColor)) {
|
if (Utils::StyleHelper::isReadableOn(backgroundColor, textColor)) {
|
||||||
qformat.setForeground(textColor);
|
qformat.setForeground(textColor);
|
||||||
qformat.setBackground(backgroundColor);
|
qformat.setBackground(backgroundColor);
|
||||||
} else if (isReadableOn(qformat.background().color(), textColor)) {
|
} else if (Utils::StyleHelper::isReadableOn(qformat.background().color(), textColor)) {
|
||||||
qformat.setForeground(textColor);
|
qformat.setForeground(textColor);
|
||||||
}
|
}
|
||||||
} else if (isReadableOn(qformat.background().color(), textColor)) {
|
} else if (Utils::StyleHelper::isReadableOn(qformat.background().color(), textColor)) {
|
||||||
qformat.setForeground(textColor);
|
qformat.setForeground(textColor);
|
||||||
}
|
}
|
||||||
} else if (format.hasBackgroundColor(defaultTheme)) {
|
} else if (format.hasBackgroundColor(defaultTheme)) {
|
||||||
const QColor backgroundColor = 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);
|
qformat.setBackground(backgroundColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user