Repository: matplotlib/matplotlib
File: lib/matplotlib/backends/backend_qt.py (Python)

Code before:
            renderer.render(painter, QtCore.QRectF(0, 0, size.width(), size.height()))
        finally:
            if painter.isActive():
                painter.end()

        return pixmap

    def _render_png(self, png_path, pixmap, scaled_size, dpr):
        """Render PNG content to pixmap with scaling and dark mode support."""
        # Load PNG
        source_pixmap = QtGui.QPixmap(str(png_path))
        if source_pixmap.isNull():
            return QtGui.QPixmap()

        # Scale to requested size
        scaled_pixmap = source_pixmap.scaled(
            scaled_size,
            QtCore.Qt.AspectRatioMode.KeepAspectRatio,
            QtCore.Qt.TransformationMode.SmoothTransformation
        )
        scaled_pixmap.setDevicePixelRatio(dpr)

        # Apply dark mode adaptation if needed
        if (self.toolbar and
            self.toolbar.palette().color(self.toolbar.backgroundRole()).value() < 128):

            icon_color = self.toolbar.palette().color(self.toolbar.foregroundRole())
            # For some reason, it may not work.
            mask = scaled_pixmap.createMaskFromColor(
                QtGui.QColor('black'),
                QtCore.Qt.MaskMode.MaskOutColor)
            scaled_pixmap.fill(icon_color)
            scaled_pixmap.setMask(mask)

        return scaled_pixmap


class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
    toolitems = [*NavigationToolbar2.toolitems]
    toolitems.insert(
        # Add 'customize' action after 'subplots'
        [name for name, *_ in toolitems].index("Subplots") + 1,
        ("Customize", "Edit axis, curve and image parameters",
         "qt4_editor_options", "edit_parameters"))

    def __init__(self, canvas, parent=None, coordinates=True):
        """coordinates: should we show the coordinates on the right?"""
        QtWidgets.QToolBar.__init__(self, parent)
        self.setAllowedAreas(QtCore.Qt.ToolBarArea(
            _to_int(QtCore.Qt.ToolBarArea.TopToolBarArea) |
            _to_int(QtCore.Qt.ToolBarArea.BottomToolBarArea)))

Review comment (suggestion):
Should be indented more.
```suggestion
        if (self.toolbar and
                self.toolbar.palette().color(self.toolbar.backgroundRole()).value() < 128):
```

Code after:
        for candidate in (large_path, base_path):
            if not candidate.exists():
                continue
            candidate_pixmap = QtGui.QPixmap(str(candidate))
            if not candidate_pixmap.isNull():
                source_pixmap = candidate_pixmap
                break
        if source_pixmap.isNull():
            return source_pixmap

        dpr = self._devicePixelRatio()

        # Scale to requested size
        scaled_size = QtCore.QSize(int(size.width() * dpr), int(size.height() * dpr))
        scaled_pixmap = source_pixmap.scaled(
            scaled_size,
            QtCore.Qt.AspectRatioMode.KeepAspectRatio,
            QtCore.Qt.TransformationMode.SmoothTransformation
        )
        scaled_pixmap.setDevicePixelRatio(dpr)

        if self._is_dark_mode():
            # On some platforms (e.g., macOS with Qt5 in dark mode), this may
            # incorrectly return a black color instead of a light one.
            # See issue #27590 for details.
            icon_color = self.toolbar.palette().color(self.toolbar.foregroundRole())
            mask = scaled_pixmap.createMaskFromColor(
                QtGui.QColor('black'),
                QtCore.Qt.MaskMode.MaskOutColor)
            scaled_pixmap.fill(icon_color)
            scaled_pixmap.setMask(mask)

        return scaled_pixmap


class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
    toolitems = [*NavigationToolbar2.toolitems]
    toolitems.insert(
        # Add 'customize' action after 'subplots'
        [name for name, *_ in toolitems].index("Subplots") + 1,
        ("Customize", "Edit axis, curve and image parameters",
         "qt4_editor_options", "edit_parameters"))

    def __init__(self, canvas, parent=None, coordinates=True):
        """coordinates: should we show the coordinates on the right?"""
        QtWidgets.QToolBar.__init__(self, parent)
        self.setAllowedAreas(QtCore.Qt.ToolBarArea(
            _to_int(QtCore.Qt.ToolBarArea.TopToolBarArea) |
            _to_int(QtCore.Qt.ToolBarArea.BottomToolBarArea)))
        self.coordinates = coordinates
        self._actions = {}  # mapping of toolitem method names to QActions.