Utils: Improve readability on output windows

Enhancing the StyleHelper by some functionality used
on the results pane of the AutoTest plugin and make use of it
inside the output formatter.
This highly improves readability depending on the current theme
when having output formatters and allows to easily re-use the
output formatter there instead of having several stuff
re-implemented.
Mainly relevant for output formatted with ANSI escape codes.

Change-Id: Ic2f5eff877656eb52e3bd2fda0ec9a015e54ea82
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Christian Stenger
2023-01-20 11:20:53 +01:00
parent 2eb9338be0
commit 8655603c2e
3 changed files with 40 additions and 1 deletions

View File

@@ -9,6 +9,7 @@
#include "link.h"
#include "qtcassert.h"
#include "stringutils.h"
#include "stylehelper.h"
#include "theme/theme.h"
#include <QDir>
@@ -273,6 +274,14 @@ void OutputFormatter::overridePostPrintAction(const PostPrintAction &postPrintAc
d->postPrintAction = postPrintAction;
}
static void checkAndFineTuneColors(QTextCharFormat *format)
{
QTC_ASSERT(format, return);
const QColor fgColor = StyleHelper::ensureReadableOn(format->background().color(),
format->foreground().color());
format->setForeground(fgColor);
}
void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
{
QTextCharFormat charFmt = charFormat(format);
@@ -292,6 +301,7 @@ void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
? *res.formatOverride : outputTypeForParser(involvedParsers.last(), format);
if (formatForParser != format && cleanLine == text && formattedText.length() == 1) {
charFmt = charFormat(formatForParser);
checkAndFineTuneColors(&charFmt);
formattedText.first().format = charFmt;
}
}
@@ -302,8 +312,10 @@ void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
}
const QList<FormattedText> linkified = linkifiedText(formattedText, res.linkSpecs);
for (const FormattedText &output : linkified)
for (FormattedText output : linkified) {
checkAndFineTuneColors(&output.format);
append(output.text, output.format);
}
if (linkified.isEmpty())
append({}, charFmt); // This might cause insertion of a newline character.

View File

@@ -726,4 +726,29 @@ bool StyleHelper::isReadableOn(const QColor &background, const QColor &foregroun
return contrastRatio(background, foreground) > 3;
}
QColor StyleHelper::ensureReadableOn(const QColor &background, const QColor &desiredForeground)
{
if (isReadableOn(background, desiredForeground))
return desiredForeground;
int h, s, v;
QColor foreground = desiredForeground;
foreground.getHsv(&h, &s, &v);
// adjust the color value to ensure better readability
if (luminance(background) < .5)
v = v + 64;
else if (v >= 64)
v = v - 64;
v %= 256;
foreground.setHsv(h, s, v);
if (!isReadableOn(background, foreground)) {
s = (s + 128) % 256; // adjust the saturation to ensure better readability
foreground.setHsv(h, s, v);
if (!isReadableOn(background, foreground)) // we failed to create some better foreground
return desiredForeground;
}
return foreground;
}
} // namespace Utils

View File

@@ -112,6 +112,8 @@ public:
static double luminance(const QColor &color);
static bool isReadableOn(const QColor &background, const QColor &foreground);
// returns a foreground color readable on background (desiredForeground if already readable or adaption fails)
static QColor ensureReadableOn(const QColor &background, const QColor &desiredForeground);
private:
static QColor m_baseColor;