|
13 | 13 | import sys
|
14 | 14 | import weakref
|
15 | 15 |
|
| 16 | +import numpy as np |
| 17 | +import PIL |
| 18 | + |
16 | 19 | import matplotlib as mpl
|
17 | 20 | from matplotlib.backend_bases import (
|
18 | 21 | _Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
|
@@ -1114,7 +1117,7 @@ def __init__(self, canvas):
|
1114 | 1117 | self.wx_ids[text] = (
|
1115 | 1118 | self.AddTool(
|
1116 | 1119 | -1,
|
1117 |
| - bitmap=_load_bitmap(f"{image_file}.png"), |
| 1120 | + bitmap=self._icon(f"{image_file}.png"), |
1118 | 1121 | bmpDisabled=wx.NullBitmap,
|
1119 | 1122 | label=text, shortHelp=text, longHelp=tooltip_text,
|
1120 | 1123 | kind=(wx.ITEM_CHECK if text in ["Pan", "Zoom"]
|
@@ -1146,6 +1149,31 @@ def __init__(self, canvas):
|
1146 | 1149 | zoomStartX = cbook._deprecate_privatize_attribute("3.3")
|
1147 | 1150 | zoomStartY = cbook._deprecate_privatize_attribute("3.3")
|
1148 | 1151 |
|
| 1152 | + @staticmethod |
| 1153 | + def _icon(name): |
| 1154 | + """ |
| 1155 | + Construct a `wx.Bitmap` suitable for use as icon from an image file |
| 1156 | + *name*, including the extension and relative to Matplotlib's "images" |
| 1157 | + data directory. |
| 1158 | + """ |
| 1159 | + image = np.array(PIL.Image.open(cbook._get_data_path("images", name))) |
| 1160 | + try: |
| 1161 | + dark = wx.SystemSettings.GetAppearance().IsDark() |
| 1162 | + except AttributeError: # wxpython < 4.1 |
| 1163 | + # copied from wx's IsUsingDarkBackground / GetLuminance. |
| 1164 | + bg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW) |
| 1165 | + fg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT) |
| 1166 | + # See wx.Colour.GetLuminance. |
| 1167 | + bg_lum = (.299 * bg.red + .587 * bg.green + .114 * bg.blue) / 255 |
| 1168 | + fg_lum = (.299 * fg.red + .587 * fg.green + .114 * fg.blue) / 255 |
| 1169 | + dark = fg_lum - bg_lum > .2 |
| 1170 | + if dark: |
| 1171 | + fg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT) |
| 1172 | + black_mask = (image[..., :3] == 0).all(axis=-1) |
| 1173 | + image[black_mask, :3] = (fg.Red(), fg.Green(), fg.Blue()) |
| 1174 | + return wx.Bitmap.FromBufferRGBA( |
| 1175 | + image.shape[1], image.shape[0], image.tobytes()) |
| 1176 | + |
1149 | 1177 | def get_canvas(self, frame, fig):
|
1150 | 1178 | return type(self.canvas)(frame, -1, fig)
|
1151 | 1179 |
|
@@ -1375,7 +1403,7 @@ def add_toolitem(self, name, group, position, image_file, description,
|
1375 | 1403 | start = self._get_tool_pos(sep) + 1
|
1376 | 1404 | idx = start + position
|
1377 | 1405 | if image_file:
|
1378 |
| - bmp = _load_bitmap(image_file) |
| 1406 | + bmp = NavigationToolbar2Wx._icon(image_file) |
1379 | 1407 | kind = wx.ITEM_NORMAL if not toggle else wx.ITEM_CHECK
|
1380 | 1408 | tool = self.InsertTool(idx, -1, name, bmp, wx.NullBitmap, kind,
|
1381 | 1409 | description or "")
|
|
0 commit comments