|
2 | 2 | # License: BSD-3-Clause |
3 | 3 | # Copyright the MNE-Python contributors. |
4 | 4 |
|
5 | | -import platform |
6 | 5 | from colorsys import rgb_to_hls |
7 | 6 |
|
8 | 7 | import numpy as np |
9 | 8 | import pytest |
10 | 9 |
|
11 | 10 | from mne import create_info |
12 | 11 | from mne.io import RawArray |
13 | | -from mne.utils import _check_qt_version |
14 | 12 | from mne.viz.backends._utils import ( |
15 | 13 | _check_color, |
16 | 14 | _get_colormap_from_array, |
@@ -50,37 +48,36 @@ def test_check_color(): |
50 | 48 | _check_color(None) |
51 | 49 |
|
52 | 50 |
|
| 51 | +def _assert_correct_darkness(widget, want_dark): |
| 52 | + __tracebackhide__ = True # noqa |
| 53 | + # The override propagates to children, so both palette and pixels should match. |
| 54 | + bgcolor = widget.palette().color(widget.backgroundRole()).getRgbF()[:3] |
| 55 | + dark = rgb_to_hls(*bgcolor)[1] < 0.5 |
| 56 | + assert dark == want_dark, f"{widget} palette dark={dark} want_dark={want_dark}" |
| 57 | + colors = _pixmap_to_ndarray(widget.grab())[:, :, :3] |
| 58 | + dark = colors.mean() < 0.5 |
| 59 | + assert dark == want_dark, f"{widget} pixmap dark={dark} want_dark={want_dark}" |
| 60 | + |
| 61 | + |
53 | 62 | @pytest.mark.pgtest |
54 | 63 | @pytest.mark.parametrize("theme", ("auto", "light", "dark")) |
55 | 64 | def test_theme_colors(pg_backend, theme, monkeypatch, tmp_path): |
56 | 65 | """Test that theme colors propagate properly.""" |
57 | 66 | darkdetect = pytest.importorskip("darkdetect") |
58 | 67 | monkeypatch.setenv("_MNE_FAKE_HOME_DIR", str(tmp_path)) |
59 | 68 | monkeypatch.delenv("MNE_BROWSER_THEME", raising=False) |
60 | | - # make it seem like the system is always in light mode |
61 | | - monkeypatch.setattr(darkdetect, "theme", lambda: "light") |
| 69 | + # A qdarkstyle stylesheet is only applied when the requested theme differs from |
| 70 | + # the system, so fake the system as the opposite of the request |
| 71 | + if theme == "auto": |
| 72 | + want_dark = (darkdetect.theme() or "light").lower() == "dark" |
| 73 | + else: |
| 74 | + want_dark = theme == "dark" |
| 75 | + fake_system = "light" if want_dark else "dark" |
| 76 | + monkeypatch.setattr(darkdetect, "theme", lambda: fake_system) |
62 | 77 | raw = RawArray(np.zeros((1, 1000)), create_info(1, 1000.0, "eeg")) |
63 | | - _, api = _check_qt_version(return_api=True) |
64 | 78 | fig = raw.plot(theme=theme) |
65 | 79 | is_dark = _qt_is_dark(fig) |
66 | | - # on Darwin these checks get complicated, so don't bother for now |
67 | | - if platform.system() == "Darwin": |
68 | | - pytest.skip("Problems on macOS") |
69 | | - if theme == "dark": |
70 | | - assert is_dark, theme |
71 | | - elif theme == "light": |
72 | | - assert not is_dark, theme |
73 | | - |
74 | | - def assert_correct_darkness(widget, want_dark): |
75 | | - __tracebackhide__ = True # noqa |
76 | | - # This should work, but it just picks up the parent in the errant case! |
77 | | - bgcolor = widget.palette().color(widget.backgroundRole()).getRgbF()[:3] |
78 | | - dark = rgb_to_hls(*bgcolor)[1] < 0.5 |
79 | | - assert dark == want_dark, f"{widget} dark={dark} want_dark={want_dark}" |
80 | | - # ... so we use a more direct test |
81 | | - colors = _pixmap_to_ndarray(widget.grab())[:, :, :3] |
82 | | - dark = colors.mean() < 0.5 |
83 | | - assert dark == want_dark, f"{widget} dark={dark} want_dark={want_dark}" |
| 80 | + assert is_dark == want_dark, theme |
84 | 81 |
|
85 | 82 | for widget in (fig.mne.toolbar, fig.statusBar()): |
86 | | - assert_correct_darkness(widget, is_dark) |
| 83 | + _assert_correct_darkness(widget, is_dark) |
0 commit comments