| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 6036642 commit 04baaf7
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -152,7 +152,7 @@ def to_rgba(c, alpha=None): | |||
| 152 | 152 | ||
| 153 | 153 | Parameters | |
| 154 | 154 | ---------- | |
| 155 | - c : Matplotlib color | ||
| 155 | + c : Matplotlib color or ``np.ma.masked`` | ||
| 156 | 156 | ||
| 157 | 157 | alpha : scalar, optional | |
| 158 | 158 | If *alpha* is not ``None``, it forces the alpha value, except if *c* is | |
@@ -189,6 +189,8 @@ def _to_rgba_no_colorcycle(c, alpha=None): | |||
| 189 | 189 | ``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``. | |
| 190 | 190 | """ | |
| 191 | 191 | orig_c = c | |
| 192 | + if c is np.ma.masked: | ||
| 193 | + return (0., 0., 0., 0.) | ||
| 192 | 194 | if isinstance(c, str): | |
| 193 | 195 | if c.lower() == "none": | |
| 194 | 196 | return (0., 0., 0., 0.) | |
@@ -254,19 +256,25 @@ def to_rgba_array(c, alpha=None): | |||
| 254 | 256 | ||
| 255 | 257 | If *alpha* is not ``None``, it forces the alpha value. If *c* is | |
| 256 | 258 | ``"none"`` (case-insensitive) or an empty list, an empty array is returned. | |
| 259 | + If *c* is a masked array, an ndarray is returned with a (0, 0, 0, 0) | ||
| 260 | + row for each masked value or row in *c*. | ||
| 257 | 261 | """ | |
| 258 | 262 | # Special-case inputs that are already arrays, for performance. (If the | |
| 259 | 263 | # array has the wrong kind or shape, raise the error during one-at-a-time | |
| 260 | 264 | # conversion.) | |
| 261 | 265 | if (isinstance(c, np.ndarray) and c.dtype.kind in "if" | |
| 262 | 266 | and c.ndim == 2 and c.shape[1] in [3, 4]): | |
| 267 | + mask = c.mask.any(axis=1) if np.ma.is_masked(c) else None | ||
| 268 | + c = np.ma.getdata(c) | ||
| 263 | 269 | if c.shape[1] == 3: | |
| 264 | 270 | result = np.column_stack([c, np.zeros(len(c))]) | |
| 265 | 271 | result[:, -1] = alpha if alpha is not None else 1. | |
| 266 | 272 | elif c.shape[1] == 4: | |
| 267 | 273 | result = c.copy() | |
| 268 | 274 | if alpha is not None: | |
| 269 | 275 | result[:, -1] = alpha | |
| 276 | + if mask is not None: | ||
| 277 | + result[mask] = 0 | ||
| 270 | 278 | if np.any((result < 0) | (result > 1)): | |
| 271 | 279 | raise ValueError("RGBA values should be within 0-1 range") | |
| 272 | 280 | return result | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -766,6 +766,33 @@ def test_conversions(): | |||
| 766 | 766 | hex_color | |
| 767 | 767 | ||
| 768 | 768 | ||
| 769 | + def test_conversions_masked(): | ||
| 770 | + x1 = np.ma.array(['k', 'b'], mask=[True, False]) | ||
| 771 | + x2 = np.ma.array([[0, 0, 0, 1], [0, 0, 1, 1]]) | ||
| 772 | + x2[0] = np.ma.masked | ||
| 773 | + assert mcolors.to_rgba(x1[0]) == (0, 0, 0, 0) | ||
| 774 | + assert_array_equal(mcolors.to_rgba_array(x1), | ||
| 775 | + [[0, 0, 0, 0], [0, 0, 1, 1]]) | ||
| 776 | + assert_array_equal(mcolors.to_rgba_array(x2), mcolors.to_rgba_array(x1)) | ||
| 777 | + | ||
| 778 | + | ||
| 779 | + def test_to_rgba_array_single_str(): | ||
| 780 | + # single color name is valid | ||
| 781 | + assert_array_equal(mcolors.to_rgba_array("red"), [(1, 0, 0, 1)]) | ||
| 782 | + | ||
| 783 | + # single char color sequence is deprecated | ||
| 784 | + with pytest.warns(cbook.MatplotlibDeprecationWarning, | ||
| 785 | + match="Using a string of single character colors as a " | ||
| 786 | + "color sequence is deprecated"): | ||
| 787 | + array = mcolors.to_rgba_array("rgb") | ||
| 788 | + assert_array_equal(array, [(1, 0, 0, 1), (0, 0.5, 0, 1), (0, 0, 1, 1)]) | ||
| 789 | + | ||
| 790 | + with pytest.raises(ValueError, | ||
| 791 | + match="neither a valid single color nor a color " | ||
| 792 | + "sequence"): | ||
| 793 | + mcolors.to_rgba_array("rgbx") | ||
| 794 | + | ||
| 795 | + | ||
| 769 | 796 | def test_grey_gray(): | |
| 770 | 797 | color_mapping = mcolors._colors_full_map | |
| 771 | 798 | for k in color_mapping.keys(): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments