Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_erosion_rgb.py
- from tkinter import *
- import time
- from PIL import Image, ImageTk
- from random import shuffle as rs, randint as ri
- tx = time.time
- ww = 180
- hh = 180
- root = Tk()
- root.title("Tk_erosion")
- root.geometry("%dx%d+-6+-2"%(ww,hh))
- canvas = Canvas(width=ww, height=hh)
- canvas.pack()
- def rgb2hex(r,g,b):
- return '#%02X%02X%02X'%(r,g,b)
- def motion(event):
- mouse_pos.append((event.x, event.y))
- root.bind('<B1-Motion>', motion)
- cols = ww
- rows = hh
- rainbow=[]
- def z(r,g,b):
- rainbow.append((r,g,b))
- r,g,b=255,0,0
- for g in range(256):
- z(r,g,b)
- for r in range(254, -1, -1):
- z(r,g,b)
- for b in range(256):
- z(r,g,b)
- for g in range(254, -1, -1):
- z(r,g,b)
- for r in range(256):
- z(r,g,b)
- for b in range(254, -1, -1):
- z(r,g,b)
- rainbow = rainbow[1:-1]+rainbow[::-1]
- max_rgb=len(rainbow)
- rgb = []
- screen = []
- xy = {}
- for j in range(0,rows):
- for i in range(0,cols):
- xy[i,j] = len(rgb)
- rgb += [(ri(0,9999))]
- if (0 < i < cols-1) and (0 < j < rows-1):
- screen += [(i,j)]
- rs(rgb)
- current = rgb[:]
- previous = rgb[:]
- mouse_pos = []
- image = Image.new("RGB", (ww,hh))
- def draw():
- image.putdata(rgb)
- photo = ImageTk.PhotoImage(image)
- canvas.create_image(0,0,image=photo,anchor=NW)
- canvas.update()
- def sides(i,j):
- return ([i-1,j],[i+1,j],[i,j-1],[i,j+1])
- swap = 1
- magic = 0
- #Mainloop
- while 1:
- rs(screen)
- exit = current[:]
- for i,j in screen:
- if mouse_pos:
- x,y = mouse_pos.pop(0)
- for x2,y2 in sides(x,y):
- try:
- t = xy[x2,y2]
- current[t], swap = swap, current[t]
- except:
- 0
- t = xy[i,j]
- top = max([current[xy[x,y]] for x,y in sides(i,j)])
- if top > current[t]+300:
- current[t] += 270
- rgb[t] = rainbow[current[t]%max_rgb] #
- draw()
- if exit == current:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement