Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 2 commits into from

2 participants

@efiring
Owner

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

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

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 merged commit 39db8f1 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 17, 2011
  1. @efiring

    graphics context: use alpha value from foreground color if present

    efiring authored
    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.
Commits on Aug 19, 2011
  1. @efiring

    gc: ensure explicitly set alpha overrides the A in rgba.

    efiring authored
    Prior to this change, calling set_alpha before set_foreground with
    an rgba argument would cause the latter to override the former.
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 9 deletions.
  1. +10 −9 lib/matplotlib/backend_bases.py
View
19 lib/matplotlib/backend_bases.py
@@ -637,6 +637,7 @@ class GraphicsContextBase:
def __init__(self):
self._alpha = 1.0
+ self._forced_alpha = False # if True, _alpha overrides A from RGBA
self._antialiased = 1 # use 0,1 not True, False for extension code
self._capstyle = 'butt'
self._cliprect = None
@@ -742,8 +743,7 @@ def get_linewidth(self):
def get_rgb(self):
"""
- returns a tuple of three floats from 0-1. color can be a
- MATLAB format string, a html hex color string, or a rgb tuple
+ returns a tuple of three or four floats from 0-1.
"""
return self._rgb
@@ -771,9 +771,9 @@ def set_alpha(self, alpha):
Set the alpha value used for blending - not supported on
all backends
"""
- if alpha is None:
- alpha = 1.0
- self._alpha = alpha
+ if alpha is not None:
+ self._alpha = alpha
+ self._forced_alpha = True
def set_antialiased(self, b):
"""
@@ -823,17 +823,18 @@ def set_dashes(self, dash_offset, dash_list):
def set_foreground(self, fg, isRGB=False):
"""
Set the foreground color. fg can be a MATLAB format string, a
- html hex color string, an rgb unit tuple, or a float between 0
+ html hex color string, an rgb or rgba unit tuple, or a float between 0
and 1. In the latter case, grayscale is used.
- The :class:`GraphicsContextBase` converts colors to rgb
- internally. If you know the color is rgb already, you can set
- ``isRGB=True`` to avoid the performace hit of the conversion
+ If you know fg is rgb or rgba, set ``isRGB=True`` for
+ efficiency.
"""
if isRGB:
self._rgb = fg
else:
self._rgb = colors.colorConverter.to_rgba(fg)
+ if len(self._rgb) == 4 and not self._forced_alpha:
+ self._alpha = self._rgb[3]
def set_graylevel(self, frac):
"""
Something went wrong with that request. Please try again.