diff options
author | Christian Stenger <christian.stenger@qt.io> | 2023-01-20 11:20:53 +0100 |
---|---|---|
committer | Christian Stenger <christian.stenger@qt.io> | 2023-02-03 06:34:53 +0000 |
commit | 8655603c2e1e5394773160ac1b6756bc89382373 (patch) | |
tree | ba202f3e720d996a8fdacf3411de86124fdb20d8 | |
parent | 2eb9338be0a30572a07c92ac9a59613383fdc055 (diff) |
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>
-rw-r--r-- | src/libs/utils/outputformatter.cpp | 14 | ||||
-rw-r--r-- | src/libs/utils/stylehelper.cpp | 25 | ||||
-rw-r--r-- | src/libs/utils/stylehelper.h | 2 |
3 files changed, 40 insertions, 1 deletions
diff --git a/src/libs/utils/outputformatter.cpp b/src/libs/utils/outputformatter.cpp index 9f7b0b5847..2bcdffb586 100644 --- a/src/libs/utils/outputformatter.cpp +++ b/src/libs/utils/outputformatter.cpp @@ -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. diff --git a/src/libs/utils/stylehelper.cpp b/src/libs/utils/stylehelper.cpp index 52ca953875..1f57baf580 100644 --- a/src/libs/utils/stylehelper.cpp +++ b/src/libs/utils/stylehelper.cpp @@ -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 diff --git a/src/libs/utils/stylehelper.h b/src/libs/utils/stylehelper.h index 26869797f6..75b82f38a2 100644 --- a/src/libs/utils/stylehelper.h +++ b/src/libs/utils/stylehelper.h @@ -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; |