|
1 | 1 | from copy import deepcopy
|
2 | 2 |
|
| 3 | +import shutil |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import matplotlib |
3 | 7 | import napari
|
4 | 8 | import numpy as np
|
5 | 9 | import pytest
|
| 10 | +from matplotlib.colors import to_rgba |
6 | 11 |
|
7 |
| -from napari_matplotlib import ScatterWidget |
| 12 | +from napari_matplotlib import HistogramWidget, ScatterWidget |
8 | 13 | from napari_matplotlib.base import NapariMPLWidget
|
9 | 14 |
|
10 | 15 |
|
@@ -140,3 +145,34 @@ def test_custom_theme(make_napari_viewer, theme_path, brain_data):
|
140 | 145 | viewer.layers.selection.add(viewer.layers[1])
|
141 | 146 |
|
142 | 147 | return deepcopy(widget.figure)
|
| 148 | + |
| 149 | + |
| 150 | +def find_mpl_stylesheet(name: str) -> Path: |
| 151 | + """Find the built-in matplotlib stylesheet.""" |
| 152 | + return Path(matplotlib.__path__[0]) / f"mpl-data/stylelib/{name}.mplstyle" |
| 153 | + |
| 154 | + |
| 155 | +def test_stylesheet_in_cwd(tmpdir, make_napari_viewer, image_data): |
| 156 | + """ |
| 157 | + Test that a stylesheet in the current directory is given precidence. |
| 158 | +
|
| 159 | + Do this by copying over a stylesheet from matplotlib's built in styles, |
| 160 | + naming it correctly, and checking the colours are as expected. |
| 161 | + """ |
| 162 | + with tmpdir.as_cwd(): |
| 163 | + # Copy Solarize_Light2 to current dir as if it was a user-overriden stylesheet. |
| 164 | + shutil.copy(find_mpl_stylesheet("Solarize_Light2"), "./user.mplstyle") |
| 165 | + viewer = make_napari_viewer() |
| 166 | + viewer.add_image(image_data[0], **image_data[1]) |
| 167 | + widget = HistogramWidget(viewer) |
| 168 | + ax = widget.figure.gca() |
| 169 | + |
| 170 | + # The axes should have a light brownish grey background: |
| 171 | + assert ax.get_facecolor() == to_rgba("#eee8d5") |
| 172 | + assert ax.patch.get_facecolor() == to_rgba("#eee8d5") |
| 173 | + |
| 174 | + # The figure background and axis gridlines are light yellow: |
| 175 | + assert widget.figure.patch.get_facecolor() == to_rgba("#fdf6e3") |
| 176 | + for gridline in ax.get_xgridlines() + ax.get_ygridlines(): |
| 177 | + assert gridline.get_visible() is True |
| 178 | + assert gridline.get_color() == "#fdf6e3" |
0 commit comments