Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 764227f

Browse files
committed
Warn on attempts at semi-transparent outputs in ps backend.
The postscript format does not support transparency at all; the postscript backend currently silently ignores the alpha channel (except if alpha = 0 in which case the artist is not drawn). Log a warning when this happens so that the user knows what happened. Small refactor of transparency handling in the backend.
1 parent 1a89250 commit 764227f

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Deprecations
2+
````````````
3+
``GraphicsContextPS.shouldstroke`` is deprecated.

lib/matplotlib/backends/backend_ps.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,12 @@ def draw_markers(
420420
if debugPS:
421421
self._pswriter.write('% draw_markers \n')
422422

423-
if rgbFace:
424-
if len(rgbFace) == 4 and rgbFace[3] == 0:
425-
ps_color = None
426-
else:
427-
if rgbFace[0] == rgbFace[1] == rgbFace[2]:
428-
ps_color = '%1.3f setgray' % rgbFace[0]
429-
else:
430-
ps_color = '%1.3f %1.3f %1.3f setrgbcolor' % rgbFace[:3]
423+
ps_color = (
424+
None
425+
if _is_transparent(rgbFace)
426+
else '%1.3f setgray' % rgbFace[0]
427+
if rgbFace[0] == rgbFace[1] == rgbFace[2]
428+
else '%1.3f %1.3f %1.3f setrgbcolor' % rgbFace[:3])
431429

432430
# construct the generic marker command:
433431

@@ -562,7 +560,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
562560
if debugPS:
563561
write("% text\n")
564562

565-
if len(gc.get_rgb()) == 4 and gc.get_rgb()[3] == 0:
563+
if _is_transparent(gc.get_rgb()):
566564
return # Special handling for fully transparent.
567565

568566
if ismath == 'TeX':
@@ -744,10 +742,12 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
744742
write = self._pswriter.write
745743
if debugPS and command:
746744
write("% "+command+"\n")
747-
mightstroke = gc.shouldstroke()
748-
stroke = stroke and mightstroke
749-
fill = (fill and rgbFace is not None and
750-
(len(rgbFace) <= 3 or rgbFace[3] != 0.0))
745+
mightstroke = (gc.get_linewidth() > 0
746+
and not _is_transparent(gc.get_rgb()))
747+
if not mightstroke:
748+
stroke = False
749+
if _is_transparent(rgbFace):
750+
fill = False
751751
hatch = gc.get_hatch()
752752

753753
if mightstroke:
@@ -793,6 +793,21 @@ def _draw_ps(self, ps, gc, rgbFace, fill=True, stroke=True, command=None):
793793
write("grestore\n")
794794

795795

796+
def _is_transparent(rgb_or_rgba):
797+
if rgb_or_rgba is None:
798+
return True # Consistent with rgbFace semantics.
799+
elif len(rgb_or_rgba) == 4:
800+
if rgb_or_rgba[3] == 0:
801+
return True
802+
if rgb_or_rgba[3] != 1:
803+
_log.warning(
804+
"The PostScript backend does not support transparency; "
805+
"partially transparent artists will be rendered opaque.")
806+
return False
807+
else: # len() == 3.
808+
return False
809+
810+
796811
class GraphicsContextPS(GraphicsContextBase):
797812
def get_capstyle(self):
798813
return {'butt': 0, 'round': 1, 'projecting': 2}[
@@ -802,6 +817,7 @@ def get_joinstyle(self):
802817
return {'miter': 0, 'round': 1, 'bevel': 2}[
803818
GraphicsContextBase.get_joinstyle(self)]
804819

820+
@cbook.deprecated("3.1")
805821
def shouldstroke(self):
806822
return (self.get_linewidth() > 0.0 and
807823
(len(self.get_rgb()) <= 3 or self.get_rgb()[3] != 0.0))

0 commit comments

Comments
 (0)