Advertisement
here2share

# Tk_300x300_Plasma_Attack.py ZZZ

Jan 9th, 2021
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. # Tk_300x300_Plasma_Attack.py ZZZ next step is to put in all the rainbow colors about equal somehow with sorted(sum())
  2.  
  3. import random, math
  4. from Tkinter import *
  5. from PIL import Image, ImageTk
  6.  
  7. sq = 300
  8.  
  9. root=Tk()
  10. root.title("Tk Plasma Attack")
  11.  
  12. canvas=Canvas(root,width=sq/2,height=sq/2)
  13. canvas.pack()
  14.  
  15. def rgb2hex(rgb):
  16.     r,g,b = rgb
  17.     return "#{:02x}{:02x}{:02x}".format(r,g,b)
  18.  
  19. RAINBOW=[]
  20. def z(r,g,b):
  21.     RAINBOW.append((r,g,b))
  22. r,g,b=255,0,0
  23. for g in range(256):
  24.     z(r,g,b)
  25. for r in range(254, -1, -1):
  26.     z(r,g,b)
  27. for b in range(256):
  28.     z(r,g,b)
  29. for g in range(254, -1, -1):
  30.     z(r,g,b)
  31. for r in range(256):
  32.     z(r,g,b)
  33. for b in range(254, -1, -1):
  34.     z(r,g,b)
  35. Lr = len(RAINBOW)
  36.  
  37. xy = Image.new('RGB', (sq, sq))
  38.  
  39. class X:
  40.    def eval(self, x, y):
  41.       return x
  42.    
  43.    def __str__(self):
  44.       return "x"
  45.  
  46. class Y:
  47.    def eval(self, x, y):
  48.       return y
  49.    
  50.    def __str__(self):
  51.       return "y"
  52.  
  53. class SinPi:
  54.    def __init__(self, prob):
  55.       self.arg = buildExpr(prob * prob)
  56.    
  57.    def __str__(self):
  58.       return "sin(pi*" + str(self.arg) + ")"
  59.  
  60.    def eval(self, x, y):
  61.       return math.sin(math.pi * self.arg.eval(x,y))
  62.  
  63. class SubPi:
  64.    def __init__(self, prob):
  65.       self.lhs = buildExpr(prob * prob)
  66.       self.rhs = buildExpr(prob * prob)
  67.  
  68.    def eval(self, x, y):
  69.       return math.pi / 2 * self.lhs.eval(x,y) - self.rhs.eval(x,y)
  70.  
  71. class Sub:
  72.    def __init__(self, prob):
  73.       self.lhs = buildExpr(prob * prob)
  74.       self.rhs = buildExpr(prob * prob)
  75.  
  76.    def eval(self, x, y):
  77.       return self.lhs.eval(x,y) - self.rhs.eval(x,y)
  78.  
  79. def buildExpr(prob = 0.95):
  80.    if random.random() < prob:
  81.       return random.choice([SinPi, Sub, SubPi])(prob)
  82.    else:
  83.       return random.choice([X, Y])()
  84.  
  85. def plotColor(redExp, greenExp, blueExp, pixelsPerUnit = 150):
  86.    
  87.     for py in range(sq):
  88.         for px in range(sq):
  89.             # Convert pixel location to [-1,1] coordinates
  90.             x = float(px - pixelsPerUnit) / pixelsPerUnit
  91.             y = -float(py - pixelsPerUnit) / pixelsPerUnit
  92.             z = [exp.eval(x,y)*255 for exp in (redExp, greenExp, blueExp)]
  93.             z = RAINBOW[int(sum(z))%Lr]
  94.  
  95.             # Scale [-1,1] result to [0,255]
  96.             xy.putpixel((px,py), tuple(z))
  97.  
  98. def makeImage(numPics = 20):
  99.    
  100.     while 1:
  101.         redExp = buildExpr()
  102.         greenExp = buildExpr()
  103.         blueExp = buildExpr()
  104.         plotColor(redExp, greenExp, blueExp)
  105.         imgTk = ImageTk.PhotoImage(xy)
  106.  
  107.         canvas.create_image((0,0), image=imgTk)
  108.         canvas.update()
  109.  
  110.  
  111. makeImage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement