8000 Make _get_rgba_face actually always return a RGBA. · matplotlib/matplotlib@8743dce · GitHub
[go: up one dir, main page]

Skip to content

Commit 8743dce

Browse files
committed
Make _get_rgba_face actually always return a RGBA.
_get_rgba_face is an internal function that is only use to set the graphicscontext's foreground color (in Line2D.draw); it is always effectively called as `gc.set_foreground(line._get_rgba_face(...), isRGBA=True)`. So it makes sense to have it actually always return a RGBA quadruplet, including when the color is "none" (in which case `mcolors.to_rgba` returns (0, 0, 0, 0) regardless of alpha, which works just fine). This removes the need for third party graphicscontexts to handle non-RGBA-quadruplets (specifically, None) even when set_foreground is actually called with isRGBA=True.
1 parent 0ddae6e commit 8743dce

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2766,3 +2766,19 @@ def _topmost_artist(
27662766
in reverse order.
27672767
"""
27682768
return _cached_max(reversed(artists))
2769+
2770+
2771+
def _str_equal(obj, s):
2772+
"""Return whether *obj* is a string equal to string *s*.
2773+
2774+
This helper solely exists to handle the case where *obj* is a numpy array.
2775+
"""
2776+
return isinstance(obj, six.string_types) and obj.lower() == s
2777+
2778+
2779+
def _str_lower_equal(obj, s):
2780+
"""Return whether *obj* is a string equal, when lowercased, to string *s*.
2781+
2782+
This helper solely exists to handle the case where *obj* is a numpy array.
2783+
"""
2784+
return isinstance(obj, six.string_types) and obj.lower() == s

lib/matplotlib/lines.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import numpy as np
1515

16-
from . import artist, colors as mcolors, docstring, rcParams
16+
from . import artist, cbook, colors as mcolors, docstring, rcParams
1717
from .artist import Artist, allow_rasterization
1818
from .cbook import (
1919
_to_unmasked_float_array, iterable, is_numlike, ls_mapper, ls_mapper_r,
@@ -793,16 +793,16 @@ def draw(self, renderer):
793793
rgbaFace = self._get_rgba_face()
794794
rgbaFaceAlt = self._get_rgba_face(alt=True)
795795
edgecolor = self.get_markeredgecolor()
796-
if (isinstance(edgecolor, six.string_types)
797-
and edgecolor.lower() == 'none'):
796+
if cbook._str_lower_equal(edgecolor, "none"):
798797
gc.set_linewidth(0)
799798
gc.set_foreground(rgbaFace, isRGBA=True)
800799
else:
801800
gc.set_foreground(edgecolor)
802801
gc.set_linewidth(self._markeredgewidth)
803802
mec = self._markeredgecolor
804-
if (isinstance(mec, six.string_types) and mec == 'auto' and
805-
rgbaFace is not None):
803+
if (cbook._str_lower_equal(mec, "auto")
804+
and not cbook._str_lower_equal(
805+
self.get_markerfacecolor(), "none")):
806806
gc.set_alpha(rgbaFace[3])
807807
else:
808808
gc.set_alpha(self.get_alpha())
@@ -828,8 +828,7 @@ def draw(self, renderer):
828828
marker_trans = marker.get_transform()
829829
w = renderer.points_to_pixels(self._markersize)
830830

831-
if (isinstance(marker.get_marker(), six.string_types) and
832-
marker.get_marker() == ','):
831+
if cbook._str_equal(marker.get_marker(), ","):
833832
gc.set_linewidth(0)
834833
else:
835834
# Don't scale for pixels, and don't stroke them
@@ -843,8 +842,9 @@ def draw(self, renderer):
843842
if alt_marker_path:
844843
alt_marker_trans = marker.get_alt_transform()
845844
alt_marker_trans = alt_marker_trans.scale(w)
846-
if (isinstance(mec, six.string_types) and mec == 'auto' and
847-
rgbaFaceAlt is not None):
845+
if (cbook._str_lower_equal(mec, "auto")
846+
and not cbook._str_lower_equal(
847+
self.get_markerfacecoloralt(), "none")):
848848
gc.set_alpha(rgbaFaceAlt[3])
849849
else:
850850
gc.set_alpha(self.get_alpha())
@@ -1257,13 +1257,7 @@ def update_from(self, other):
12571257
self._drawstyle = other._drawstyle
12581258

12591259
def _get_rgba_face(self, alt=False):
1260-
facecolor = self._get_markerfacecolor(alt=alt)
1261-
if (isinstance(facecolor, six.string_types)
1262-
and facecolor.lower() == 'none'):
1263-
rgbaFace = None
1264-
else:
1265-
rgbaFace = mcolors.to_rgba(facecolor, self._alpha)
1266-
return rgbaFace
1260+
return mcolors.to_rgba(self._get_markerfacecolor(alt=alt), self._alpha)
12671261

12681262
def _get_rgba_ln_color(self, alt=False):
12691263
return mcolors.to_rgba(self._color, self._alpha)

0 commit comments

Comments
 (0)
0