here2share

# tk_flippant_cells_spiral.py

Jan 1st, 2026
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # tk_flippant_cells_spiral.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import math
  6. import colorsys
  7.  
  8. cell_size = 20
  9. SQ = 600 // cell_size * cell_size
  10. GRID = SQ // cell_size
  11. CENTER = (GRID - 1) / 2
  12.  
  13. rr = tk.Tk()
  14. rr.geometry('+0+0')
  15.  
  16. cv = tk.Canvas(rr, width=SQ, height=SQ, bg='black')
  17. cv.pack(side='left')
  18.  
  19. canvas_image_id = cv.create_image(0, 0, anchor='nw')
  20.  
  21. MIN_SPEED = 0.5
  22. MAX_SPEED = 5.0
  23.  
  24. c = []
  25. loop = range(0, 256, int(MAX_SPEED))
  26. for r in loop:
  27.     for g in loop:
  28.         for b in loop:
  29.             c.append(f"#{r:02X}{g:02X}{b:02X}")
  30.  
  31. def hex_to_hue(hexcol):
  32.     r = int(hexcol[1:3], 16) / 255.0
  33.     g = int(hexcol[3:5], 16) / 255.0
  34.     b = int(hexcol[5:7], 16) / 255.0
  35.     h, _, _ = colorsys.rgb_to_hsv(r, g, b)
  36.     return h
  37.  
  38. c = sorted(c, key=hex_to_hue)
  39. lc = len(c) - 1
  40.  
  41. img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
  42. draw = ImageDraw.Draw(img)
  43.  
  44. cos_cache = {}
  45.  
  46. def cos_cached(v):
  47.     if v not in cos_cache:
  48.         cos_cache[v] = math.cos(v)
  49.     return cos_cache[v]
  50.  
  51. spiral_metric = []
  52. max_dist = math.hypot(CENTER, CENTER)
  53.  
  54. for y in range(GRID):
  55.     for x in range(GRID):
  56.         dx = x - CENTER
  57.         dy = y - CENTER
  58.         r = math.hypot(dx, dy) / max_dist
  59.         theta = math.atan2(dy, dx)
  60.         spiral_metric.append(r + 0.5 * cos_cached(theta + r * 6.283185307179586))
  61.  
  62. order = sorted(range(len(spiral_metric)), key=lambda i: spiral_metric[i])
  63.  
  64. speeds = [0.0] * (GRID * GRID)
  65. for rank, i in enumerate(order):
  66.     speeds[i] = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * (1.0 - rank / (len(order) - 1))
  67.  
  68. cells = [0.0] * (GRID * GRID)
  69.  
  70. speeds = speeds[::-1]
  71.  
  72. while 1:
  73.     for y in range(GRID):
  74.         for x in range(GRID):
  75.             i = y * GRID + x
  76.             cells[i] += speeds[i]
  77.  
  78.             if cells[i] >= lc:
  79.                 cells[i] = lc
  80.                 speeds[i] *= -1
  81.             elif cells[i] <= 0:
  82.                 cells[i] = 0
  83.                 speeds[i] *= -1
  84.  
  85.             col = c[int(cells[i])]
  86.             x0 = x * cell_size
  87.             y0 = y * cell_size
  88.  
  89.             draw.rectangle(
  90.                 (x0, y0, x0 + cell_size, y0 + cell_size),
  91.                 fill=col
  92.             )
  93.  
  94.     img_out = img.filter(ImageFilter.GaussianBlur(radius=50 * 0.2))
  95.  
  96.     tk_img = ImageTk.PhotoImage(img_out)
  97.     cv.itemconfig(canvas_image_id, image=tk_img)
  98.     cv.tk_img = tk_img
  99.  
  100.     rr.update_idletasks()
  101.     rr.update()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment