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

Code before:

    This engine provides icons on-demand with proper scaling based on the current
    device pixel ratio, eliminating the need for manual refresh when DPI changes.
    """

    def __init__(self, image_path, toolbar=None):
        super().__init__()
        self.image_path = image_path
        self.toolbar = toolbar

    def _is_dark_mode(self):
        return self.toolbar.palette().color(self.toolbar.backgroundRole()).value() < 128

    def paint(self, painter, rect, mode, state):
        """Paint the icon at the requested size and state."""
        pixmap = self.pixmap(rect.size(), mode, state)
        if not pixmap.isNull():
            painter.drawPixmap(rect, pixmap)

    def pixmap(self, size, mode, state):
        """Generate a pixmap for the requested size, mode, and state."""
        if size.width() <= 0 or size.height() <= 0:
            return QtGui.QPixmap()

        # Get the current device pixel ratio
        dpr = (self.toolbar.devicePixelRatioF() or 1) if self.toolbar else 1

        # Try SVG first, then fall back to PNG
        svg_path = self.image_path.with_suffix('.svg')
        if svg_path.exists():
            return self._create_pixmap_from_svg(svg_path, size, dpr)
        else:
            large_path = self.image_path.with_name(self.image_path.stem + '_large.png')
            path = large_path if large_path.exists() else self.image_path
            scaled_size = QtCore.QSize(
                int(size.width() * dpr),
                int(size.height() * dpr)
            )
            return self._create_pixmap_from_png(path, scaled_size, dpr)

    def _create_pixmap_from_svg(self, svg_path, size, dpr):
        """Create a pixmap from SVG with proper scaling and dark mode support."""
        QSvgRenderer = getattr(QtSvg, "QSvgRenderer", None)
        if QSvgRenderer is None:
            return QtGui.QPixmap()

        svg_content = svg_path.read_bytes()

        if self._is_dark_mode():
            svg_content = svg_content.replace(b'fill:black;', b'fill:white;')
            svg_content = svg_content.replace(b'stroke:black;', b'stroke:white;')

Review comment (refactor):
Idea for further simplification:

move this into a private function `_devicePixelRatio()`.

Then ...

Code after:

    This engine provides icons on-demand with proper scaling based on the current
    device pixel ratio, eliminating the need for manual refresh when DPI changes.
    """

    def __init__(self, image_path, toolbar=None):
        super().__init__()
        self.image_path = image_path
        self.toolbar = toolbar

    def _is_dark_mode(self):
        return self.toolbar.palette().color(self.toolbar.backgroundRole()).value() < 128

    def paint(self, painter, rect, mode, state):
        """Paint the icon at the requested size and state."""
        pixmap = self.pixmap(rect.size(), mode, state)
        if not pixmap.isNull():
            painter.drawPixmap(rect, pixmap)

    def pixmap(self, size, mode, state):
        """Generate a pixmap for the requested size, mode, and state."""
        if size.width() <= 0 or size.height() <= 0:
            return QtGui.QPixmap()

        # Try SVG first, then fall back to PNG
        svg_path = self.image_path.with_suffix('.svg')
        if svg_path.exists():
            pixmap = self._create_pixmap_from_svg(svg_path, size)
            if not pixmap.isNull():
                return pixmap
        return self._create_pixmap_from_png(self.image_path, size)

    def _devicePixelRatio(self):
        """Return the current device pixel ratio for the toolbar, defaulting to 1."""
        return (self.toolbar.devicePixelRatioF() or 1) if self.toolbar else 1

    def _create_pixmap_from_svg(self, svg_path, size):
        """Create a pixmap from SVG with proper scaling and dark mode support."""
        QSvgRenderer = getattr(QtSvg, "QSvgRenderer", None)
        if QSvgRenderer is None:
            return QtGui.QPixmap()

        svg_content = svg_path.read_bytes()

        if self._is_dark_mode():
            svg_content = svg_content.replace(b'fill:black;', b'fill:white;')
            svg_content = svg_content.replace(b'stroke:black;', b'stroke:white;')

        renderer = QSvgRenderer(QtCore.QByteArray(svg_content))
        if not renderer.isValid():
            return QtGui.QPixmap()