Skip to content

Commit 010a02e

Browse files
committed
Better variable names. Add a TODO note about #140.
1 parent 91d6937 commit 010a02e

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/napari_matplotlib/base.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def __init__(
5353
) # type: ignore[no-untyped-call]
5454
self._replace_toolbar_icons()
5555
# callback to update when napari theme changed
56+
# TODO: this isn't working completely (see issue #140)
57+
# most of our styling respects the theme change but not all
5658
self.viewer.events.theme.connect(self._on_theme_change)
5759

5860
self.setLayout(QVBoxLayout())
@@ -77,22 +79,22 @@ def apply_napari_colorscheme(self, ax: Axes) -> None:
7779
"""Apply napari-compatible colorscheme to an Axes."""
7880
# get the foreground colours from current theme
7981
theme = napari.utils.theme.get_theme(self.viewer.theme, as_dict=False)
80-
fg = theme.foreground.as_hex() # fg is a muted contrast to bg
81-
tx = theme.text.as_hex() # text is high contrast to bg
82+
fg_colour = theme.foreground.as_hex() # fg is a muted contrast to bg
83+
text_colour = theme.text.as_hex() # text is high contrast to bg
8284

8385
# changing color of axes background to transparent
8486
ax.set_facecolor("none")
8587

8688
# changing colors of all axes
8789
for spine in ax.spines:
88-
ax.spines[spine].set_color(fg)
90+
ax.spines[spine].set_color(fg_colour)
8991

90-
ax.xaxis.label.set_color(tx)
91-
ax.yaxis.label.set_color(tx)
92+
ax.xaxis.label.set_color(text_colour)
93+
ax.yaxis.label.set_color(text_colour)
9294

9395
# changing colors of axes labels
94-
ax.tick_params(axis="x", colors=tx)
95-
ax.tick_params(axis="y", colors=tx)
96+
ax.tick_params(axis="x", colors=text_colour)
97+
ax.tick_params(axis="y", colors=text_colour)
9698

9799
def _on_theme_change(self) -> None:
98100
"""Update MPL toolbar and axis styling when `napari.Viewer.theme` is changed.
@@ -104,15 +106,24 @@ def _on_theme_change(self) -> None:
104106
if self.figure.gca():
105107
self.apply_napari_colorscheme(self.figure.gca())
106108

109+
def _theme_has_light_bg(self) -> bool:
110+
"""
111+
Does this theme have a light background?
112+
113+
Returns
114+
-------
115+
bool
116+
True if theme's background colour has hsl lighter than 50%, False if darker.
117+
"""
118+
theme = napari.utils.theme.get_theme(self.viewer.theme, as_dict=False)
119+
_, _, bg_lightness = theme.background.as_hsl_tuple()
120+
return bg_lightness > 0.5
121+
107122
def _get_path_to_icon(self) -> Path:
108123
"""
109124
Get the icons directory (which is theme-dependent).
110125
"""
111-
# TODO: can make this more robust by doing some RGB tricks to figure out
112-
# whether white or black icons are going to be more visible given the
113-
# theme.background
114-
islight = self.viewer.theme == "light"
115-
if islight:
126+
if self._theme_has_light_bg():
116127
return ICON_ROOT / "black"
117128
else:
118129
return ICON_ROOT / "white"

0 commit comments

Comments
 (0)