Try to automatically handle transparent pictures in darkmode (#12076)

We'll see how this plays in practice.
This commit is contained in:
Juergen Spitzmueller 2021-01-17 11:03:21 +01:00
parent 606a404632
commit 5d47a7ee57
2 changed files with 22 additions and 3 deletions

View 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;

View 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();