Advertisement
here2share

# Tk_rgb_sq_spiral.py

Feb 22nd, 2022
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. # Tk_rgb_sq_spiral.py
  2.  
  3. from Tkinter import *
  4. from math import sqrt
  5.  
  6. def oRGB(rgb): # pass
  7.     r,g,b = rgb
  8.     return "#%02x%02x%02x" % (r,g,b)
  9.    
  10. ww = 600
  11. hh = 600
  12. root = Tk()
  13. root.title("# Tk_rgb_sq_spiral")
  14. root.geometry("%dx%d+0+0"%(ww,hh))
  15. canvas = Canvas(root, width=ww, height=hh)
  16. canvas.grid()
  17.  
  18. def switch(state):
  19.     return {
  20.         0 : (x + stepSize, y),
  21.         1 : (x, y - stepSize),
  22.         2 : (x - stepSize, y),
  23.         3 : (x, y + stepSize),
  24.     }[state]
  25.  
  26. # to record spiral
  27. xy = []
  28.  
  29. # set up spiral
  30. step = 1
  31. state = 0
  32. numSteps = 1
  33. turnCounter = 1
  34. stepSize = 5
  35. cols = ww / stepSize
  36. rows = hh / stepSize
  37. totalSteps = cols * rows
  38. x = ww / 2
  39. y = hh / 2
  40.  
  41. o256 = range(0,256,10)
  42. rgb = [(r,g,b) for r in o256 for g in o256 for b in o256][::-1]
  43.  
  44. while 1:
  45.     xy.append((x,y))
  46.    
  47.     # Change state
  48.     if step % numSteps == 0:
  49.         state = (state + 1) % 4
  50.         turnCounter += 1
  51.         if turnCounter % 2 == 0:
  52.             numSteps += 1
  53.     step += 1
  54.     x, y = switch(state)
  55.  
  56.     # Are we done?
  57.     if step > totalSteps:
  58.         break
  59.    
  60. step = 20
  61.  
  62. for x,y in xy:
  63.     t = stepSize
  64.     color = oRGB(rgb[step])
  65.     canvas.create_rectangle((x,y,x+t,y+t), fill=color, outline='')
  66.     step += 1
  67. canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement