Switch agg backend to use pixfmt_rgba32_plain instead of pixfmt_rgba3… · matplotlib/matplotlib@c985196 · GitHub
Skip to content

Commit c985196

Browse files
Westacularpelson
authored andcommitted
Switch agg backend to use pixfmt_rgba32_plain instead of pixfmt_rgba32, which fixes a problem with alpha-blending partially transparent backgrounds.
1 parent d8fce7b commit c985196

8 files changed

Lines changed: 25 additions & 20 deletions

File tree

doc/users/whats_new.rst

Lines changed: 7 additions & 5 deletions
465 Bytes
Loading

lib/matplotlib/tests/baseline_images/test_figure/alpha_background.svg

Lines changed: 2 additions & 2 deletions
Loading

lib/matplotlib/tests/test_agg.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
@cleanup
1717
def test_repeated_save_with_alpha():
18-
# We want an image which has a background color of blue, with an
18+
# We want an image which has a background color of bluish green, with an
1919
# alpha of 0.25.
2020

2121
fig = Figure([1, 0.4])
2222
canvas = FigureCanvas(fig)
23-
fig.set_facecolor((0, 0, 0.4))
23+
fig.set_facecolor((0, 1, 0.4))
2424
fig.patch.set_alpha(0.25)
2525

2626
# The target color is fig.patch.get_facecolor()
@@ -38,9 +38,9 @@ def test_repeated_save_with_alpha():
3838
edgecolor='none')
3939

4040
# Check the first pixel has the desired color & alpha
41-
# (approx: 0, 0, 0.1, 0.25)
41+
# (approx: 0, 1.0, 0.4, 0.25)
4242
assert_array_almost_equal(tuple(imread(img_fname)[0, 0]),
43-
(0.0, 0.0, 0.098, 0.250),
43+
(0.0, 1.0, 0.4, 0.250),
4444
decimal=3)
4545
finally:
4646
os.remove(img_fname)

lib/matplotlib/tests/test_figure.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,21 @@ def test_suptitle():
7575

7676

7777
@image_comparison(baseline_images=['alpha_background'],
78-
extensions=['png', 'svg'], # PDF is wrong
79-
savefig_kwarg={'facecolor': 'black', 'edgecolor': 'none'})
78+
# only test png and svg. The PDF output appears correct,
79+
# but Ghostscript does not preserve the background color.
80+
extensions=['png', 'svg'],
81+
savefig_kwarg={'facecolor': (0, 1, 0.4), 'edgecolor': 'none'})
8082
def test_alpha():
81-
# We want an image which has a background color of black, with an
82-
# alpha of 0.3.
83+
# We want an image which has a background color and an
84+
# alpha of 0.4.
8385
fig = plt.figure(figsize=[2, 1])
84-
fig.set_facecolor('black')
85-
fig.patch.set_alpha(0.3)
86+
fig.set_facecolor((0, 1, 0.4))
87+
fig.patch.set_alpha(0.4)
8688

8789
import matplotlib.patches as mpatches
8890
fig.patches.append(mpatches.CirclePolygon([20, 20],
8991
radius=15,
92+
alpha=0.6,
9093
facecolor='red'))
9194

9295

src/_backend_agg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ RendererAgg::draw_image(const Py::Tuple& args)
11081108
inv_mtx.invert();
11091109

11101110
typedef agg::span_allocator<agg::rgba8> color_span_alloc_type;
1111-
typedef agg::image_accessor_clip<agg::pixfmt_rgba32>
1111+
typedef agg::image_accessor_clip<agg::pixfmt_rgba32_plain>
11121112
image_accessor_type;
11131113
typedef agg::span_interpolator_linear<> interpolator_type;
11141114
typedef agg::span_image_filter_rgba_nn<image_accessor_type,

src/_backend_agg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
const size_t NUM_VERTICES[] = { 1, 1, 1, 2, 3, 1 };
5555

56-
typedef agg::pixfmt_rgba32 pixfmt;
56+
typedef agg::pixfmt_rgba32_plain pixfmt;
5757
typedef agg::renderer_base<pixfmt> renderer_base;
5858
typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_aa;
5959
typedef agg::renderer_scanline_bin_solid<renderer_base> renderer_bin;

src/_image.cpp

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)