Construct a `wx.Bitmap` suitable for use as icon from an image file *name*, including the extension and relative to Matplotlib's "images" data directory.
(name)
| 1086 | |
| 1087 | @staticmethod |
| 1088 | def _icon(name): |
| 1089 | """ |
| 1090 | Construct a `wx.Bitmap` suitable for use as icon from an image file |
| 1091 | *name*, including the extension and relative to Matplotlib's "images" |
| 1092 | data directory. |
| 1093 | """ |
| 1094 | try: |
| 1095 | dark = wx.SystemSettings.GetAppearance().IsDark() |
| 1096 | except AttributeError: # wxpython < 4.1 |
| 1097 | # copied from wx's IsUsingDarkBackground / GetLuminance. |
| 1098 | bg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW) |
| 1099 | fg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT) |
| 1100 | # See wx.Colour.GetLuminance. |
| 1101 | bg_lum = (.299 * bg.red + .587 * bg.green + .114 * bg.blue) / 255 |
| 1102 | fg_lum = (.299 * fg.red + .587 * fg.green + .114 * fg.blue) / 255 |
| 1103 | dark = fg_lum - bg_lum > .2 |
| 1104 | |
| 1105 | path = cbook._get_data_path('images', name) |
| 1106 | if path.suffix == '.svg': |
| 1107 | svg = path.read_bytes() |
| 1108 | if dark: |
| 1109 | svg = svg.replace(b'fill:black;', b'fill:white;') |
| 1110 | toolbarIconSize = wx.ArtProvider().GetDIPSizeHint(wx.ART_TOOLBAR) |
| 1111 | return wx.BitmapBundle.FromSVG(svg, toolbarIconSize) |
| 1112 | else: |
| 1113 | pilimg = PIL.Image.open(path) |
| 1114 | # ensure RGBA as wx BitMap expects RGBA format |
| 1115 | image = np.array(pilimg.convert("RGBA")) |
| 1116 | if dark: |
| 1117 | fg = wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT) |
| 1118 | black_mask = (image[..., :3] == 0).all(axis=-1) |
| 1119 | image[black_mask, :3] = (fg.Red(), fg.Green(), fg.Blue()) |
| 1120 | return wx.Bitmap.FromBufferRGBA( |
| 1121 | image.shape[1], image.shape[0], image.tobytes()) |
| 1122 | |
| 1123 | def _update_buttons_checked(self): |
| 1124 | if "Pan" in self.wx_ids: |