Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cairo
- from random import randint
- import matplotlib.colors
- # colors = '''#cccccc
- # #fe6a6a
- # #ee91b6
- # #ffd7fc
- # #df93ff
- # #bee1e3
- # #b0cffe
- # #c4c3e3
- # #ba97ff
- # #a7fddb
- # #a6e8aa
- # #ccfda6
- # #f9ff91
- # #997d65
- # #f1956e
- # #fcb96b
- # #ead376'''
- colors = '''
- #dcff0f
- #dcff0f
- #dcff0f
- #6507d8
- #6507d8
- #fd01ba
- #fd01ba
- #fd01ba
- #1dd9b8
- #ffe6da
- #ffffff
- #000000
- '''
- colors = colors.strip()
- colors = colors.split('\n')
- colors = tuple(map(matplotlib.colors.to_rgb, colors))
- WIDTH, HEIGHT = 1000, 1000
- surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
- ctx = cairo.Context(surface)
- ctx.scale(WIDTH, HEIGHT) # Normalizing the canvas
- def draw_segment(ctx, x, y, color, n):
- x, y = x/n, y/n
- unit = 1/n * 2
- ctx.set_source_rgb(*color)
- ctx.move_to(x, y)
- ctx.line_to(x+unit, y)
- ctx.line_to(x, y+unit)
- ctx.line_to(x-unit, y+unit)
- ctx.close_path()
- ctx.fill()
- n = 7
- prev_indices = [randint(0, len(colors)-1) for i in range(n)]
- for y in range(n):
- for x in range(n+1):
- # get new color index
- index = prev_indices[-1]
- while prev_indices[-1] == index or prev_indices[0] == index: # the same color as right next to it or above it
- index = randint(0, len(colors)-1)
- # draw segment
- draw_segment(ctx, x, y, colors[index], n)
- # push current color index
- prev_indices.pop(0)
- prev_indices.append(index)
- surface.write_to_png("example.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement