Skip to content

Commit 39db8f1

Browse files
committed
Merge pull request #423 from efiring/rgba_alpha
graphics context: use alpha value from foreground color if present
2 parents 763112f + 356c996 commit 39db8f1

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ class GraphicsContextBase:
637637

638638
def __init__(self):
639639
self._alpha = 1.0
640+
self._forced_alpha = False # if True, _alpha overrides A from RGBA
640641
self._antialiased = 1 # use 0,1 not True, False for extension code
641642
self._capstyle = 'butt'
642643
self._cliprect = None
@@ -742,8 +743,7 @@ def get_linewidth(self):
742743

743744
def get_rgb(self):
744745
"""
745-
returns a tuple of three floats from 0-1. color can be a
746-
MATLAB format string, a html hex color string, or a rgb tuple
746+
returns a tuple of three or four floats from 0-1.
747747
"""
748748
return self._rgb
749749

@@ -771,9 +771,9 @@ def set_alpha(self, alpha):
771771
Set the alpha value used for blending - not supported on
772772
all backends
773773
"""
774-
if alpha is None:
775-
alpha = 1.0
776-
self._alpha = alpha
774+
if alpha is not None:
775+
self._alpha = alpha
776+
self._forced_alpha = True
777777

778778
def set_antialiased(self, b):
779779
"""
@@ -823,17 +823,18 @@ def set_dashes(self, dash_offset, dash_list):
823823
def set_foreground(self, fg, isRGB=False):
824824
"""
825825
Set the foreground color. fg can be a MATLAB format string, a
826-
html hex color string, an rgb unit tuple, or a float between 0
826+
html hex color string, an rgb or rgba unit tuple, or a float between 0
827827
and 1. In the latter case, grayscale is used.
828828
829-
The :class:`GraphicsContextBase` converts colors to rgb
830-
internally. If you know the color is rgb already, you can set
831-
``isRGB=True`` to avoid the performace hit of the conversion
829+
If you know fg is rgb or rgba, set ``isRGB=True`` for
830+
efficiency.
832831
"""
833832
if isRGB:
834833
self._rgb = fg
835834
else:
836835
self._rgb = colors.colorConverter.to_rgba(fg)
836+
if len(self._rgb) == 4 and not self._forced_alpha:
837+
self._alpha = self._rgb[3]
837838

838839
def set_graylevel(self, frac):
839840
"""

0 commit comments

Comments
 (0)