8000 When drawing markers, don't set the GraphicsContext alpha. · matplotlib/matplotlib@15b8aa1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15b8aa1

Browse files
committed
When drawing markers, don't set the GraphicsContext alpha.
Setting the alpha value on the GraphicsContext forcefully applies it to both the edgecolor and the facecolor, *except* that a facecolor of `None` ignores the alpha. Instead of requiring this behavior (which needlessly complicates the implementation of new rendering backends), the alpha value (if any) can directly be preapplied to the edgecolor and facecolor, the edgecolor can be applied using gc.set_foreground, and the alpha value never set on the GraphicsContext.
1 parent 41a81c0 commit 15b8aa1

File tree

6 files changed

+667
-638
lines changed

6 files changed

+667
-638
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,10 @@ def draw_markers(
536536
ps_cmd = ['/o {', 'gsave', 'newpath', 'translate'] # don't want the translate to be global
537537

538538
lw = gc.get_linewidth()
539-
stroke = lw != 0.0
539+
alpha = (gc.get_alpha()
540+
if gc.get_forced_alpha() or len(gc.get_rgb()) == 3
541+
else gc.get_rgb()[3])
542+
stroke = lw > 0 and alpha > 0
540543
if stroke:
541544
ps_cmd.append('%.1f setlinewidth' % lw)
542545
jint = gc.get_joinstyle()

lib/matplotlib/lines.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,8 @@ def draw(self, renderer):
759759
gc = renderer.new_gc()
760760
self._set_gc_clip(gc)
761761

762-
ln_color_rgba = self._get_rgba_ln_color()
763-
gc.set_foreground(ln_color_rgba, isRGBA=True)
764-
gc.set_alpha(ln_color_rgba[3])
762+
lc_rgba = mcolors.to_rgba(self._color, self._alpha)
763+
gc.set_foreground(lc_rgba, isRGBA=True)
765764

766765
gc.set_antialiased(self._antialiased)
767766
gc.set_linewidth(self._linewidth)
@@ -785,24 +784,23 @@ def draw(self, renderer):
785784
if self._marker and self._markersize > 0:
786785
gc = renderer.new_gc()
787786
self._set_gc_clip(gc)
788-
rgbaFace = self._get_rgba_face()
789-
rgbaFaceAlt = self._get_rgba_face(alt=True)
790-
edgecolor = self.get_markeredgecolor()
791-
if cbook._str_lower_equal(edgecolor, "none"):
792-
gc.set_linewidth(0)
793-
gc.set_foreground(rgbaFace, isRGBA=True)
794-
else:
795-
gc.set_foreground(edgecolor)
796-
gc.set_linewidth(self._markeredgewidth)
797-
mec = self._markeredgecolor
798-
if (cbook._str_equal(mec, "auto")
799-
and not cbook._str_lower_equal(
800-
self.get_markerfacecolor(), "none")):
801-
gc.set_alpha(rgbaFace[3])
802-
else:
803-
gc.set_alpha(self.get_alpha())
787+
gc.set_linewidth(self._markeredgewidth)
804788
gc.set_antialiased(self._antialiased)
805789

790+
ec_rgba = mcolors.to_rgba(
791+
self.get_markeredgecolor(), self._alpha)
792+
fc_rgba = mcolors.to_rgba(
793+
self._get_markerfacecolor(), self._alpha)
794+
fcalt_rgba = mcolors.to_rgba(
795+
self._get_markerfacecolor(alt=True), self._alpha)
796+
# If the edgecolor is "auto", it is set according to the *line*
797+
# color but inherits the alpha value of the *face* color, if any.
798+
if (cbook._str_equal(self._markeredgecolor, "auto")
799+
and not cbook._str_lower_equal(
800+
self.get_markerfacecolor(), "none")):
801+
ec_rgba = ec_rgba[:3] + (fc_rgba[3],)
802+
gc.set_foreground(ec_rgba, isRGBA=True)
803+
806804
marker = self._marker
807805
tpath, affine = transf_path.get_transformed_points_and_affine()
808806
if len(tpath.vertices):
@@ -832,22 +830,15 @@ def draw(self, renderer):
832830

833831
renderer.draw_markers(gc, marker_path, marker_trans,
834832
subsampled, affine.frozen(),
835-
rgbaFace)
833+
fc_rgba)
836834

837835
alt_marker_path = marker.get_alt_path()
838836
if alt_marker_path:
839837
alt_marker_trans = marker.get_alt_transform()
840838
alt_marker_trans = alt_marker_trans.scale(w)
841-
if (cbook._str_equal(mec, "auto")
842-
and not cbook._str_lower_equal(
843-
self.get_markerfacecoloralt(), "none")):
844-
gc.set_alpha(rgbaFaceAlt[3])
845-
else:
846-
gc.set_alpha(self.get_alpha())
847-
848839
renderer.draw_markers(
849840
gc, alt_marker_path, alt_marker_trans, subsampled,
850-
affine.frozen(), rgbaFaceAlt)
841+
affine.frozen(), fcalt_rgba)
851842

852843
gc.restore()
853844

@@ -892,8 +883,7 @@ def _get_markerfacecolor(self, alt=False):
892883
fc = self._markerfacecoloralt
893884
else:
894885
fc = self._markerfacecolor
895-
896-
if (isinstance(fc, six.string_types) and fc.lower() == 'auto'):
886+
if cbook._str_lower_equal(fc, 'auto'):
897887
if self.get_fillstyle() == 'none':
898888
return 'none'
899889
else:

0 commit comments

Comments
 (0)
0