[LyX/master] Try to automatically handle transparent pictures in darkmode (#12076)

Juergen Spitzmueller spitz at lyx.org
Sun Jan 17 10:03:05 UTC 2021


commit 5d47a7ee575065847bc692063b8a641bec3765e8
Author: Juergen Spitzmueller <spitz at lyx.org>
Date:   Sun Jan 17 11:03:21 2021 +0100

    Try to automatically handle transparent pictures in darkmode (#12076)
    
    We'll see how this plays in practice.
---
 src/frontends/Painter.h         |    2 +-
 src/frontends/qt/GuiPainter.cpp |   23 +++++++++++++++++++++--
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/frontends/Painter.h b/src/frontends/Painter.h
index 6deeda6..580b72e 100644
--- a/src/frontends/Painter.h
+++ b/src/frontends/Painter.h
@@ -127,7 +127,7 @@ public:
 
 	/// draw an image from the image cache
 	virtual void image(int x, int y, int w, int h,
-		graphics::Image const & image, bool const darkmode = false) = 0;
+		graphics::Image const & image, bool revert_in_darkmode = false) = 0;
 
 	/// draw a string at position x, y (y is the baseline).
 	virtual void text(int x, int y, docstring const & str, FontInfo const & f) = 0;
diff --git a/src/frontends/qt/GuiPainter.cpp b/src/frontends/qt/GuiPainter.cpp
index 4decbfa..c3e5b37 100644
--- a/src/frontends/qt/GuiPainter.cpp
+++ b/src/frontends/qt/GuiPainter.cpp
@@ -233,7 +233,8 @@ void GuiPainter::arc(int x, int y, unsigned int w, unsigned int h,
 }
 
 
-void GuiPainter::image(int x, int y, int w, int h, graphics::Image const & i, bool const darkmode)
+void GuiPainter::image(int x, int y, int w, int h, graphics::Image const & i,
+		       bool revert_in_darkmode)
 {
 	graphics::GuiImage const & qlimage =
 		static_cast<graphics::GuiImage const &>(i);
@@ -246,7 +247,25 @@ void GuiPainter::image(int x, int y, int w, int h, graphics::Image const & i, bo
 	QColor text_color = palette.color(QPalette::Active, QPalette::WindowText);
 	QColor bg_color = palette.color(QPalette::Active, QPalette::Window);
 	// guess whether we are in dark mode
-	if (darkmode && text_color.black() < bg_color.black())
+	bool const in_dark_mode = text_color.black() < bg_color.black();
+	// if we are in dark mode, check whether we have transparent pixels
+	if (in_dark_mode && !revert_in_darkmode) {
+		QImage img = image.convertToFormat(QImage::Format_ARGB32);
+		for (int x = 0 ; x < img.width() ; x++) {
+			if (revert_in_darkmode)
+				break;
+			for (int y = 0 ; y < img.height() ; y++) {
+				QRgb currentPixel = (img.pixel(x, y));
+				if (qAlpha(currentPixel) == 0) {
+					// we have transparent pixels, revert
+					// this image in dark mode (#12076)
+					revert_in_darkmode = true;
+					break;
+				}
+			}
+		}
+	}
+	if (in_dark_mode && revert_in_darkmode)
 		// FIXME this is only a cheap approximation
 		// Ideally, replace colors as in GuiApplication::prepareForDarkmode()
 		image.invertPixels();


More information about the lyx-cvs mailing list