Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/matplotlib/backends/backend_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,25 @@ def close_blend_group(self):
ctx.save()
self.gc.set_blend_mode(group_state.blend_mode)
ctx.set_source(group)
if group_state.alpha != 1:
ctx.paint_with_alpha(group_state.alpha)

# For these two Porter-Duff compositing operators, we need to mask out fully
# transparent pixels, otherwise those pixels will override the backdrop
if group_state.blend_mode in {'knockout', 'clear'}:
# Extract the 8-bit alpha channel
mask_surface = ctx.get_target().create_similar_image(
cairo.FORMAT_A8, self.width, self.height)
mask_ctx = cairo.Context(mask_surface)
mask_ctx.set_source(group)
mask_ctx.paint()
mask_surface.flush()

# Pixels that are not fully transparent are painted with the group alpha
mask_buf = np.asarray(mask_surface.get_data())
mask_buf[mask_buf > 0] = 255 * group_state.alpha + 0.5 # round to int
ctx.mask_surface(mask_surface)
else:
ctx.paint()
ctx.paint_with_alpha(group_state.alpha)

ctx.restore()


Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 15 additions & 3 deletions lib/matplotlib/tests/test_backends_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def draw(self, renderer):
renderer.close_blend_group()


def plot_blend_group_types(rasterize=False):
def plot_blend_group_types(rasterize=False, porterduff=False):
# Rows: top row is non-isolated, bottom row is isolated
# Columns: left column is non-knockout, right column is knockout
fig, axs = plt.subplots(2, 2, figsize=(3, 3), dpi=80, layout='constrained')
Expand Down Expand Up @@ -129,6 +129,18 @@ def plot_blend_group_types(rasterize=False):
gray = Circle((0, 0), 0.1, fc='gray', zorder=0)
axs[i, j].add_artist(gray)

# For backends that support the Porter-Duff compositing operators, additionally
# test 'knockout' and 'clear' as the blend mode for an isolated group
if porterduff:
for x, group_blend_mode, group_alpha in [(-0.6, 'knockout', 1),
(-0.2, 'knockout', 0.6),
(0.2, 'clear', 0.6),
(0.6, 'clear', 1)]:
circle = Circle((x, -0.8), 0.15, fc='k', alpha=0.75)
group = ArtistGroup([circle], group_blend_mode=group_blend_mode,
group_alpha=group_alpha)
axs[1, 0].add_artist(group)


@image_comparison(['blend_modes_agg.png'], style='mpl20')
def test_blend_modes_agg():
Expand Down Expand Up @@ -184,7 +196,7 @@ def test_blend_groups_agg():
# The top-right panel (knockout but not isolated) is not supported, so will be
# rendered like the top-left panel (neither knockout nor isolated)

plot_blend_group_types()
plot_blend_group_types(porterduff=True)


@pytest.mark.backend('cairo')
Expand All @@ -193,7 +205,7 @@ def test_blend_groups_cairo():
# The top-right panel (knockout but not isolated) is not supported, so will be
# rendered like the top-left panel (neither knockout nor isolated)

plot_blend_group_types()
plot_blend_group_types(porterduff=True)


@image_comparison(['blend_groups_svg.svg'], style='mpl20')
Expand Down
Loading