| 1 | diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py |
| 2 | --- a/lib/matplotlib/colors.py |
| 3 | +++ b/lib/matplotlib/colors.py |
| 4 | @@ -715,16 +715,17 @@ def __call__(self, X, alpha=None, bytes=False): |
| 5 | if not xa.dtype.isnative: |
| 6 | xa = xa.byteswap().newbyteorder() # Native byteorder is faster. |
| 7 | if xa.dtype.kind == "f": |
| 8 | - with np.errstate(invalid="ignore"): |
| 9 | - xa *= self.N |
| 10 | - # Negative values are out of range, but astype(int) would |
| 11 | - # truncate them towards zero. |
| 12 | - xa[xa < 0] = -1 |
| 13 | - # xa == 1 (== N after multiplication) is not out of range. |
| 14 | - xa[xa == self.N] = self.N - 1 |
| 15 | - # Avoid converting large positive values to negative integers. |
| 16 | - np.clip(xa, -1, self.N, out=xa) |
| 17 | - xa = xa.astype(int) |
| 18 | + xa *= self.N |
| 19 | + # Negative values are out of range, but astype(int) would |
| 20 | + # truncate them towards zero. |
| 21 | + xa[xa < 0] = -1 |
| 22 | + # xa == 1 (== N after multiplication) is not out of range. |
| 23 | + xa[xa == self.N] = self.N - 1 |
| 24 | + # Avoid converting large positive values to negative integers. |
| 25 | + np.clip(xa, -1, self.N, out=xa) |
| 26 | + with np.errstate(invalid="ignore"): |
| 27 | + # We need this cast for unsigned ints as well as floats |
| 28 | + xa = xa.astype(int) |
| 29 | # Set the over-range indices before the under-range; |
| 30 | # otherwise the under-range values get converted to over-range. |
| 31 | xa[xa > self.N - 1] = self._i_over |