Skip to content

Simplify Colormap.__call__ a bit. #21301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,24 +633,20 @@ def __call__(self, X, alpha=None, bytes=False):
xa[xa < 0] = self._i_under
xa[mask_bad] = self._i_bad

lut = self._lut
if bytes:
lut = (self._lut * 255).astype(np.uint8)
else:
lut = self._lut.copy() # Don't let alpha modify original _lut.
lut = (lut * 255).astype(np.uint8)

rgba = np.empty(shape=xa.shape + (4,), dtype=lut.dtype)
lut.take(xa, axis=0, mode='clip', out=rgba)
rgba = lut.take(xa, axis=0, mode='clip')

if alpha is not None:
if np.iterable(alpha):
alpha = np.asarray(alpha)
if alpha.shape != xa.shape:
raise ValueError("alpha is array-like but its shape"
" %s doesn't match that of X %s" %
(alpha.shape, xa.shape))
alpha = np.clip(alpha, 0, 1)
if bytes:
alpha = (alpha * 255).astype(np.uint8)
alpha *= 255 # Will be cast to uint8 upon assignment.
if alpha.shape not in [(), xa.shape]:
raise ValueError(
f"alpha is array-like but its shape {alpha.shape} does "
f"not match that of X {xa.shape}")
rgba[..., -1] = alpha

# If the "bad" color is all zeros, then ignore alpha input.
Expand Down