Advertisement
yclee126

random color tiled parallelogram

Mar 17th, 2022
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import cairo
  2. from random import randint
  3. import matplotlib.colors
  4.  
  5. # colors = '''#cccccc
  6. # #fe6a6a
  7. # #ee91b6
  8. # #ffd7fc
  9. # #df93ff
  10. # #bee1e3
  11. # #b0cffe
  12. # #c4c3e3
  13. # #ba97ff
  14. # #a7fddb
  15. # #a6e8aa
  16. # #ccfda6
  17. # #f9ff91
  18. # #997d65
  19. # #f1956e
  20. # #fcb96b
  21. # #ead376'''
  22. colors = '''
  23. #dcff0f
  24. #dcff0f
  25. #dcff0f
  26. #6507d8
  27. #6507d8
  28. #fd01ba
  29. #fd01ba
  30. #fd01ba
  31. #1dd9b8
  32. #ffe6da
  33. #ffffff
  34. #000000
  35. '''
  36. colors = colors.strip()
  37. colors = colors.split('\n')
  38. colors = tuple(map(matplotlib.colors.to_rgb, colors))
  39.  
  40. WIDTH, HEIGHT = 1000, 1000
  41.  
  42. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
  43. ctx = cairo.Context(surface)
  44.  
  45. ctx.scale(WIDTH, HEIGHT)  # Normalizing the canvas
  46.  
  47. def draw_segment(ctx, x, y, color, n):
  48.     x, y = x/n, y/n
  49.     unit = 1/n * 2
  50.     ctx.set_source_rgb(*color)
  51.     ctx.move_to(x, y)
  52.     ctx.line_to(x+unit, y)
  53.     ctx.line_to(x, y+unit)
  54.     ctx.line_to(x-unit, y+unit)
  55.     ctx.close_path()
  56.     ctx.fill()
  57.  
  58. n = 7
  59. prev_indices = [randint(0, len(colors)-1) for i in range(n)]
  60. for y in range(n):
  61.     for x in range(n+1):
  62.         # get new color index
  63.         index = prev_indices[-1]
  64.         while prev_indices[-1] == index or prev_indices[0] == index: # the same color as right next to it or above it
  65.             index = randint(0, len(colors)-1)
  66.        
  67.         # draw segment
  68.         draw_segment(ctx, x, y, colors[index], n)
  69.        
  70.         # push current color index
  71.         prev_indices.pop(0)
  72.         prev_indices.append(index)
  73.  
  74. surface.write_to_png("example.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement