graphics context: use alpha value from foreground color if present #423

Merged
merged 2 commits into from Aug 20, 2011

Conversation

Projects
None yet
2 participants
Owner

efiring commented Aug 17, 2011

When a Line2D color is given as an rgba, this causes the "a" part
to be used unless it is explicitly overridden by an alpha kwarg.

It is in response to
http://www.mail-archive.com/matplotlib-users@lists.sourceforge.net/msg21706.html

@efiring efiring graphics context: use alpha value from foreground color if present
When a Line2D color is given as an rgba, this causes the "a" part
to be used unless it is explicitly overriden by an alpha kwarg.
4c078dd

vbr commented on 4c078dd Aug 17, 2011

Just a comment, as I unfortunately don't have experiences with the workflow using github etc., and since I only started to use matplotlib in a rather simple way...

Apart from some naming inconsistence (rgb meaning effectively rgba sometimes), it seems to me, that colors.colorConverter.to_rgba(...) always returns a 4-tuple, _rgb would become 4-tuple in this code, or am I missing something? Can the rest of the code handle _rgb being rgba?
Could possibly adding a line to slice back to 3-tuple work?

if isRGB:
self._rgb = fg
else:
self._rgb = colors.colorConverter.to_rgba(fg)
if len(self._rgb) == 4:
self._alpha = self._rgb[3]
self._rgb = self._rgb[:3]

I don't know, how frequently this function is used in the code and whether the additional checks and tuple-slicing and len(...) might be a bottleneck.

It should be decided, how alpha is applied in case of collisions:
with isRGB, i believe it is here used only if a 4-tuple rgba is passed, however, with isRGB == False the possibly(?) previously set _alpha would be reset to 1.0; If this scenario is possible (i.e. _alpha and _rgb can be set independently), some further tests would be necessary, but I don't know, how to decide whether alpha=1.0 was contained in the color passed or was added as default by colors.colorConverter.to_rgba(fg).
Regards,
Vlastimil Brom

Owner

efiring replied Aug 17, 2011

The rest of the code can handle _rgb as rgba, but I agree it is confusing. This is somewhat inherent, however, since there are many places where a color tuple can be either rgb or rgba.
The GC is a low-level object, created internally when drawing and then discarded. _alpha is initialized to 1. When it is set to another value, this is normally done after setting the the foreground color, and my change relies on this to establish the priority. This is fragile; I have an idea for making it less so. A more thorough overhaul of color handling may also be in order.

@efiring efiring gc: ensure explicitly set alpha overrides the A in rgba.
Prior to this change, calling set_alpha before set_foreground with
an rgba argument would cause the latter to override the former.
356c996

@efiring efiring added a commit that referenced this pull request Aug 20, 2011

@efiring efiring Merge pull request #423 from efiring/rgba_alpha
graphics context: use alpha value from foreground color if present
39db8f1

@efiring efiring merged commit 39db8f1 into matplotlib:master Aug 20, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment