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

Code before:
    def _create_pixmap(self, image_path, size, dpr, is_svg=False):
        """Create a pixmap from image file with proper scaling and dark mode support."""
        # Create high-resolution pixmap
        scaled_size = QtCore.QSize(int(size.width() * dpr), int(size.height() * dpr))
        pixmap = QtGui.QPixmap(scaled_size)
        pixmap.setDevicePixelRatio(dpr)
        pixmap.fill(QtCore.Qt.GlobalColor.transparent)

        if is_svg:
            return self._render_svg(image_path, pixmap, size, dpr)
        else:
            return self._render_png(image_path, pixmap, scaled_size, dpr)

    def _render_svg(self, svg_path, pixmap, size, dpr):
        """Render SVG content to pixmap."""
        try:
            from PyQt5.QtSvg import QSvgRenderer
        except Exception:
            from PySide2.QtSvg import QSvgRenderer

        svg_content = svg_path.read_bytes()

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

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

        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 _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

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

Code after:
            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()

        dpr = self._devicePixelRatio()
        scaled_size = QtCore.QSize(int(size.width() * dpr), int(size.height() * dpr))
        pixmap = QtGui.QPixmap(scaled_size)
        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(