here2share

# tk_flippant_cells_spiral_2.py

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