8000 wx: Continue supporting non-SVG for toolmanager's sake · matplotlib/matplotlib@39f28cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 39f28cc

Browse files
committed
wx: Continue supporting non-SVG for toolmanager's sake
This falls back to the previous dark-mode setup for any other formats. It won't be HiDPI compatible, but that's probably not possible for raster formats.
1 parent bce3643 commit 39f28cc

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

lib/matplotlib/backends/backend_wx.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import sys
1515
import weakref
1616

17+
import numpy as np
18+
import PIL.Image
19+
1720
import matplotlib as mpl
1821
from matplotlib.backend_bases import (
1922
_Backend, FigureCanvasBase, FigureManagerBase,
@@ -1071,7 +1074,6 @@ def _icon(name):
10711074
*name*, including the extension and relative to Matplotlib's "images"
10721075
data directory.
10731076
"""
1074-
svg = cbook._get_data_path("images", name).read_bytes()
10751077
try:
10761078
dark = wx.SystemSettings.GetAppearance().IsDark()
10771079
except AttributeError: # wxpython < 4.1
@@ -1082,10 +1084,24 @@ def _icon(name):
10821084
bg_lum = (.299 * bg.red + .587 * bg.green + .114 * bg.blue) / 255
10831085
fg_lum = (.299 * fg.red + .587 * fg.green + .114 * fg.blue) / 255
10841086
dark = fg_lum - bg_lum > .2
1085-
if dark:
1086-
svg = svg.replace(b'fill:black;', b'fill:white;')
1087-
toolbarIconSize = wx.ArtProvider().GetDIPSizeHint(wx.ART_TOOLBAR)
1088-
return wx.BitmapBundle.FromSVG(svg, toolbarIconSize)
1087+
1088+
path = cbook._get_data_path('images', name)
1089+
if path.suffix == '.svg':
1090+
svg = path.read_bytes()
1091+
if dark:
1092+
svg = svg.replace(b'fill:black;', b'fill:white;')
1093+
toolbarIconSize = wx.ArtProvider().GetDIPSizeHint(wx.ART_TOOLBAR)
1094+
return wx.BitmapBundle.FromSVG(svg, toolbarIconSize)
1095+
else:
1096+
pilimg = PIL.Image.open(path)
1097+
# ensure RGBA as wx BitMap expects RGBA format
1098+
image = np.array(pilimg.convert("RGBA"))
1099+
if dark:
1100+
fg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT)
1101+
black_mask = (image[..., :3] == 0).all(axis=-1)
1102+
image[black_mask, :3] = (fg.Red(), fg.Green(), fg.Blue())
1103+
return wx.Bitmap.FromBufferRGBA(
1104+
image.shape[1], image.shape[0], image.tobytes())
10891105

10901106
def _update_buttons_checked(self):
10911107
if "Pan" in self.wx_ids:

0 commit comments

Comments
 (0)
0