Skip to content

Commit 038306a

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 038306a

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

lib/matplotlib/backends/backend_ps.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,13 @@ def draw_path(self, gc, path, transform, rgbFace=None):
495495
"""
496496
Draws a Path instance using the given affine transform.
497497
"""
498-
clip = (rgbFace is None and gc.get_hatch_path() is None)
498+
clip = rgbFace is None and gc.get_hatch_path() is None
499499
simplify = path.should_simplify and clip
500-
ps = self._convert_path(
501-
path, transform, clip=clip, simplify=simplify)
500+
ps = self._convert_path(path, transform, clip=clip, simplify=simplify)
502501
self._draw_ps(ps, gc, rgbFace)
503502

504-
def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
503+
def draw_markers(
504+
self, gc, marker_path, marker_trans, path, trans, rgbFace=None):
505505
"""
506506
Draw the markers defined by path at each of the positions in x
507507
and y. path coordinates are points, x and y coords will be
@@ -510,7 +510,9 @@ def draw_markers(self, gc, marker_path, marker_trans, path, trans, rgbFace=None)
510510
if debugPS: self._pswriter.write('% draw_markers \n')
511511

512512
if rgbFace:
513-
if rgbFace[0]==rgbFace[1] and rgbFace[0]==rgbFace[2]:
513+
if len(rgbFace) == 4 and rgbFace[3] == 0:
514+
return
515+
if rgbFace[0] == rgbFace[1] == rgbFace[2]:
514516
ps_color = '%1.3f setgray' % rgbFace[0]
515517
else:
516518
ps_color = '%1.3f %1.3f %1.3f setrgbcolor' % rgbFace[:3]

lib/matplotlib/cbook/__init__.py

+16
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 == 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

+10-16
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)