Skip to content

Commit 7021730

Browse files
authored
Merge pull request #17459 from anntzer/gtkwxdark
Use light icons on dark themes for wx and gtk, too.
2 parents 5445d77 + 03e0cb1 commit 7021730

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,10 @@ def basedir(self):
703703
return str(cbook._get_data_path('images'))
704704

705705
def _icon(self, name):
706+
"""
707+
Construct a `.QIcon` from an image file *name*, including the extension
708+
and relative to Matplotlib's "images" data directory.
709+
"""
706710
if QtCore.qVersion() >= '5.':
707711
name = name.replace('.png', '_large.png')
708712
pm = QtGui.QPixmap(str(cbook._get_data_path('images', name)))

lib/matplotlib/backends/backend_wx.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import sys
1414
import weakref
1515

16+
import numpy as np
17+
import PIL
18+
1619
import matplotlib as mpl
1720
from matplotlib.backend_bases import (
1821
_Backend, FigureCanvasBase, FigureManagerBase, GraphicsContextBase,
@@ -1114,7 +1117,7 @@ def __init__(self, canvas):
11141117
self.wx_ids[text] = (
11151118
self.AddTool(
11161119
-1,
1117-
bitmap=_load_bitmap(f"{image_file}.png"),
1120+
bitmap=self._icon(f"{image_file}.png"),
11181121
bmpDisabled=wx.NullBitmap,
11191122
label=text, shortHelp=text, longHelp=tooltip_text,
11201123
kind=(wx.ITEM_CHECK if text in ["Pan", "Zoom"]
@@ -1146,6 +1149,31 @@ def __init__(self, canvas):
11461149
zoomStartX = cbook._deprecate_privatize_attribute("3.3")
11471150
zoomStartY = cbook._deprecate_privatize_attribute("3.3")
11481151

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+
11491177
def get_canvas(self, frame, fig):
11501178
return type(self.canvas)(frame, -1, fig)
11511179

@@ -1375,7 +1403,7 @@ def add_toolitem(self, name, group, position, image_file, description,
13751403
start = self._get_tool_pos(sep) + 1
13761404
idx = start + position
13771405
if image_file:
1378-
bmp = _load_bitmap(image_file)
1406+
bmp = NavigationToolbar2Wx._icon(image_file)
13791407
kind = wx.ITEM_NORMAL if not toggle else wx.ITEM_CHECK
13801408
tool = self.InsertTool(idx, -1, name, bmp, wx.NullBitmap, kind,
13811409
description or "")

0 commit comments

Comments
 (0)