本文整理汇总了Python中cairo.RadialGradient方法的典型用法代码示例。如果您正苦于以下问题:Python cairo.RadialGradient方法的具体用法?Python cairo.RadialGradient怎么用?Python cairo.RadialGradient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cairo
的用法示例。
在下文中一共展示了cairo.RadialGradient方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_gradient
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import RadialGradient [as 别名]
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)
示例2: get_pattern_r
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import RadialGradient [as 别名]
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
示例3: sphere
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import RadialGradient [as 别名]
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)
示例4: sphere
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import RadialGradient [as 别名]
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)
示例5: polyp
# 需要导入模块: import cairo [as 别名]
# 或者: from cairo import RadialGradient [as 别名]
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()