diff --git a/src/libs/utils/outputformatter.cpp b/src/libs/utils/outputformatter.cpp index 9f7b0b58475..2bcdffb5864 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 @@ -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 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 52ca9538756..1f57baf5804 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 26869797f69..75b82f38a22 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;