here2share

# tk_spiral_hue_ani.py

Jan 8th, 2026
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. # tk_spiral_hue_ani.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. root = tk.Tk()
  14. root.title("# tk_spiral_hue_ani.py")
  15. canvas = tk.Canvas(root, width=SQ, height=SQ, bg='black')
  16. canvas.pack()
  17. canvas_image_id = canvas.create_image(0, 0, anchor='nw')
  18.  
  19. colors = []
  20. loop = range(0, 256, 25)
  21. for r in loop:
  22.     for g in loop:
  23.         for b in loop:
  24.             colors.append(f"#{r:02X}{g:02X}{b:02X}")
  25.  
  26. def get_hue(hex_color):
  27.     r = int(hex_color[1:3], 16) / 255.0
  28.     g = int(hex_color[3:5], 16) / 255.0
  29.     b = int(hex_color[5:7], 16) / 255.0
  30.     h, _, _ = colorsys.rgb_to_hsv(r, g, b)
  31.     return h
  32.  
  33. colors = sorted(colors, key=get_hue)
  34. num_colors = len(colors)
  35.  
  36. max_dist = math.hypot(CENTER, CENTER)
  37. order = sorted(
  38.     [(x, y) for y in range(GRID) for x in range(GRID)],
  39.     key=lambda xy: (
  40.         math.hypot(xy[0] - CENTER, xy[1] - CENTER) / max_dist,
  41.         math.atan2(xy[1] - CENTER, xy[0] - CENTER)
  42.     )
  43. )
  44.  
  45. frame_count = 0
  46.  
  47. img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
  48. while 1:    
  49.     draw = ImageDraw.Draw(img)
  50.    
  51.     for i, (x, y) in enumerate(order):
  52.         color_idx = (i + frame_count) % num_colors
  53.         col = colors[color_idx]
  54.        
  55.         x0, y0 = x * cell_size, y * cell_size
  56.         draw.rectangle([x0, y0, x0 + cell_size, y0 + cell_size], fill=col)
  57.  
  58.     img_blurred = img.filter(ImageFilter.GaussianBlur(radius=8))
  59.    
  60.     tk_img = ImageTk.PhotoImage(img_blurred)
  61.     canvas.itemconfig(canvas_image_id, image=tk_img)
  62.    
  63.     frame_count += 1
  64.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment