Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_300x300_Plasma_Attack.py ZZZ next step is to put in all the rainbow colors about equal somehow with sorted(sum())
- import random, math
- from Tkinter import *
- from PIL import Image, ImageTk
- sq = 300
- root=Tk()
- root.title("Tk Plasma Attack")
- canvas=Canvas(root,width=sq/2,height=sq/2)
- canvas.pack()
- def rgb2hex(rgb):
- r,g,b = rgb
- return "#{:02x}{:02x}{:02x}".format(r,g,b)
- 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)
- Lr = len(RAINBOW)
- xy = Image.new('RGB', (sq, sq))
- class X:
- def eval(self, x, y):
- return x
- def __str__(self):
- return "x"
- class Y:
- def eval(self, x, y):
- return y
- def __str__(self):
- return "y"
- class SinPi:
- def __init__(self, prob):
- self.arg = buildExpr(prob * prob)
- def __str__(self):
- return "sin(pi*" + str(self.arg) + ")"
- def eval(self, x, y):
- return math.sin(math.pi * self.arg.eval(x,y))
- class SubPi:
- def __init__(self, prob):
- self.lhs = buildExpr(prob * prob)
- self.rhs = buildExpr(prob * prob)
- def eval(self, x, y):
- return math.pi / 2 * self.lhs.eval(x,y) - self.rhs.eval(x,y)
- class Sub:
- def __init__(self, prob):
- self.lhs = buildExpr(prob * prob)
- self.rhs = buildExpr(prob * prob)
- def eval(self, x, y):
- return self.lhs.eval(x,y) - self.rhs.eval(x,y)
- def buildExpr(prob = 0.95):
- if random.random() < prob:
- return random.choice([SinPi, Sub, SubPi])(prob)
- else:
- return random.choice([X, Y])()
- def plotColor(redExp, greenExp, blueExp, pixelsPerUnit = 150):
- for py in range(sq):
- for px in range(sq):
- # Convert pixel location to [-1,1] coordinates
- x = float(px - pixelsPerUnit) / pixelsPerUnit
- y = -float(py - pixelsPerUnit) / pixelsPerUnit
- z = [exp.eval(x,y)*255 for exp in (redExp, greenExp, blueExp)]
- z = RAINBOW[int(sum(z))%Lr]
- # Scale [-1,1] result to [0,255]
- xy.putpixel((px,py), tuple(z))
- def makeImage(numPics = 20):
- while 1:
- redExp = buildExpr()
- greenExp = buildExpr()
- blueExp = buildExpr()
- plotColor(redExp, greenExp, blueExp)
- imgTk = ImageTk.PhotoImage(xy)
- canvas.create_image((0,0), image=imgTk)
- canvas.update()
- makeImage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement