10000 Colormap alpha by efiring · Pull Request #660 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Colormap alpha #660

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 7 commits into from
Jan 8, 2012
Merged

Colormap alpha #660

merged 7 commits into from
Jan 8, 2012

Conversation

efiring
Copy link
Member
@efiring efiring commented Jan 6, 2012

Expands LinearSegmentedColormap and ListedColormap to support full RGBA. This was prompted by a mailing list thread: http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg22983.html, continued with: http://www.mail-archive.com/matplotlib-devel@lists.sourceforge.net/msg08808.html. It is close to a patch proposed by @ivanov a couple years ago. A changeset by @mdboom is included to fix a bug in images with alpha less than 1.

@@ -200,6 +200,8 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
else:
if self._rgbacache is None:
x = self.to_rgba(self._A, bytes=True)
# premultiply the colors
x[...,0:3] *= x[...,3:4] / 255.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line gave me an error, actually:

TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'  

I had to change it to

x[...,0:3] = np.uint8(x[...,0:3] * (x[...,3:4] / 255.0))

But I'm not sure if x is guaranteed to be uint8.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With "bytes=True" in the line above, it is guaranteed to be uint8.
It looks like what we might want to do here, though, is leave "bytes=False", premultiply, and then convert to uint8. Or that could all be done inside to_rgba, controlled by another kwarg. (to_rgba is already a little bit hard to follow, though.)
What numpy version are you using? Mike's version worked on mine (1.6.2.dev-396dbb9).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm running github master ('2.0.0.dev-b817938').

I'm not quite sure what you have in mind. The following wouldn't work

x = self.to_rgba(self._A)
x[...,0:3] *= x[...,3:4] / 255.0
x = np.uint8(x)

because x is not guaranteed to be float (right?). So would the alternative be something like

x = self.to_rgba(self._A)
rgb = x[...,0:3] * (x[...,3:4] / 255.0)
x = np.uint8(np.hstack(rgb, x[...,3]))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see in your latest commits, you make sure that to_rgba returns normalized float values. Just confirm, this now works perfectly on my system.

Previously, it was possible for the bytes kwarg to be False,
but the returned value to still be in bytes form (if the
input x was in bytes).  Now bytes=False guarantees the output
will be floating point.
The earlier version of the premultiplication step did not work
with numpy 2, and required additional dtype conversion steps in
the common case of a colormapped image.
efiring added a commit that referenced this pull request Jan 8, 2012
@efiring efiring merged commit 912ea58 into matplotlib:master Jan 8, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0