def __init__(self, *args): self.linear = cairo.LinearGradient(0, 0, 1, 1) # mask self.linear.add_color_stop_rgb(0, 0, 0.3, 0.8) # mask self.linear.add_color_stop_rgb(1, 0, 0.8, 0.3) # mask # mask self.radial = cairo.RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.75) # mask self.radial.add_color_stop_rgba(0, 0, 0, 0, 1) # mask self.radial.add_color_stop_rgba(0.5, 0, 0, 0, 0) # mask # mask self.radialinv = cairo.RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.75) self.radialinv.add_color_stop_rgba(0, 0, 0, 0, 0) self.radialinv.add_color_stop_rgba(0.5, 0, 0, 0, 1) super(Mask, self).__init__(*args)
def roundedRect(self, context, x, y, width, height, radius, fadeR, fadeG, fadeB, fadeToPoint): if min(width, height) < 2*radius: raise ValueError("Radius is too large for dimensions!") #{{{ corner fade rectColor = context.get_source().get_rgba() with context: for sideY in [0, 1]: for sideX in [0, 1]: # either center of fillet or corner of rect fadeToX = x+sideX*width+(0 if fadeToPoint else (-1 if sideX else 1)*radius) fadeToY = y+sideY*height+(0 if fadeToPoint else (-1 if sideY else 1)*radius) fadeToRadius = 1/2 if fadeToPoint else math.ceil(math.sqrt(2)*radius) # center of fillet filletX = x+sideX*width+(-1 if sideX else 1)*radius filletY = y+sideY*height+(-1 if sideY else 1)*radius fadeCorner = cairo.RadialGradient(fadeToX, fadeToY, fadeToRadius, filletX, filletY, radius) fadeCorner.add_color_stop_rgb(0, fadeR, fadeG, fadeB) fadeCorner.add_color_stop_rgba(1, *rectColor) context.set_source(fadeCorner) context.rectangle(x+(width-radius if sideX else 0), y+(height-radius if sideY else 0), radius, radius) context.fill() #}}} #{{{ rounded rectangle context.new_sub_path() context.arc(x+width-radius, y+height-radius, radius, 0, 1/2*math.pi) context.arc(x+radius, y+height-radius, radius, 1/2*math.pi, math.pi) context.arc(x+radius, y+radius, radius, math.pi, 3/2*math.pi) context.arc(x+width-radius, y+radius, radius, 3/2*math.pi, 2*math.pi) context.close_path() context.fill()
def __init__(self, rgba, radius): self.radius = radius self.rgba = rgba self.rgba_clear = tuple(rgba[:3]) + (0, ) self.linear = cairo.LinearGradient(0, 1, 0, -1) self.linear.add_color_stop_rgba(0, *self.rgba) self.linear.add_color_stop_rgba(1, *self.rgba_clear) self.radial = cairo.RadialGradient(0, 0, 0, 0, 0, self.radius * 2) self.radial.add_color_stop_rgba(0, *self.rgba) self.radial.add_color_stop_rgba(1, *self.rgba_clear)
def set_source(self, ctx): """Create a pattern and set it as source for the given context.""" if self.type == "linear": (x1, y1), (x2, y2) = self.xy1, self.xy2 pat = cairo.LinearGradient(x1, y1, x2, y2) elif self.type == "radial": (x1, y1), (x2, y2), (x3, y3) = self.xy1, self.xy2, self.xy3 pat = cairo.RadialGradient(x1, y1, x2, y2, x3, y3) for stop, color in self.stops_colors: if len(color) == 4: pat.add_color_stop_rgba(stop, *color) else: pat.add_color_stop_rgb(stop, *color) ctx.set_source(pat)
def set_source(self, ctx): if self.type == "linear": (x1, y1), (x2, y2) = self.xy1, self.xy2 pat = cairo.LinearGradient(x1, y1, x2, y2) elif self.type == "radial": (x1, y1), (x2, y2), (x3, y3) = self.xy1, self.xy2, self.xy3 pat = cairo.RadialGradient(x1, y1, x2, y2, x3, y3) for stop, color in self.stops_colors: if len(color) == 4: pat.add_color_stop_rgba(stop, *color) else: pat.add_color_stop_rgb(stop, *color) ctx.set_source(pat)
def radial(self, cr): cr.save() x = [] y = [] for it in range(3): x.append(np.random.randint(self.border, self.MAX_X - self.border)) y.append(np.random.randint(self.border, self.MAX_Y - self.border)) cr.translate(x[0], y[0]) ran = np.random.randint(10, 40) ran_1 = np.random.randint(10, 20) r2 = cairo.RadialGradient(0, 0, ran_1, 0, 0, ran) r2.add_color_stop_rgba(0.8, 0, 0, 0, 1) r2.add_color_stop_rgba(0, 1, 1, 1, 1) cr.set_source(r2) cr.arc(0, 0, ran, 0, math.pi * 2) cr.fill() cr.restore()
def test_rounded_rectangle_apply(cr): w, h = 200, 200 s = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) c = cairo.Context(s) # TODO: This DOES NOT work with the code below... if False: c.save() p = cairo.RadialGradient(w / 2.0, h / 2.0, 0.0, w / 2.0, h / 2.0, 200.0) p.add_color_stop_rgba(0.0, 0.8, 0.4, 0.0, 0.0) p.add_color_stop_rgba(1.0, 0.8, 0.4, 0.0, 1.0) c.arc(w / 2.0, h / 2.0, 200.0, 0.0, 2.0 * math.pi) c.set_source(p) c.paint() c.restore() # TODO: This DOES work with the code below... if True: c.set_source_rgb(0.0, 1.0, 0.5) c.paint() # cairocks.rounded_rectangle_apply(c, 20.0, 20.0, 160.0, 160.0, 30.0) # TODO: The code below does the exact same thing as the call # to rounded_rectangle_apply. p2 = cairo.SolidPattern(0.0, 0.0, 0.0) c.save() c.set_operator(cairo.OPERATOR_DEST_IN) c.set_source(p2) cairocks.rounded_rectangle(c, 20.0, 20.0, 160.0, 160.0, 30.0) c.fill() c.restore() cr.set_source_surface(s, 400, 200) cr.paint()
def draw(width, height, colors): images = [] post = -1 for i in range(len(colors)): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) cr = cairo.Context(surface) cr.scale(width, height) cr.set_line_width(0.01) pattern = cairo.LinearGradient(0, 0, 1, 1) # assign pre and post pointers if i == len(colors)-1: post = -1 else: post = i+1 if colors[i] != colors[post] and post != -1: pattern.add_color_stop_rgb(0, 1, .3, .3) pattern.add_color_stop_rgb(1, .3, .3, 1) if colors[i] == 0: # blue pattern.add_color_stop_rgb(0, 1, 1, 1) pattern.add_color_stop_rgb(1, 0, 0, 1) else: # red pattern.add_color_stop_rgb(0, 1, 0, 0) pattern.add_color_stop_rgb(1, 1, 1, 1) mask = cairo.RadialGradient(0.5, 0.5, 0.25, 0.5, 0.5, 0.5) mask.add_color_stop_rgba(0, 0, 0, 0, 1) mask.add_color_stop_rgba(0.5, 0, 0, 0, 0) cr.set_source(pattern) cr.mask(mask) surface.write_to_png(f'gradient{i}.png') images.append(imageio.imread(f'gradient{i}.png')) imageio.mimsave('gradient.gif', images)
linewidth = 0.0018 markersize = 0.05 surface = cairo.ImageSurface(cairo.FORMAT_RGB24, image_size, image_size) ctx = cairo.Context(surface) ctx.scale(image_size / (extent * 2.0), -image_size / (extent * 2.0)) ctx.translate(extent, -extent) ctx.set_source_rgb(1, 1, 1) ctx.paint() for i, j in edges: x1, y1 = roots_2d[i] x2, y2 = roots_2d[j] ctx.set_source_rgb(0.2, 0.2, 0.2) ctx.set_line_width(linewidth) ctx.move_to(x1, y1) ctx.line_to(x2, y2) ctx.stroke() for i in range(len(roots)): x, y = roots_2d[i] color = vertex_colors[i] grad = cairo.RadialGradient(x, y, 0.0001, x, y, markersize) grad.add_color_stop_rgb(0, *color) grad.add_color_stop_rgb(1, *color / 2) ctx.set_source(grad) ctx.arc(x, y, markersize, 0, 2 * np.pi) ctx.fill() surface.write_to_png('E8.png')
def render(): w = 600 h = 600 cx = w / 2 cy = h / 2 # Flower vars radius = -1 * h * 0.5 * 0.80; # Radius 80% of canvas final_angle = 1.0471975511965976 # Math.PI / 3, # 60ยบ deep = random.randint(3, 32) print('Deep:' + str(deep)) subangle_factor = (random.random() * 10) - 5.0 print('Subangle:' + str(subangle_factor)) direction = random.choice(['in', 'out', 'inOut']) avaliable_easings = ['Quint', 'Cubic', 'Quad', 'Sine', 'Back', 'Bounce', 'Elastic'] if (direction == 'inOut') or (direction == 'in'): avaliable_easings.remove('Elastic'); avaliable_easings.remove('Back'); easing = random.choice(avaliable_easings) easing_name = direction + easing print('Easing:' + easing_name) # Elemtents to be draw circles = [] circles_radius = [] circles_distance_to_center = [0] # colors # palette = [ [ 30, 81, 52 ], [ 220, 77, 48 ] ] palette = [ [ 0.0833, 0.81, 0.52 ], [ 0.6111, 0.77, 0.48 ] ] # Create surface ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(ims) ctx.save() # Clear ctx.rectangle (0, 0, w, h) # Rectangle(x0, y0, x1, y1) ctx.set_source_rgb(0.1, 0.1, 0.1) ctx.fill () # Move 0,0 to center of canvas ctx.translate( cx, cy ) # Calculate circles last = 0 for i in range(deep): circle = [] t = i / deep e_func = getattr( eaze, direction + easing ) f = e_func(t) # print(t) ctx.rotate( random.random() ) for j in range(i): angle = ( final_angle / i) * j * subangle_factor py = f * radius point = utilz.rotatePoint(0, 0, 0, py, angle) if point[1] != 0: circle.append(point) if j == 0: r = math.fabs(last - py) circles_radius.append(r) circles_distance_to_center.append(py) last = py # ctx.set_source_rgba(0.9, 0.9, 0.9, 0.5) # draw.plot(ctx, point[0], point[1], 1) if len(circle) > 0: circles.append(circle) #print(circle) # ims.write_to_png("test.png") # sys.exit() # Draw circles for i in range( len(circles) -1, 0, -1 ): line_alpha = 1 - (i / deep) col = utilz.hslLerp(palette[0], palette[1], math.fabs(circles_distance_to_center[i] / radius)) # print(col) # Draw 6 times for k in range(6): ctx.rotate(final_angle) ctx.set_operator(cairo.OPERATOR_SCREEN) # Draws all circles in current level for j in range(len(circles[i])): grd = cairo.RadialGradient( circles[i][j][0], circles[i][j][1], circles_radius[i-1]*0.64, circles[i][j][0], circles[i][j][1], circles_radius[i-1] ) grd.add_color_stop_rgba(0, 0.85, 0.108, 0.333, 0.12) grd.add_color_stop_rgba(1, 0.85, 0.108, 0.333, 0.60) rgb = colorsys.hsv_to_rgb( *col ) grd.add_color_stop_rgba(0, *rgb, 0.10) grd.add_color_stop_rgba(1, *rgb, 0.75) ctx.set_source(grd) draw.plot( ctx, circles[i][j][0], circles[i][j][1], circles_radius[i-1] ) ctx.set_source_rgba( *rgb, line_alpha ) draw.circle( ctx, circles[i][j][0], circles[i][j][1], circles_radius[i-1] ); # Cover central part of current level of circles if ( i > 1 ): grd_tapa = cairo.RadialGradient( 0, 0, math.fabs( circles[i][0][1] )*0.78, 0, 0, math.fabs( circles[i][0][1] ) ) grd_tapa.add_color_stop_rgba(0.0, 0.1, 0.1, 0.1, 1.0) grd_tapa.add_color_stop_rgba(1.1, 0.1, 0.1, 0.1, 0.0) ctx.set_source(grd_tapa) ctx.set_operator(cairo.OPERATOR_OVER) draw.plot(ctx, 0, 0, math.fabs( circles[i][0][1] )) # Move 0,0 to top, left corner ctx.restore() # gp -> generation params gp = collections.OrderedDict() gp['name'] = 'chrysanthemum' gp['params'] = collections.OrderedDict() gp['params']['deep'] = deep gp['params']['easing'] = easing_name gp['params']['saf'] = subangle_factor footline = name.footline( gp ) draw.footline( ctx, footline) filename = name.filename( gp ) return ims, filename, footline
continue days[int(filename[:2])] = int(line.decode('utf-8').split()[0]) # color scheme from https://colorhunt.co/palette/164242 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 2048, 2448) cr = cairo.Context(surface) cr.set_source_rgb(0.984, 0.89, 0.725) cr.paint() cr.scale(2048, 2048) count = 0 for y in range(5): for x in range(6): rg = cairo.RadialGradient(.07 + (x / 5.8), .075 + (y / 4.8), .024, .07 + (x / 5.8), .075 + (y / 4.8), .05) rg.add_color_stop_rgba(1, 0.98, .714, .588, .8) rg.add_color_stop_rgba(1, 0.3, 0.3, 0.3, .9) cr.set_source(rg) cr.arc(.07 + (x / 5.8), .075 + (y / 4.8), .033, 0., 2 * math.pi) cr.fill() if count in days.keys(): cr.set_line_cap(0) cr.set_source_rgba(0.176, 0.2, 0.29, 1.) cr.arc(.07 + (x / 5.8), .075 + (y / 4.8), .03, 1.5 * math.pi, (days[count] / 2972.0) * 2 * math.pi + (1.5 * math.pi))