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

Skip to content

Cairo backend: Fix alpha render of collections #12774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2018
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
51 changes: 0 additions & 51 deletions lib/matplotlib/backends/backend_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,57 +286,6 @@ def draw_markers(self, gc, marker_path, marker_trans, path, transform,
self._fill_and_stroke(
ctx, rgbFace, gc.get_alpha(), gc.get_forced_alpha())

def draw_path_collection(
self, gc, master_transform, paths, all_transforms, offsets,
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):

path_ids = [
(path, Affine2D(transform))
for path, transform in self._iter_collection_raw_paths(
master_transform, paths, all_transforms)]

reuse_key = None
grouped_draw = []

def _draw_paths():
if not grouped_draw:
return
gc_vars, rgb_fc = reuse_key
gc = copy.copy(gc0)
# We actually need to call the setters to reset the internal state.
vars(gc).update(gc_vars)
for k, v in gc_vars.items():
if k == "_linestyle": # Deprecated, no effect.
continue
try:
getattr(gc, "set" + k)(v)
except (AttributeError, TypeError) as e:
pass
gc.ctx.new_path()
paths, transforms = zip(*grouped_draw)
grouped_draw.clear()
_append_paths(gc.ctx, paths, transforms)
self._fill_and_stroke(
gc.ctx, rgb_fc, gc.get_alpha(), gc.get_forced_alpha())

for xo, yo, path_id, gc0, rgb_fc in self._iter_collection(
gc, master_transform, all_transforms, path_ids, offsets,
offsetTrans, facecolors, edgecolors, linewidths, linestyles,
antialiaseds, urls, offset_position):
path, transform = path_id
transform = (Affine2D(transform.get_matrix())
.translate(xo, yo - self.height).scale(1, -1))
# rgb_fc could be a ndarray, for which equality is elementwise.
new_key = vars(gc0), tuple(rgb_fc) if rgb_fc is not None else None
if new_key == reuse_key:
grouped_draw.append((path, transform))
else:
_draw_paths()
grouped_draw.append((path, transform))
reuse_key = new_key
_draw_paths()

def draw_image(self, gc, x, y, im):
im = cbook._unmultiplied_rgba8888_to_premultiplied_argb32(im[::-1])
surface = cairo.ImageSurface.create_for_data(
Expand Down
55 changes: 55 additions & 0 deletions lib/matplotlib/tests/test_backend_cairo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import numpy as np
from io import BytesIO
import os
import tempfile
import warnings
import xml.parsers.expat

import pytest

import matplotlib.pyplot as plt
from matplotlib.testing.decorators import check_figures_equal
import matplotlib
from matplotlib import (
collections as mcollections, patches as mpatches, path as mpath)


@pytest.mark.backend('cairo')
@check_figures_equal(extensions=["png"])
def test_patch_alpha_coloring(fig_test, fig_ref):
"""
Test checks that the patch and collection are rendered with the specified
alpha values in their facecolor and edgecolor.
"""
star = mpath.Path.unit_regular_star(6)
circle = mpath.Path.unit_circle()
# concatenate the star with an internal cutout of the circle
verts = np.concatenate([circle.vertices, star.vertices[::-1]])
codes = np.concatenate([circle.codes, star.codes])
cut_star1 = mpath.Path(verts, codes)
cut_star2 = mpath.Path(verts + 1, codes)

# Reference: two separate patches
ax = fig_ref.subplots()
ax.set_xlim([-1, 2])
ax.set_ylim([-1, 2])
patch = mpatches.PathPatch(cut_star1,
linewidth=5, linestyle='dashdot',
facecolor=(1, 0, 0, 0.5),
edgecolor=(0, 0, 1, 0.75))
ax.add_patch(patch)
patch = mpatches.PathPatch(cut_star2,
linewidth=5, linestyle='dashdot',
facecolor=(1, 0, 0, 0.5),
edgecolor=(0, 0, 1, 0.75))
ax.add_patch(patch)

# Test: path collection
ax = fig_test.subplots()
ax.set_xlim([-1, 2])
ax.set_ylim([-1, 2])
col = mcollections.PathCollection([cut_star1, cut_star2],
linewidth=5, linestyles='dashdot',
facecolor=(1, 0, 0, 0.5),
edgecolor=(0, 0, 1, 0.75))
ax.add_collection(col)