def draw_rgb_image(self, x, y): s = 80 h = 40 self.context.save() linpat = cairo.LinearGradient(x, y, x+s, y+s) linpat.add_color_stop_rgb(0.0, 1, 1, 1) linpat.add_color_stop_rgb(1.0, 0, 1, 0) self.context.set_source(linpat) self.context.move_to(x,y) self.context.line_to(x,y+s) self.context.line_to(x+s,y+s) self.context.line_to(x+s,y) self.context.fill() linpat = cairo.LinearGradient(x, y+s, x+s, y) linpat.add_color_stop_rgba(0.0, 0, 0, 1, 1) linpat.add_color_stop_rgba(0.5, 1, 0, 1, 0) linpat.add_color_stop_rgba(1.0, 1, 0, 0, 1) self.context.set_source(linpat) self.context.move_to(x,y) self.context.line_to(x,y+s) self.context.line_to(x+s,y+s) self.context.line_to(x+s,y) self.context.fill() self.context.restore() self.context.save() self.context.move_to(x+5, y+92) self.context.set_font_size(11.5) self.context.show_text("RGB Image") self.context.restore() return
def __init__(self, rgba_start, rgba_end=None, vertical=False, flipped=False): if rgba_end is None: rgba_end = tuple(rgba_start[:3]) + (0, ) self.vertical = vertical self.flipped = flipped if vertical: self.linear = cairo.LinearGradient(0, -0.5, 0, 0.5) else: self.linear = cairo.LinearGradient(-0.5, 0, 0.5, 0) self.linear.add_color_stop_rgba(0, *rgba_start) self.linear.add_color_stop_rgba(1, *rgba_end)
def set_source_rgb(self, colour: Union[ColorType, List[ColorType]], ctx: cairocffi.Context = None): # If an alternate context is not provided then we draw to the # drawer's default context if ctx is None: ctx = self.ctx if isinstance(colour, list): if len(colour) == 0: # defaults to black ctx.set_source_rgba(*utils.rgb("#000000")) elif len(colour) == 1: ctx.set_source_rgba(*utils.rgb(colour[0])) else: linear = cairocffi.LinearGradient(0.0, 0.0, 0.0, self.height) step_size = 1.0 / (len(colour) - 1) step = 0.0 for c in colour: rgb_col = utils.rgb(c) if len(rgb_col) < 4: rgb_col[3] = 1 linear.add_color_stop_rgba(step, *rgb_col) step += step_size ctx.set_source(linear) else: ctx.set_source_rgba(*utils.rgb(colour))
def background(): ctx.rectangle(0, 0, width, height) pattern = cairo.LinearGradient(0, 0, width, height) pattern.add_color_stop_rgb(0.2, (76 / 255), (40 / 255), (156 / 255)) #76,40,156 pattern.add_color_stop_rgb(1, 223 / 255, 153 / 255, 216 / 255) #223,153,216 ctx.set_source(pattern) ctx.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_rgb(self, colour): if type(colour) == list: linear = cairocffi.LinearGradient(0.0, 0.0, 0.0, self.height) step_size = 1.0 / (len(colour) - 1) step = 0.0 for c in colour: rgb_col = utils.rgb(c) if len(rgb_col) < 4: rgb_col[3] = 1 linear.add_color_stop_rgba(step, *rgb_col) step += step_size self.ctx.set_source(linear) else: self.ctx.set_source_rgba(*utils.rgb(colour))
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 __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 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 fade_pattern(x0, y0, x1, y1, r, g, b, a, fade): pat = cairo.LinearGradient(x0, y0, x1, y1) if fade == 'None': return cairo.SolidPattern(r, g, b, a) elif fade == 'in': pat.add_color_stop_rgba(0, r, g, b, 0) pat.add_color_stop_rgba(1, r, g, b, a) elif fade == 'out': pat.add_color_stop_rgba(0, r, g, b, a) pat.add_color_stop_rgba(1, r, g, b, 0) elif fade == 'both': pat.add_color_stop_rgba(0, r, g, b, 0) pat.add_color_stop_rgba(0.5, r, g, b, a) pat.add_color_stop_rgba(1, r, g, b, 0) else: sys.stderr.write(f"Unknown fade style {fade}!\n") return pat
def gradientTri( ctx, points, colors, alpha = 1.0 ): def fillPath( ctx, points ): ctx.new_path() ctx.move_to( points[0][0], points[0][1] ) for i in range(1, len(points)): ctx.line_to( points[i][0], points[i][1] ) ctx.close_path() ctx.fill() points_ = points * 2 for i in range(len(points)): grd = cairo.LinearGradient( points_[i][0], points_[i][1], (points_[i+1][0] + points_[i+2][0]) / 2, (points_[i+1][1] + points_[i+2][1]) / 2 ) grd.add_color_stop_rgba( 0, *colors[i].rgb, alpha ) grd.add_color_stop_rgba( 0.99, *colors[i].rgb, 0.0 ) ctx.set_source(grd) fillPath( ctx, points )
def render(): global ctx, cx, cy, ori_w, ori_h, gp setPoints() addNoise() ctx.save() ctx.translate(cx, cy) ctx.rotate(random.random() * 2 * math.pi) ctx.translate(-cx, -cy) renderPoints() ctx.set_operator(cairo.OPERATOR_SCREEN) renderPoints() ctx.restore() ctx.restore() # Rectangle border ctx.set_source_rgb(0.1, 0.1, 0.1) margin = 40 ctx.rectangle(0, 0, ori_w, margin) ctx.rectangle(0, margin, margin, ori_h) ctx.rectangle(0, ori_h - margin, ori_w, ori_h - margin) ctx.rectangle(ori_w - margin, 0, ori_w, ori_h) ctx.fill() # Gradien overlay ctx.set_operator(cairo.OPERATOR_OVERLAY) grd = cairo.LinearGradient(0, 0, ori_w, ori_h) grd.add_color_stop_rgba(0, *c1.rgb, 0.10) grd.add_color_stop_rgba(1, *c2.hsl, 0.50) ctx.rectangle(0, 0, ori_w, ori_h) # Rectangle(x0, y0, x1, y1) ctx.set_source(grd) ctx.fill() footline = name.footline(gp) draw.footline(ctx, footline, 8, ori_h - 10) filename = name.filename(gp) return ims, filename, footline
def set_source_rgb(self, colour: Union[ColorType, List[ColorType]]): if isinstance(colour, list): if len(colour) == 0: # defaults to black self.ctx.set_source_rgba(*utils.rgb("#000000")) elif len(colour) == 1: self.ctx.set_source_rgba(*utils.rgb(colour[0])) else: linear = cairocffi.LinearGradient(0.0, 0.0, 0.0, self.height) step_size = 1.0 / (len(colour) - 1) step = 0.0 for c in colour: rgb_col = utils.rgb(c) if len(rgb_col) < 4: rgb_col[3] = 1 linear.add_color_stop_rgba(step, *rgb_col) step += step_size self.ctx.set_source(linear) else: self.ctx.set_source_rgba(*utils.rgb(colour))
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)
lw = 6.0 r = 60.0 bc = 0.4, 0.4, 0.4 cr.set_source_rgb(*[c * 1.70 for c in bc]) cr.arc((w / 2.0) + (lw / 2.0), (h / 2.0) + (lw / 2.0), r + (lw / 2.0), 0.0, 2.0 * math.pi) cr.fill() cr.set_line_width(lw) cr.arc(w / 2.0, h / 2.0, r, 0.0, 2.0 * math.pi) cr.set_source_rgb(1.0, 1.0, 1.0) cr.fill_preserve() # --- p = cairo.LinearGradient((w / 2.0) + r, (h / 2.0) + r, (w / 2.0) - r, (h / 2.0) - r) p.add_color_stop_rgba(0.0, 0.8, 0.4, 0.0, 1.0) p.add_color_stop_rgba(0.8, 0.8, 0.4, 0.0, 0.0) cr.set_source(p) cr.fill_preserve() # --- cr.set_source_rgb(*bc) cr.stroke() cr.set_line_width(lw / 2.0) cr.arc(w / 2.0, h / 2.0, r - (lw / 2.0), 0.0, 2.0 * math.pi) cr.set_source_rgb(1.0, 1.0, 1.0) cr.stroke()
#!/usr/bin/env python import math import cairocffi as cairo WIDTH, HEIGHT = 256, 256 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context(surface) ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0) pat.add_color_stop_rgba(1, 0.7, 0, 0, 0.5) # First stop, 50% opacity pat.add_color_stop_rgba(0, 0.9, 0.7, 0.2, 1) # Last stop, 100% opacity ctx.rectangle(0, 0, 1, 1) # Rectangle(x0, y0, x1, y1) ctx.set_source(pat) ctx.fill() ctx.translate(0.1, 0.1) # Changing the current transformation matrix ctx.move_to(0, 0) ctx.arc(0.2, 0.1, 0.1, -math.pi / 2, 0) # Arc(cx, cy, radius, start_angle, stop_angle) ctx.line_to(0.5, 0.1) # Line to (x,y) ctx.curve_to(0.5, 0.2, 0.5, 0.4, 0.2, 0.8) # Curve(x1, y1, x2, y2, x3, y3) ctx.close_path() ctx.set_source_rgb(0.3, 0.2, 0.5) # Solid color ctx.set_line_width(0.02)
def DrawIt(exons_pos, ex_lengths, motifs_list, gene, fileName): width = len(gene) + 150 height = 100 + (len(motifs_list) + 2) * 50 surface = cairo.SVGSurface(fileName + ".svg", width, height) context = cairo.Context(surface) #draw Intron context.set_line_width(5) context.move_to(50, height - 50) context.line_to(len(gene) + 50, height - 50) context.stroke() #exon color pat = cairo.LinearGradient(0.0, 0.0, 0.0, 1.0) pat.add_color_stop_rgba(0, 0.3, 1, 0.2, 0.5) #draw legend context.rectangle(5, 5, 12, 12) context.set_source(pat) context.fill() context.select_font_face("pyrus", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) context.set_font_size(20) context.move_to(20, 18) context.set_source_rgb(0, 0, 0) context.show_text("Exon") context.move_to(85, 12) context.line_to(97, 12) context.stroke() context.move_to(100, 18) context.show_text("Intron") context.move_to(30, height - 50) context.show_text("5'") context.move_to(len(gene) + 60, height - 50) context.show_text("3'") #draw exons for start, lengths in zip(exons_pos, ex_lengths): context.rectangle(int(start[0]) + 50, height - 110, int(lengths), 110) context.set_source(pat) context.fill() context.stroke() #draw motifs skipped = 0 for i, motif in enumerate(motifs_list): j = i + 1 - skipped positions = find_all(motif, gene) if positions == []: #just skip the rest if there are no motifs in that gene skipped += 1 continue #set color / legend for motif mot = cairo.LinearGradient(0.0, 0.0, 0.0, 0.7) r, g, b = getNextColor(i) mot.add_color_stop_rgba(0, r / 255, g / 255, b / 255, 0.75) context.rectangle(5, 30 * j, 12, 12) context.set_source(mot) context.fill() context.move_to(20, 30 * j + 12) context.set_source_rgb(0, 0, 0) context.show_text(motif) for pos in positions: context.rectangle(int(pos) + 50, height - 100, len(motif), 100) context.set_source(mot) context.fill() context.stroke() surface.finish()
i += 1 funFactLineHeight = context.text_extents( funFactsLines[0] )[3] # via http://blog.mathieu-leplatre.info/text-extents-with-python-cairo.html labelHeight += i * funFactLineHeight * 1.5 + 0.035 * pageSize # draw white box for label context.set_source_rgb(1, 1, 1) context.rectangle(0, 0.9 * pageHeight - labelHeight, pageWidth, labelHeight) context.fill() # draw drop shadows, top one slightly smaller than bottom one to simulate # soft light from just above gradient = cairo.LinearGradient( 0, 0.9 * pageHeight - labelHeight, 0, 0.9 * pageHeight - labelHeight - 0.004 * pageSize) gradient.add_color_stop_rgba(0, 0, 0, 0, 0.13) gradient.add_color_stop_rgba(1, 0, 0, 0, 0.0) context.rectangle(0, 0.9 * pageHeight - labelHeight - 0.004 * pageSize, pageWidth, 0.004 * pageSize) context.set_source(gradient) context.fill() gradient = cairo.LinearGradient(0, 0.9 * pageHeight, 0, 0.9 * pageHeight + 0.006 * pageSize) gradient.add_color_stop_rgba(0, 0, 0, 0, 0.21) gradient.add_color_stop_rgba(1, 0, 0, 0, 0.0) context.rectangle(0, 0.9 * pageHeight, pageWidth, 0.006 * pageSize) context.set_source(gradient) context.fill()