Python cairo.LinearGradient() Examples

The following are 11 code examples of cairo.LinearGradient(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cairo , or try the search function .
Example #1
Source File: colorctrls.py    From sk1-wx with GNU General Public License v3.0 6 votes vote down vote up
def draw_gradient(self):
        w, h = self.get_size()
        gradient = self.fill[2]
        surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)
        ctx = cairo.Context(surface)
        self.draw_cairo_background(ctx)
        if gradient[0] == sk2const.GRADIENT_LINEAR:
            grd = cairo.LinearGradient(0.0, h / 2.0, w, h / 2.0)
        else:
            grd = cairo.RadialGradient(w / 2.0, h / 2.0, 0,
                                       w / 2.0, h / 2.0, w / 2.0)
        for stop in gradient[2]:
            grd.add_color_stop_rgba(stop[0], *self.get_cairo_color(stop[1]))
        ctx.set_source(grd)
        ctx.rectangle(0, 0, w, h)
        ctx.fill()
        self.gc_draw_bitmap(wal.copy_surface_to_bitmap(surface), 0, 0) 
Example #2
Source File: tiled_lines.py    From generative-art with MIT License 6 votes vote down vote up
def draw_line(ctx, x, y, width, height, line_width, palette):
    leftToRight = random.random() >= 0.5

    if leftToRight:
        start = (x, y)
        end = (x + width, y + height)
    else:
        start = (x + width, y)
        end = (x, y + height)

    ctx.move_to(start[0], start[1])
    ctx.line_to(end[0], end[1])

    ctx.set_line_width(line_width)
    ctx.set_line_cap(cairo.LineCap.ROUND)

    g = cairo.LinearGradient(start[0], start[1], end[0], end[1])
    colors = [hex_to_tuple(c) for c in random.choices(palette['colors'], k=2)]
    g.add_color_stop_rgb(0, *colors[0])
    g.add_color_stop_rgb(1, *colors[1])
    ctx.set_source(g)
    ctx.stroke() 
Example #3
Source File: color_field.py    From generative-art with MIT License 6 votes vote down vote up
def main(filename="output.png", img_width=2000, img_height=2000):
    ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, img_width, img_height)
    ims.set_fallback_resolution(300.0, 300.0)
    ctx = cairo.Context(ims)

    ctx.rectangle(0, 0, img_width, img_height)
    gradient_start_y = random.randint(0, img_height)
    gradient_end_y = random.randint(gradient_start_y, img_height)
    gradient = cairo.LinearGradient(0, gradient_start_y, img_width, gradient_end_y)
    stop = 0
    while stop < 0.98:
        color = (random.random(), random.random(), random.random())
        gradient.add_color_stop_rgb(stop, *color)
        stop = random.uniform(stop, 1)

    ctx.set_source(gradient)
    ctx.fill()

    ims.write_to_png(filename) 
Example #4
Source File: edge.py    From pypath with GNU General Public License v3.0 6 votes vote down vote up
def draw_directed_edge(self, edge, src_vertex, dest_vertex):
        if src_vertex == dest_vertex:  # TODO
            return self.draw_loop_edge(edge, src_vertex)

        src_pos, dest_pos = src_vertex.position, dest_vertex.position
        ctx = self.context

        # Set up the gradient
        lg = LinearGradient(src_pos[0], src_pos[1], dest_pos[0], dest_pos[1])
        edge_color = edge.color[:3] + self.alpha_at_src
        edge_color_end = edge_color[:3] + self.alpha_at_dest
        lg.add_color_stop_rgba(0, *edge_color)
        lg.add_color_stop_rgba(1, *edge_color_end)

        # Draw the edge
        ctx.set_source(lg)
        ctx.set_line_width(edge.width)
        ctx.move_to(*src_pos)
        ctx.line_to(*dest_pos)
        ctx.stroke() 
Example #5
Source File: square-pack.py    From generative-art with MIT License 5 votes vote down vote up
def get_random_gradient(square, palette):
    (x, y, size) = square
    g = cairo.LinearGradient(x, y, x + size * RATIO, y + size)
    colors = random_colors(palette, 2)
    g.add_color_stop_rgb(0, *colors[0])
    g.add_color_stop_rgb(1, *colors[1])
    return g 
Example #6
Source File: fingers.py    From generative-art with MIT License 5 votes vote down vote up
def draw_line(ctx, x, y, x2, y2, line_width, color1, color2):
    ctx.move_to(x, y)
    ctx.line_to(x2, y2)

    ctx.set_line_width(line_width)
    ctx.set_line_cap(cairo.LineCap.ROUND)

    g = cairo.LinearGradient(x, y, x2, y2)
    g.add_color_stop_rgb(0, *color1)
    g.add_color_stop_rgb(1, *color2)
    ctx.set_source(g)
    ctx.stroke() 
Example #7
Source File: chart.py    From openxenmanager with GNU General Public License v2.0 5 votes vote down vote up
def _do_draw(self, context, rect):
        """
        Do all the drawing stuff.
        
        @type context: cairo.Context
        @param context: The context to draw on.
        @type rect: gtk.gdk.Rectangle
        @param rect: A rectangle representing the charts area.
        """
        if self._color != None:
            #set source color
            context.set_source_rgb(*color_gdk_to_cairo(self._color))
        elif self._gradient != None:
            #set source gradient
            cs = color_gdk_to_cairo(self._gradient[0])
            ce = color_gdk_to_cairo(self._gradient[1])
            gradient = cairo.LinearGradient(0, 0, 0, rect.height)
            gradient.add_color_stop_rgb(0, cs[0], cs[1], cs[2])
            gradient.add_color_stop_rgb(1, ce[0], ce[1], ce[2])
            context.set_source(gradient)
        elif self._pixbuf:
            context.set_source_pixbuf(self._pixbuf, 0, 0)
        else:
            context.set_source_rgb(1, 1, 1) #fallback to white bg
        #create the background rectangle and fill it:
        context.rectangle(0, 0, rect.width, rect.height)
        context.fill() 
Example #8
Source File: tool_shape.py    From drawing with GNU General Public License v3.0 5 votes vote down vote up
def get_pattern_h(self, xmin, xmax):
		pattern = cairo.LinearGradient(xmin, 0.0, xmax, 0.0)
		return pattern 
Example #9
Source File: tool_shape.py    From drawing with GNU General Public License v3.0 5 votes vote down vote up
def get_pattern_v(self, ymin, ymax):
		pattern = cairo.LinearGradient(0.0, ymin, 0.0, ymax)
		return pattern 
Example #10
Source File: tool_line.py    From drawing with GNU General Public License v3.0 5 votes vote down vote up
def do_tool_operation(self, operation):
		cairo_context = self.start_tool_operation(operation)
		cairo_context.set_operator(operation['operator'])
		cairo_context.set_line_cap(operation['line_cap'])
		line_width = operation['line_width']
		cairo_context.set_line_width(line_width)
		c1 = operation['rgba']
		c2 = operation['rgba2']
		x1 = operation['x_press']
		y1 = operation['y_press']
		x2 = operation['x_release']
		y2 = operation['y_release']
		if operation['use_gradient']:
			pattern = cairo.LinearGradient(x1, y1, x2, y2)
			pattern.add_color_stop_rgba(0.1, c1.red, c1.green, c1.blue, c1.alpha)
			pattern.add_color_stop_rgba(0.9, c2.red, c2.green, c2.blue, c2.alpha)
			cairo_context.set_source(pattern)
		else:
			cairo_context.set_source_rgba(c1.red, c1.green, c1.blue, c1.alpha)
		if operation['use_dashes']:
			cairo_context.set_dash([2 * line_width, 2 * line_width])
		# We don't memorize the path because all coords are here anyway for the
		# linear grandient and/or the arrow.
		cairo_context.move_to(x1, y1)
		cairo_context.line_to(x2, y2)

		self.stroke_with_operator(operation['operator'], cairo_context, \
		                                    line_width, operation['is_preview'])

		if operation['use_arrow']:
			utilities_add_arrow_triangle(cairo_context, x2, y2, x1, y1, line_width)

	############################################################################
################################################################################ 
Example #11
Source File: coral_play.py    From generative-art with MIT License 4 votes vote down vote up
def polyp(ctx, x, y, width, height, color):
    min_dimension = min(width, height)
    center_x = x + width // 2
    center_y = y + height // 2
    radius = random.randint(int(min_dimension / 10), int(min_dimension / 6))

    line_width = radius // 5
    ball_radius = int(line_width * 1.5)
    center = Point(0, 0).buffer(radius)
    existing_shapes = center.buffer(-0.1)
    for _ in range(20):
        for _ in range(MAX_ATTEMPTS):
            (end_x, end_y) = make_limb(width, height, ball_radius)
            ball = Point(end_x, end_y).buffer(ball_radius)
            line = LineString([(0, 0), (end_x, end_y)]).buffer(line_width).difference(center)
            limb = line.union(ball)
            if not existing_shapes.intersects(limb):
                break
        else:
            continue

        existing_shapes = existing_shapes.union(limb)

        # Draw line
        ctx.save()
        ctx.translate(center_x, center_y)
        ctx.move_to(0, 0)
        ctx.line_to(end_x, end_y)
        ctx.set_line_width(line_width)
        ctx.set_line_cap(cairo.LineCap.ROUND)
        g = cairo.LinearGradient(0, 0, end_x, end_y)
        add_gradient_stops(color, g)
        ctx.set_source(g)
        ctx.stroke()

        # Draw ball at the end
        g = cairo.RadialGradient(end_x, end_y, 0, end_x, end_y, ball_radius)
        add_gradient_stops(color, g)
        ctx.set_source(g)
        ctx.arc(end_x, end_y, ball_radius, 0, 2 * math.pi)
        ctx.fill()
        ctx.restore()

    # Draw in center
    g = cairo.RadialGradient(center_x, center_y, 0, center_x, center_y, radius)
    add_gradient_stops(color, g)
    ctx.set_source(g)
    ctx.arc(center_x, center_y, radius, 0, 2 * math.pi)
    ctx.fill()