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

Skip to content

Commit 1707b1e

Browse files
authored
Merge pull request #10773 from TeamChillUTSC/10419-svg-alpha-text
Consider alpha channel from RGBA color of text for SVG backend text opacity rendering
2 parents 8a27054 + cd9d84f commit 1707b1e

File tree

4 files changed

+176
-4
lines changed

4 files changed

+176
-4
lines changed

lib/matplotlib/backends/backend_svg.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,8 +901,10 @@ def _draw_text_as_path(self, gc, x, y, s, prop, angle, ismath, mtext=None):
901901
style = {}
902902
if color != '#000000':
903903
style['fill'] = color
904-
if gc.get_alpha() != 1.0:
905-
style['opacity'] = short_float_fmt(gc.get_alpha())
904+
905+
alpha = gc.get_alpha() if gc.get_forced_alpha() else gc.get_rgb()[3]
906+
if alpha != 1:
907+
style['opacity'] = short_float_fmt(alpha)
906908

907909
if not ismath:
908910
font = text2path._get_font(prop)
@@ -1002,8 +1004,10 @@ def _draw_text_as_text(self, gc, x, y, s, prop, angle, ismath, mtext=None):
10021004
style = {}
10031005
if color != '#000000':
10041006
style['fill'] = color
1005-
if gc.get_alpha() != 1.0:
1006-
style['opacity'] = short_float_fmt(gc.get_alpha())
1007+
1008+
alpha = gc.get_alpha() if gc.get_forced_alpha() else gc.get_rgb()[3]
1009+
if alpha != 1:
1010+
style['opacity'] = short_float_fmt(alpha)
10071011

10081012
if not ismath:
10091013
font = self._get_font(prop)
Lines changed: 114 additions & 0 deletions
Loading
Lines changed: 32 additions & 0 deletions
Loading

lib/matplotlib/tests/test_text.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,25 @@ def test_single_artist_usetex():
474474
fig, ax = plt.subplots()
475475
ax.text(.5, .5, r"$\frac12$", usetex=True)
476476
fig.canvas.draw()
477+
478+
479+
@image_comparison(baseline_images=['text_as_path_opacity'],
480+
extensions=['svg'])
481+
def test_text_as_path_opacity():
482+
plt.figure()
483+
plt.gca().set_axis_off()
484+
plt.text(0.25, 0.25, 'c', color=(0, 0, 0, 0.5))
485+
plt.text(0.25, 0.5, 'a', alpha=0.5)
486+
plt.text(0.25, 0.75, 'x', alpha=0.5, color=(0, 0, 0, 1))
487+
488+
489+
@image_comparison(baseline_images=['text_as_text_opacity'],
490+
extensions=['svg'])
491+
def test_text_as_text_opacity():
492+
matplotlib.rcParams['svg.fonttype'] = 'none'
493+
plt.figure()
494+
plt.gca().set_axis_off()
495+
plt.text(0.25, 0.25, '50% using `color`', color=(0, 0, 0, 0.5))
496+
plt.text(0.25, 0.5, '50% using `alpha`', alpha=0.5)
497+
plt.text(0.25, 0.75, '50% using `alpha` and 100% `color`', alpha=0.5,
498+
color=(0, 0, 0, 1))

0 commit comments

Comments
 (0)