]> git.lyx.org Git - features.git/commitdiff
Try to automatically handle transparent pictures in darkmode (#12076)
authorJuergen Spitzmueller <spitz@lyx.org>
Sun, 17 Jan 2021 10:03:21 +0000 (11:03 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Sun, 17 Jan 2021 10:03:21 +0000 (11:03 +0100)
We'll see how this plays in practice.

src/frontends/Painter.h
src/frontends/qt/GuiPainter.cpp

index 6deeda608dd325afc2aba16cf12990c64ee0ee4a..580b72e59eeedb2274311356aab01b8bdf280fda 100644 (file)
@@ -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;
index 4decbfa0a4a304af6d38fc8b6769b60e0382d367..c3e5b3772fa0ef655a0f2b7c95a9a344a04a2443 100644 (file)
@@ -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();