here2share

# tk_flippant_spiral_rgb_zigzag.py

Jan 6th, 2026
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # tk_flippant_spiral_rgb_zigzag.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. cv = tk.Canvas(rr, width=SQ, height=SQ, bg='black')
  15. cv.pack(side='left')
  16. canvas_image_id = cv.create_image(0, 0, anchor='nw')
  17.  
  18. MIN_SPEED = 1.0
  19. MAX_SPEED = 20.0
  20.  
  21. rgb_zigzag = []
  22. step = 15
  23.  
  24. def z(r, g, b):
  25.     rgb_zigzag.append(f"#{r:02X}{g:02X}{b:02X}")
  26.  
  27. r, g, b = 0, 0, 0
  28. direction_r = 1
  29. direction_g = 1
  30. direction_b = 1
  31.  
  32. while 1:
  33.     z(r, g, b)
  34.     r += step * direction_r
  35.     r = max(0, min(255, r))
  36.     if r in (0, 255):
  37.         direction_r *= -1
  38.  
  39.         z(r, g, b)
  40.         g += step * direction_g
  41.         g = max(0, min(255, g))
  42.         if g in (0, 255):
  43.             direction_g *= -1
  44.  
  45.             z(r, g, b)
  46.             b += step * direction_b
  47.             b = max(0, min(255, b))
  48.             if b in (0, 255):
  49.                 direction_b *= -1
  50.                
  51.     if (r, g, b) == (255, 255, 255):
  52.         break
  53.  
  54. lc = len(rgb_zigzag) - 1
  55.  
  56. img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
  57. draw = ImageDraw.Draw(img)
  58. max_dist = math.hypot(CENTER, CENTER)
  59.  
  60. order = sorted(
  61.     [(x, y) for y in range(GRID) for x in range(GRID)],
  62.     key=lambda xy: (
  63.         math.hypot(xy[0] - CENTER, xy[1] - CENTER) / max_dist,
  64.         math.atan2(xy[1] - CENTER, xy[0] - CENTER)
  65.     )
  66. )
  67.  
  68. speeds = [0.0] * (GRID * GRID)
  69. for i, (x, y) in enumerate(order):
  70.     speeds[y * GRID + x] = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * (1.0 - i / ((GRID * GRID) - 1))
  71.  
  72. cells = [0.0] * (GRID * GRID)
  73.  
  74. while 1:
  75.     i = 0
  76.     for y in range(GRID):
  77.         for x in range(GRID):
  78.             cells[i] += speeds[i]
  79.             if cells[i] >= lc:
  80.                 cells[i] = lc
  81.                 speeds[i] *= -1
  82.             elif cells[i] <= 0:
  83.                 cells[i] = 0
  84.                 speeds[i] *= -1
  85.  
  86.             col = rgb_zigzag[int(cells[i])]
  87.             x0 = x * cell_size
  88.             y0 = y * cell_size
  89.             draw.rectangle((x0, y0, x0 + cell_size, y0 + cell_size), fill=col)
  90.             i += 1
  91.  
  92.     img_out = img.filter(ImageFilter.GaussianBlur(radius=50 * 0.2))
  93.     tk_img = ImageTk.PhotoImage(img_out)
  94.     cv.itemconfig(canvas_image_id, image=tk_img)
  95.     cv.tk_img = tk_img
  96.     rr.update_idletasks()
  97.     rr.update()
Advertisement
Add Comment
Please, Sign In to add comment