[LyX/master] Move function
Juergen Spitzmueller <[email protected]> Sun, 10 May 2026 08:16:18 +0000
| Newsgroups | gmane.editors.lyx.cvs |
|---|---|
| Message-ID | <[email protected]> |
commit c316f29b42d48ae85d14bcd5ddcfa79cc96b7798 Author: Juergen Spitzmueller <[email protected]> Date: Sun May 10 10:16:00 2026 +0200 Move function --- src/RowPainter.cpp | 34 +--------------------------------- src/frontends/Application.h | 6 ++++++ src/frontends/qt/ColorCache.cpp | 27 +++++++++++++++++++++++++++ src/frontends/qt/ColorCache.h | 7 +++++++ src/frontends/qt/GuiApplication.cpp | 6 ++++++ src/frontends/qt/GuiApplication.h | 1 + 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/RowPainter.cpp b/src/RowPainter.cpp index cced976d6f..fd45245ea7 100644 --- a/src/RowPainter.cpp +++ b/src/RowPainter.cpp @@ -41,7 +41,6 @@ #include "support/lassert.h" #include <algorithm> -#include <cmath> using namespace std; @@ -248,37 +247,6 @@ void RowPainter::paintMisspelledMark(Row::Element const & e) const } -namespace { - -// check whether a color is relatively light (i.e., needs -// a dark one as contrast) depending on relative luminance -// (cf. https://www.w3.org/TR/WCAG20/#relativeluminancedef) -bool isLightColor(RGBColor const & rgb) -{ - // RGB to sRGB - double r = rgb.r / 255.0; - if (r <= 0.04045) - r = r / 12.92; - else - r = pow((r + 0.055) / 1.055, 2.4); - double g = rgb.g / 255.0; - if (g <= 0.04045) - g = g / 12.92; - else - g = pow((g + 0.055) / 1.055, 2.4); - double b = rgb.b / 255.0; - if (b <= 0.04045) - b = b / 12.92; - else - b = pow((b + 0.055) / 1.055, 2.4); - // Calculate luminance - double L = 0.2126 * r + 0.7152 * g + 0.0722 * b; - - return (L + 0.05) / (0.0 + 0.05) > (1.0 + 0.05) / (L + 0.05); -} -} // namespace anon - - void RowPainter::paintStringAndSel(Row::Element const & e) const { // at least part of text selected? @@ -290,7 +258,7 @@ void RowPainter::paintStringAndSel(Row::Element const & e) const bool invert = false; if (theApp() && theApp()->isInDarkMode() - && isLightColor(rgbFromHexName(lcolor.getX11HexName(pi_.background_color, true)))) + && theApp()->isLightColor(rgbFromHexName(lcolor.getX11HexName(pi_.background_color, true)))) invert = true; if (e.type == Row::PREEDIT) { diff --git a/src/frontends/Application.h b/src/frontends/Application.h index f52b124e2c..615cca5150 100644 --- a/src/frontends/Application.h +++ b/src/frontends/Application.h @@ -214,6 +214,12 @@ public: */ virtual bool isInDarkMode() = 0; + /** + * @return true this is a relatively light color + * depending on relative luminance + */ + virtual bool isLightColor(RGBColor const & rgb) = 0; + /** Eg, passing Color_black returns "000000", * passing Color_white returns "ffffff". */ diff --git a/src/frontends/qt/ColorCache.cpp b/src/frontends/qt/ColorCache.cpp index bdf4ab1d8b..dbc9a72040 100644 --- a/src/frontends/qt/ColorCache.cpp +++ b/src/frontends/qt/ColorCache.cpp @@ -17,6 +17,8 @@ #include "Color.h" +#include <cmath> + namespace lyx { namespace{ @@ -155,6 +157,31 @@ bool ColorCache::isDarkMode() const } +bool ColorCache::isLightColor(RGBColor const & rgb) +{ + // RGB to sRGB + double r = rgb.r / 255.0; + if (r <= 0.04045) + r = r / 12.92; + else + r = pow((r + 0.055) / 1.055, 2.4); + double g = rgb.g / 255.0; + if (g <= 0.04045) + g = g / 12.92; + else + g = pow((g + 0.055) / 1.055, 2.4); + double b = rgb.b / 255.0; + if (b <= 0.04045) + b = b / 12.92; + else + b = pow((b + 0.055) / 1.055, 2.4); + // Calculate luminance + double L = 0.2126 * r + 0.7152 * g + 0.0722 * b; + + return (L + 0.05) / (0.0 + 0.05) > (1.0 + 0.05) / (L + 0.05); +} + + QColor const rgb2qcolor(RGBColor const & rgb) { return QColor(rgb.r, rgb.g, rgb.b); diff --git a/src/frontends/qt/ColorCache.h b/src/frontends/qt/ColorCache.h index 105a97df87..ced8177b87 100644 --- a/src/frontends/qt/ColorCache.h +++ b/src/frontends/qt/ColorCache.h @@ -47,6 +47,13 @@ public: /// guess whether we are in dark mode bool isDarkMode() const; + /** + * check whether a color is relatively light (i.e., needs + * a dark one as contrast) depending on relative luminance + * (cf. https://www.w3.org/TR/WCAG20/#relativeluminancedef) + */ + bool isLightColor(RGBColor const & rgb); + /// change the undelying palette void setPalette(QPalette const pal) { pal_ = pal; clear(); } diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp index e786fea150..f3070c525d 100644 --- a/src/frontends/qt/GuiApplication.cpp +++ b/src/frontends/qt/GuiApplication.cpp @@ -3091,6 +3091,12 @@ bool GuiApplication::isInDarkMode() } +bool GuiApplication::isLightColor(RGBColor const & rgb) +{ + return colorCache().isLightColor(rgb); +} + + bool GuiApplication::getRgbColor(ColorCode col, RGBColor & rgbcol) { QColor const & qcol = d->color_cache_.get(col); diff --git a/src/frontends/qt/GuiApplication.h b/src/frontends/qt/GuiApplication.h index 2b8040eff9..db84925ca9 100644 --- a/src/frontends/qt/GuiApplication.h +++ b/src/frontends/qt/GuiApplication.h @@ -75,6 +75,7 @@ public: bool event(QEvent * e) override; bool getRgbColor(ColorCode col, RGBColor & rgbcol) override; bool isInDarkMode() override; + bool isLightColor(RGBColor const & rgb) override; std::string const hexName(ColorCode col) override; void registerSocketCallback(int fd, SocketCallback func) override; void unregisterSocketCallback(int fd) override; -- lyx-cvs mailing list [email protected] https://lists.lyx.org/mailman/listinfo/lyx-cvs