|
1 | 1 | import os |
| 2 | +import warnings |
| 3 | + |
| 4 | +import matplotlib as mpl |
| 5 | +import matplotlib.colors as mcolors |
2 | 6 | import pytest |
3 | 7 | import numpy as np |
4 | | -import matplotlib.colors as mcolors |
5 | 8 |
|
6 | 9 | from ultraplot import colors as pcolors |
7 | 10 | from ultraplot import config |
8 | 11 |
|
9 | 12 |
|
| 13 | +@pytest.mark.parametrize( |
| 14 | + ("N", "expected"), |
| 15 | + ( |
| 16 | + (None, ["#ff0000", "#0000ff"]), |
| 17 | + (0, []), |
| 18 | + (1, ["#ff0000"]), |
| 19 | + (3, ["#ff0000", "#0000ff", "#ff0000"]), |
| 20 | + ), |
| 21 | +) |
| 22 | +def test_discrete_colormap_resizes_without_listed_n_warning(N, expected): |
| 23 | + """DiscreteColormap preserves N semantics without deprecated mpl input.""" |
| 24 | + with warnings.catch_warnings(): |
| 25 | + warnings.filterwarnings( |
| 26 | + "error", |
| 27 | + message=r"Passing 'N' to ListedColormap.*", |
| 28 | + category=mpl.MatplotlibDeprecationWarning, |
| 29 | + ) |
| 30 | + cmap = pcolors.DiscreteColormap(["red", "blue"], N=N) |
| 31 | + |
| 32 | + assert cmap.N == len(expected) |
| 33 | + assert [mcolors.to_hex(color) for color in cmap.colors] == expected |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + ("N", "expected_size"), |
| 38 | + ( |
| 39 | + (None, 3), |
| 40 | + (1, 1), |
| 41 | + (4, 4), |
| 42 | + ), |
| 43 | +) |
| 44 | +def test_discrete_colormap_expands_scalar_color(N, expected_size): |
| 45 | + """Scalar color strings produce the requested monochromatic colormap.""" |
| 46 | + cmap = pcolors.DiscreteColormap("red", N=N) |
| 47 | + |
| 48 | + assert cmap.N == expected_size |
| 49 | + assert [mcolors.to_hex(color) for color in cmap.colors] == [ |
| 50 | + "#ff0000" |
| 51 | + ] * expected_size |
| 52 | + assert cmap.monochrome is True |
| 53 | + |
| 54 | + |
| 55 | +def test_discrete_colormap_resizing_preserves_alpha_override(): |
| 56 | + """Alpha replacement is applied to every color after cycling the input.""" |
| 57 | + cmap = pcolors.DiscreteColormap([(1, 0, 0, 0.2), (0, 0, 1, 0.8)], N=3, alpha=0.5) |
| 58 | + |
| 59 | + assert [mcolors.to_hex(color, keep_alpha=True) for color in cmap.colors] == [ |
| 60 | + "#ff000080", |
| 61 | + "#0000ff80", |
| 62 | + "#ff000080", |
| 63 | + ] |
| 64 | + assert cmap.monochrome is False |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize( |
| 68 | + ("colors", "expected"), |
| 69 | + ( |
| 70 | + (["red", "red"], True), |
| 71 | + (["red", "blue"], False), |
| 72 | + ), |
| 73 | +) |
| 74 | +def test_discrete_colormap_monochrome_is_python_bool(colors, expected): |
| 75 | + """Monochrome detection remains available across matplotlib versions.""" |
| 76 | + cmap = pcolors.DiscreteColormap(colors) |
| 77 | + |
| 78 | + assert cmap.monochrome is expected |
| 79 | + |
| 80 | + |
10 | 81 | @pytest.fixture(autouse=True) |
11 | 82 | def setup_teardown(): |
12 | 83 | """ |
|
0 commit comments