def sky(width, height, canvas, gs): def rcolor(): return hsv2rgb(.55 + randneghalf2half() * .25, .6 + randneghalf2half() * .125, .25 + randneghalf2half() * .125) transform = agg.Transform() starpaint = agg.SolidPaint(*hsv2rgb(.165, .96, .99)) starshape = agg.Path() angles = np.linspace(0, 2 * np.pi, num=5, endpoint=False) pts = np.stack([np.cos(angles), np.sin(angles)]).T * 7 starshape.lines([pts[i] for i in (0, 2, 4, 1, 3)]) starshape.close() bgshape = agg.Path() bgshape.rect(0, 0, width, height) bgpaint = agg.LinearGradientPaint(0, 0, 0, height, randgradstops(2, rcolor), agg.GradientSpread.SpreadReflect, agg.GradientUnits.UserSpace) canvas.draw_shape(bgshape, transform, gs, fill=bgpaint) star_count = 40 xs = np.random.rand(star_count) * width ys = np.random.rand(star_count) * height for x, y in zip(xs, ys): transform.reset() transform.translate(x, y) canvas.draw_shape(starshape, transform, gs, fill=starpaint)
def interpret_color(value, context): color = parse_color(value) if color is not None: return agg.SolidPaint(*color) elif value.lower() == 'none': return None elif value.startswith('url'): match = re.match(r'url\(#(?P<label>\w+)\)', value) if match is None: return None obj = context.get(match.group('label')) if obj is None or 'type' not in obj: return None if obj['type'] == 'linearGradient': grad = agg.LinearGradientPaint( obj['x1'], obj['y1'], obj['x2'], obj['y2'], obj['stops'], obj.get('spreadMethod', agg.GradientSpread.SpreadPad), obj.get('gradientUnits', agg.GradientUnits.ObjectBoundingBox)) grad.transform = obj.get('gradientTransform', agg.Transform()) return grad if obj['type'] == 'radialGradient': grad = agg.RadialGradientPaint( obj['cx'], obj['cy'], obj['r'], obj['fx'], obj['fy'], obj['stops'], obj.get('spreadMethod', agg.GradientSpread.SpreadPad), obj.get('gradientUnits', agg.GradientUnits.ObjectBoundingBox)) grad.transform = obj.get('gradientTransform', agg.Transform()) return grad
def linear_gradient(self, x1, y1, x2, y2, stops, spread_method, units='userSpaceOnUse'): """ Sets a linear gradient as the current brush. """ spread, units = self._get_gradient_enums(spread_method, units) self.fill_paint = agg.LinearGradientPaint( x1, y1, x2, y2, stops, spread, units )
def draw_horizon(xs, ys, canvas, gs): def rcolor(): return hsv2rgb(.125 + randneghalf2half() * .25, .6 + randneghalf2half() * .4, .6 + randneghalf2half() * .4) width = xs[-1] path = agg.Path() path.move_to(0, 0) path.line_to(xs[0], ys[0]) for x, y in zip(xs[1:], ys[1:]): path.line_to(x, y) path.line_to(width, 0) path.close() transform = agg.Transform() fill = agg.LinearGradientPaint(0, 0, width, 0, randgradstops(2, rcolor), agg.GradientSpread.SpreadReflect, agg.GradientUnits.UserSpace) canvas.draw_shape(path, transform, gs, fill=fill)
star_transform.translate(-SIZE / 2, -SIZE / 2) big_box = agg.Path() big_box.rect(0, 0, SIZE, SIZE) small_box = agg.Path() small_box.rect(0, 0, SIZE / 2, SIZE / 2) stencil_canvas = agg.CanvasG8(np.zeros((SIZE, SIZE), dtype=np.uint8)) canvas = agg.CanvasRGB24(np.zeros((SIZE, SIZE, 3), dtype=np.uint8)) gs = agg.GraphicsState(drawing_mode=agg.DrawingMode.DrawFill, line_width=6.0) transform = agg.Transform() blue_paint = agg.SolidPaint(0.1, 0.1, 1.0) white_paint = agg.SolidPaint(1.0, 1.0, 1.0) stops = ((0.0, 0.0, 0.0, 0.0, 1.0), (1.0, 0.3, 0.3, 0.75, 1.0)) bw_grad = agg.LinearGradientPaint(0, 0, 2, 5, stops, agg.GradientSpread.SpreadReflect, agg.GradientUnits.UserSpace) stops = ((0.0, 1.0, 1.0, 1.0, 1.0), (1.0, 0.7, 0.7, 0.25, 1.0)) lin_grad = agg.LinearGradientPaint(0, 0, 2, 5, stops, agg.GradientSpread.SpreadReflect, agg.GradientUnits.UserSpace) stencil_canvas.clear(0, 0, 0) stencil_canvas.draw_shape(star_shape, star_transform, gs, fill=white_paint) canvas.clear(1, 1, 1) canvas.draw_shape(big_box, transform, gs, fill=bw_grad) gs.stencil = stencil_canvas.image canvas.draw_shape(star_shape, star_transform, gs, fill=lin_grad) gs.drawing_mode = agg.DrawingMode.DrawStroke