Python cairo.RadialGradient() Examples

The following are 5 code examples of cairo.RadialGradient(). 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: tool_shape.py    From drawing with GNU General Public License v3.0 5 votes vote down vote up
def get_pattern_r(self, center_x, center_y, rad):
		pattern = cairo.RadialGradient(center_x, center_y, 0.1 * rad, \
		                               center_x, center_y, 0.9 * rad)
		# the 2 centers could be 2 distinct points
		return pattern 
Example #3
Source File: shaded_discs.py    From generative-art with MIT License 4 votes vote down vote up
def sphere(ctx, color, img_width, img_height, existing_shapes, min_radius=10, max_attempts=100):
    def getStartXY():
        x = random.randint(min_radius, img_width - min_radius)
        y = random.randint(min_radius, img_height - min_radius)
        return (x, y)

    # 1. Get a start point that doesn't overlap with anything we already have.
    (start_x, start_y) = getStartXY()
    sphere = Point(start_x, start_y).buffer(min_radius, resolution=8)
    for _ in range(max_attempts * 100):
        if not existing_shapes.intersects(sphere):
            break
        (start_x, start_y) = getStartXY()
        sphere = Point(start_x, start_y).buffer(min_radius, resolution=8)
    # For the rare case that we did not find a working point.
    if existing_shapes.intersects(sphere):
        print("Could not find valid start point!")
        return existing_shapes

    # 2. Grow the sphere as far as possible, randomly.
    failed_attempts = 0
    max_increment = 500
    radius = min_radius
    while failed_attempts < max_attempts:
        new_radius = radius + max_increment
        new_sphere = Point(start_x, start_y).buffer(new_radius, resolution=12)
        if not existing_shapes.intersects(new_sphere) and within_canvas(start_x, start_y, img_width, img_height, new_radius):
            radius = new_radius
            sphere = new_sphere
        else:
            failed_attempts += 1
            max_increment = int(max_increment * 3/4)
            if max_increment < 1:
                break

    # 3. Draw the sphere
    color_t = palettes.hex_to_tuple(color)
    tints = colors.tints(color_t, 5)
    shades = colors.shades(color_t, 3)

    ctx.arc(start_x, start_y, radius, 0, 2 * math.pi)

    gradient = cairo.RadialGradient(start_x, start_y, 0, start_x, start_y, radius)
    step1 = random.uniform(0.5, 1.0)
    step2 = random.uniform(step1, 1.0)
    step3 = random.uniform(step2, 1.0)
    step4 = random.uniform(step3, 1.0)
    step5 = random.uniform(step4, 1.0)
    gradient.add_color_stop_rgb(0, *random.choice(shades))
    gradient.add_color_stop_rgb(step1, *random.choice(tints))
    gradient.add_color_stop_rgb(step2, *random.choice(tints))
    gradient.add_color_stop_rgb(step3, 1, 1, 1)
    gradient.add_color_stop_rgb(step4, 1, 1, 1)
    gradient.add_color_stop_rgb(step5, *random.choice(tints))
    gradient.add_color_stop_rgb(1, *random.choice(shades))

    ctx.set_source(gradient)
    ctx.fill()

    buffered_sphere = Point(start_x, start_y).buffer(radius + 5, resolution=12)
    return existing_shapes.union(buffered_sphere) 
Example #4
Source File: shiny_spheres.py    From generative-art with MIT License 4 votes vote down vote up
def sphere(ctx, color, img_width, img_height, existing_shapes, min_radius=10, max_attempts=100):
    def getStartXY():
        x = random.randint(min_radius, img_width - min_radius)
        y = random.randint(min_radius, img_height - min_radius)
        return (x, y)

    # 1. Get a start point that doesn't overlap with anything we already have.
    (start_x, start_y) = getStartXY()
    sphere = Point(start_x, start_y).buffer(min_radius, resolution=8)
    for _ in range(max_attempts * 100):
        if not existing_shapes.intersects(sphere):
            break
        (start_x, start_y) = getStartXY()
        sphere = Point(start_x, start_y).buffer(min_radius, resolution=8)
    # For the rare case that we did not find a working point.
    if existing_shapes.intersects(sphere):
        print("Could not find valid start point!")
        return existing_shapes

    # 2. Grow the sphere as far as possible, randomly.
    failed_attempts = 0
    max_increment = 500
    radius = min_radius
    while failed_attempts < max_attempts:
        new_radius = radius + max_increment
        new_sphere = Point(start_x, start_y).buffer(new_radius, resolution=12)
        if not existing_shapes.intersects(new_sphere) and within_canvas(start_x, start_y, img_width, img_height, new_radius):
            radius = new_radius
            sphere = new_sphere
        else:
            failed_attempts += 1
            max_increment = int(max_increment * 3/4)
            if max_increment < 1:
                break

    # 3. Draw the sphere
    color_t = palettes.hex_to_tuple(color)
    tints = colors.tints(color_t, 5)
    shades = colors.shades(color_t, 3)

    ctx.arc(start_x, start_y, radius, 0, 2 * math.pi)

    gradient = cairo.RadialGradient(start_x - (1/2) * radius, start_y + (1/2) * radius, 0, start_x - (1/4) * radius, start_y + (1/4) * radius, radius * (5/4))
    gradient.add_color_stop_rgb(0, 1, 1, 1)
    gradient.add_color_stop_rgb(0.9, *tints[-1])
    gradient.add_color_stop_rgb(1, *shades[-1])
    ctx.set_source(gradient)
    ctx.fill()

    buffered_sphere = Point(start_x, start_y).buffer(radius + 5, resolution=12)
    return existing_shapes.union(buffered_sphere) 
Example #5
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()