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

Code before:
        pixmap.fill(QtCore.Qt.GlobalColor.transparent)

        painter = QtGui.QPainter()
        try:
            painter.begin(pixmap)
            renderer.render(painter, QtCore.QRectF(0, 0, size.width(), size.height()))
        finally:
            if painter.isActive():
                painter.end()

        return pixmap

    def _create_pixmap_from_png(self, base_path, size):
        """
        Create a pixmap from PNG with scaling and dark mode support.

        Prefer to use the *_large.png with the same name; otherwise, use base_path.
        """
        large_path = base_path.with_name(base_path.stem + '_large.png')
        png_path = large_path if large_path.exists() else base_path

        dpr = self._devicePixelRatio()

        source_pixmap = QtGui.QPixmap(str(png_path))
        if source_pixmap.isNull():
            return QtGui.QPixmap()

        # 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):

Review comment (bug):
May also want to fall back to the `base_path` here if the `large_path` failed. Also, you could just return `source_pixmap` here, no? It will do nothing in the caller since it satisfies `pixmap.isNull()` already.

Code after:
        pixmap.setDevicePixelRatio(dpr)
        pixmap.fill(QtCore.Qt.GlobalColor.transparent)

        painter = QtGui.QPainter()
        try:
            painter.begin(pixmap)
            renderer.render(painter, QtCore.QRectF(0, 0, size.width(), size.height()))
        finally:
            if painter.isActive():
                painter.end()

        return pixmap

    def _create_pixmap_from_png(self, base_path, size):
        """
        Create a pixmap from PNG with scaling and dark mode support.

        Prefer to use the *_large.png with the same name; otherwise, use base_path.
        """
        large_path = base_path.with_name(base_path.stem + '_large.png')
        source_pixmap = QtGui.QPixmap()
        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)