From ca05b2e10de4be6f06aa06945a7f4344d97ac93e Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 26 Feb 2026 12:03:40 +0000 Subject: [PATCH] feat(ui_builders_shared): implement def icon_from_svg_data_url --- scanslicer/ui_builders_shared.py | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/scanslicer/ui_builders_shared.py b/scanslicer/ui_builders_shared.py index 0295035..4fab19c 100644 --- a/scanslicer/ui_builders_shared.py +++ b/scanslicer/ui_builders_shared.py @@ -35,3 +35,39 @@ def _icon_color_from_palette(palette): + (0.0722 * window_color.blueF()) ) return "#1f1f1f" if luma >= 0.5 else "#f2f2f2" + + +# icon_from_svg_data_url: function definition for ScanSlicer behavior. +def icon_from_svg_data_url(data_url, size=24, color_hex=None, palette=None): + prefix = "data:image/svg+xml;base64," + if not data_url.startswith(prefix): + return None + + try: + svg_bytes = base64.b64decode(data_url[len(prefix):], validate=True) + except Exception: + return None + + if color_hex is None: + color_hex = _icon_color_from_palette(palette) + + # Most toolbar SVGs use "currentColor"; replace it with the resolved color. + if color_hex: + try: + svg_text = svg_bytes.decode("utf-8") + svg_text = svg_text.replace("currentColor", color_hex) + svg_text = svg_text.replace("currentcolor", color_hex) + svg_bytes = svg_text.encode("utf-8") + except UnicodeDecodeError: + pass + + renderer = QSvgRenderer(svg_bytes) + if not renderer.isValid(): + return None + + pixmap = QPixmap(size, size) + pixmap.fill(Qt.GlobalColor.transparent) + painter = QPainter(pixmap) + renderer.render(painter) + painter.end() + return QIcon(pixmap)