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

Code before:
        # will resize this label instead of the buttons.
        if self.coordinates:
            self.locLabel = QtWidgets.QLabel("", self)
            self.locLabel.setAlignment(QtCore.Qt.AlignmentFlag(
                _to_int(QtCore.Qt.AlignmentFlag.AlignRight) |
                _to_int(QtCore.Qt.AlignmentFlag.AlignVCenter)))

            self.locLabel.setSizePolicy(QtWidgets.QSizePolicy(
                QtWidgets.QSizePolicy.Policy.Expanding,
                QtWidgets.QSizePolicy.Policy.Ignored,
            ))
            labelAction = self.addWidget(self.locLabel)
            labelAction.setVisible(True)

        NavigationToolbar2.__init__(self, canvas)

    def _icon(self, name):
        """
        Construct a `.QIcon` from an image file *name*, including the extension
        and relative to Matplotlib's "images" data directory.
        """
        # Check for SVG first, then fall back to PNG
        path_regular = cbook._get_data_path('images', name)

        # Try SVG first
        svg_path = path_regular.with_suffix('.svg')
        if svg_path.exists():
            try:
                svg_icon = self._create_svg_icon(svg_path)
                return svg_icon
            except Exception:
                pass

        # Fall back to PNG with high-resolution support
        path_large = path_regular.with_name(
            path_regular.name.replace('.png', '_large.png'))
        filename = str(path_large if path_large.exists() else path_regular)

        pm = QtGui.QPixmap(filename)
        pm.setDevicePixelRatio(
            self.devicePixelRatioF() or 1)  # rarely, devicePixelRatioF=0

        # Apply dark mode color adaptation for PNG icons,
        # but for some reasons, it doesn't work.
        if self.palette().color(self.backgroundRole()).value() < 128:
            icon_color = self.palette().color(self.foregroundRole())
            mask = pm.createMaskFromColor(
                QtGui.QColor('black'),
                QtCore.Qt.MaskMode.MaskOutColor)
            pm.fill(icon_color)
            pm.setMask(mask)

Review comment (suggestion):
I don't think we need to be defensive about this, we should expect the svg files to always be there.

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

        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