1414import sys
1515import weakref
1616
17+ import numpy as np
18+ import PIL .Image
19+
1720import matplotlib as mpl
1821from 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 :
@@ -1136,7 +1152,7 @@ def save_figure(self, *args):
11361152
11371153 def draw_rubberband (self , event , x0 , y0 , x1 , y1 ):
11381154 height = self .canvas .figure .bbox .height
1139- sf = 1 if wx .Platform == '__WXMSW__' else self .GetDPIScaleFactor ()
1155+ sf = 1 if wx .Platform == '__WXMSW__' else self .canvas . GetDPIScaleFactor ()
11401156 self .canvas ._rubberband_rect = (x0 / sf , (height - y0 )/ sf ,
11411157 x1 / sf , (height - y1 )/ sf )
11421158 self .canvas .Refresh ()
@@ -1161,6 +1177,8 @@ def set_history_buttons(self):
11611177# tools for matplotlib.backend_managers.ToolManager:
11621178
11631179class ToolbarWx (ToolContainerBase , wx .ToolBar ):
1180+ _icon_extension = '.svg'
1181+
11641182 def __init__ (self , toolmanager , parent = None , style = wx .TB_BOTTOM ):
11651183 if parent is None :
11661184 parent = toolmanager .canvas .GetParent ()
0 commit comments