here2share

# tk_flippant_spiral_rainbow.py

Jan 3rd, 2026
27
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_spiral_rainbow.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import math
  6.  
  7. cell_size = 20
  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 = 1.0
  21. MAX_SPEED = 20.0
  22.  
  23. rainbow = []
  24. def z(r, g, b):
  25.     rainbow.append(f"#{r:02X}{g:02X}{b:02X}")
  26. r, g, b = 255, 0, 0
  27. for g in range(256):
  28.     z(r, g, b)
  29. for r in range(254, -1, -1):
  30.     z(r, g, b)
  31. for b in range(256):
  32.     z(r, g, b)
  33. for g in range(254, -1, -1):
  34.     z(r, g, b)
  35. for r in range(256):
  36.     z(r, g, b)
  37. for b in range(254, -1, -1):
  38.     z(r, g, b)
  39.  
  40. lc = len(rainbow) - 1
  41.  
  42. img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
  43. draw = ImageDraw.Draw(img)
  44.  
  45. max_dist = math.hypot(CENTER, CENTER)
  46. order = sorted(
  47.     [(x, y) for y in range(GRID) for x in range(GRID)],
  48.     key=lambda xy: (
  49.         math.hypot(xy[0] - CENTER, xy[1] - CENTER) / max_dist,
  50.         math.atan2(xy[1] - CENTER, xy[0] - CENTER)
  51.     )
  52. )
  53.  
  54.  
  55. speeds = [0.0] * (GRID * GRID)
  56. for i, (x, y) in enumerate(order):
  57.     speeds[y * GRID + x] = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * (1.0 - i / ((GRID * GRID) - 1))
  58.  
  59. cells = [0.0] * (GRID * GRID)
  60.  
  61. while 1:
  62.     i = 0
  63.     for y in range(GRID):
  64.         for x in range(GRID):
  65.             cells[i] += speeds[i]
  66.  
  67.             if cells[i] >= lc:
  68.                 cells[i] = lc
  69.                 speeds[i] *= -1
  70.             elif cells[i] <= 0:
  71.                 cells[i] = 0
  72.                 speeds[i] *= -1
  73.  
  74.             col = rainbow[int(cells[i])]
  75.             x0 = x * cell_size
  76.             y0 = y * cell_size
  77.  
  78.             draw.rectangle(
  79.                 (x0, y0, x0 + cell_size, y0 + cell_size),
  80.                 fill=col
  81.             )
  82.            
  83.             i += 1
  84.  
  85.     img_out = img.filter(ImageFilter.GaussianBlur(radius=50 * 0.2))
  86.  
  87.     tk_img = ImageTk.PhotoImage(img_out)
  88.     cv.itemconfig(canvas_image_id, image=tk_img)
  89.     cv.tk_img = tk_img
  90.  
  91.     rr.update_idletasks()
  92.     rr.update()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment