Move helper functions to more public location

Change-Id: I12a2000bfc82c194d12fb5c25302a06f782aa605
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2019-11-14 14:12:59 +01:00
parent 2c029d8700
commit 9ac4e29a20
3 changed files with 42 additions and 41 deletions

View File

@@ -573,4 +573,39 @@ QList<int> 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<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