Advertisement
here2share

# Tk_erosion_rgb.py

Mar 28th, 2022
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. # Tk_erosion_rgb.py
  2.  
  3. from tkinter import *
  4. import time
  5. from PIL import Image, ImageTk
  6. from random import shuffle as rs, randint as ri
  7.  
  8. tx =  time.time
  9. ww = 180
  10. hh = 180
  11.  
  12. root = Tk()
  13. root.title("Tk_erosion")
  14. root.geometry("%dx%d+-6+-2"%(ww,hh))
  15. canvas = Canvas(width=ww, height=hh)
  16. canvas.pack()
  17.  
  18. def rgb2hex(r,g,b):
  19.     return '#%02X%02X%02X'%(r,g,b)
  20.    
  21. def motion(event):
  22.     mouse_pos.append((event.x, event.y))
  23. root.bind('<B1-Motion>', motion)
  24.  
  25. cols = ww
  26. rows = hh
  27.  
  28. rainbow=[]
  29. def z(r,g,b):
  30.     rainbow.append((r,g,b))
  31. r,g,b=255,0,0
  32. for g in range(256):
  33.     z(r,g,b)
  34. for r in range(254, -1, -1):
  35.     z(r,g,b)
  36. for b in range(256):
  37.     z(r,g,b)
  38. for g in range(254, -1, -1):
  39.     z(r,g,b)
  40. for r in range(256):
  41.     z(r,g,b)
  42. for b in range(254, -1, -1):
  43.     z(r,g,b)
  44. rainbow = rainbow[1:-1]+rainbow[::-1]
  45. max_rgb=len(rainbow)
  46.  
  47. rgb = []
  48. screen = []
  49. xy = {}
  50. for j in range(0,rows):
  51.     for i in range(0,cols):
  52.         xy[i,j] = len(rgb)
  53.         rgb += [(ri(0,9999))]
  54.         if (0 < i < cols-1) and  (0 < j < rows-1):
  55.             screen += [(i,j)]
  56. rs(rgb)
  57. current = rgb[:]
  58. previous = rgb[:]
  59.  
  60. mouse_pos = []
  61. image = Image.new("RGB", (ww,hh))
  62.  
  63. def draw():
  64.     image.putdata(rgb)
  65.     photo = ImageTk.PhotoImage(image)
  66.     canvas.create_image(0,0,image=photo,anchor=NW)
  67.     canvas.update()
  68.  
  69. def sides(i,j):
  70.     return ([i-1,j],[i+1,j],[i,j-1],[i,j+1])
  71.  
  72. swap = 1
  73. magic = 0
  74.  
  75. #Mainloop
  76. while 1:
  77.     rs(screen)
  78.     exit = current[:]
  79.     for i,j in screen:
  80.         if mouse_pos:
  81.             x,y = mouse_pos.pop(0)
  82.             for x2,y2 in  sides(x,y):
  83.                 try:
  84.                     t = xy[x2,y2]
  85.                     current[t], swap = swap, current[t]
  86.                 except:
  87.                     0
  88.                    
  89.         t = xy[i,j]
  90.         top = max([current[xy[x,y]] for x,y in sides(i,j)])
  91.        
  92.         if top > current[t]+300:
  93.             current[t] += 270
  94.        
  95.         rgb[t] = rainbow[current[t]%max_rgb] #
  96.    
  97.     draw()
  98.     if exit == current:
  99.         break
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement