14
14
import sys
15
15
import weakref
16
16
17
+ import numpy as np
18
+ import PIL .Image
19
+
17
20
import matplotlib as mpl
18
21
from matplotlib .backend_bases import (
19
22
_Backend , FigureCanvasBase , FigureManagerBase ,
@@ -1071,7 +1074,6 @@ def _icon(name):
1071
1074
*name*, including the extension and relative to Matplotlib's "images"
1072
1075
data directory.
1073
1076
"""
1074
- svg = cbook ._get_data_path ("images" , name ).read_bytes ()
1075
1077
try :
1076
1078
dark = wx .SystemSettings .GetAppearance ().IsDark ()
1077
1079
except AttributeError : # wxpython < 4.1
@@ -1082,10 +1084,24 @@ def _icon(name):
1082
1084
bg_lum = (.299 * bg .red + .587 * bg .green + .114 * bg .blue ) / 255
1083
1085
fg_lum = (.299 * fg .red + .587 * fg .green + .114 * fg .blue ) / 255
1084
1086
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 ())
1089
1105
1090
1106
def _update_buttons_checked (self ):
1091
1107
if "Pan" in self .wx_ids :
@@ -1136,7 +1152,7 @@ def save_figure(self, *args):
1136
1152
1137
1153
def draw_rubberband (self , event , x0 , y0 , x1 , y1 ):
1138
1154
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 ()
1140
1156
self .canvas ._rubberband_rect = (x0 / sf , (height - y0 )/ sf ,
1141
1157
x1 / sf , (height - y1 )/ sf )
1142
1158
self .canvas .Refresh ()
@@ -1161,6 +1177,8 @@ def set_history_buttons(self):
1161
1177
# tools for matplotlib.backend_managers.ToolManager:
1162
1178
1163
1179
class ToolbarWx (ToolContainerBase , wx .ToolBar ):
1180
+ _icon_extension = '.svg'
1181
+
1164
1182
def __init__ (self , toolmanager , parent = None , style = wx .TB_BOTTOM ):
1165
1183
if parent is None :
1166
1184
parent = toolmanager .canvas .GetParent ()
0 commit comments