feat(ui_builders_shared): implement def icon_from_svg_data_url
This commit is contained in:
parent
1574e77a22
commit
ca05b2e10d
1 changed files with 36 additions and 0 deletions
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue