Advertisement
here2share

# tk_rgb_interpolation_frosted.py

Mar 17th, 2023
780
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. # tk_rgb_interpolation_frosted.py
  2.  
  3. import tkinter as tk
  4. import random
  5.  
  6. ww = 400
  7. hh = 400
  8. canvas = tk.Canvas(width=ww, height=hh)
  9. canvas.pack()
  10.  
  11. sq = 10
  12. sq2 = sq // 2
  13. rad = 1 / 11
  14. ddd = [0]
  15.  
  16. def rgb():
  17.     return [random.randint(128, 255) for i in range(3)]
  18.    
  19. def interpolate():
  20.     canvas.update()
  21.     def dist(x = sq / 2, y = sq / 2):
  22.         return round(((i - x) ** 2 + (j - y) ** 2) ** 0.5 * rad, 2)
  23.     # Initialize the grid with the corner values
  24.     def quad():
  25.         r = cubic(0)
  26.         g = cubic(1)
  27.         b = cubic(2)
  28.  
  29.         color = f"#{r:02x}{g:02x}{b:02x}"
  30.         x2,y2 = x+3,y+3
  31.         canvas.create_rectangle(x2+i, y2+j, x2+i, y2+j, fill=color, outline="")
  32.     def cubic(k):
  33.         return min(255, max(0, int(t[k] * d + cc[k] * (1 - d)) + random.randint(-30, 30)))
  34.  
  35.     for y in range(0, hh, sq):
  36.         for x in range(0, ww, sq):
  37.             x2, y2 = x + sq, y + sq
  38.             c1 = grid[x, y]
  39.             c2 = grid[x + sq, y]
  40.             c3 = grid[x, y + sq]
  41.             c4 = grid[x + sq, y + sq]
  42.             cc = [int((c1[k] + c2[k] + c3[k] + c4[k]) / 4) for k in (0,1,2)]
  43.             for j in range(0, sq2):
  44.                 for i in range(0, sq2):
  45.                     t = c1
  46.                     d = dist()
  47.                     quad()
  48.             for j in range(0, sq2):
  49.                 for i in range(sq2, sq):
  50.                     t = c2
  51.                     d = dist()
  52.                     quad()
  53.             for j in range(sq2, sq):
  54.                 for i in range(0, sq2):
  55.                     t = c3
  56.                     d = dist()
  57.                     quad()
  58.             for j in range(sq2, sq):
  59.                 for i in range(sq2, sq):
  60.                     t = c4
  61.                     d = dist()
  62.                     quad()
  63.            
  64.     canvas.update()
  65.  
  66. while 1:
  67.     grid = {}
  68.     for x in range(0,(ww+sq+1),sq):
  69.         for y in range(0,(hh+sq+1),sq):
  70.             grid[x,y] = rgb()
  71.  
  72.     interpolate()
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement