here2share

# tk_flippant_spiral_16_colors.py

Jan 6th, 2026
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. # tk_flippant_spiral_16_colors.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. vga_colors = [
  19.     "#FFFFFF",  # Bright White
  20.     "#AAAAAA",  # Light Gray
  21.     "#FFFF55",  # Bright Yellow
  22.     "#FFA500",  # Orange
  23.     "#AA5500",  # Brown
  24.     "#AA0000",  # Bright Red
  25.     "#FF5555",  # Red
  26.     "#AA00AA",  # Magenta
  27.     "#00AAAA",  # Bright Cyan
  28.     "#55FFFF",  # Cyan
  29.     "#00AA00",  # Bright Green
  30.     "#55FF55",  # Green
  31.     "#0000AA",  # Bright Blue
  32.     "#5555FF",  # Blue
  33.     "#555555",  # Dark Gray
  34.     "#000000"   # Black
  35. ]
  36. vga_colors = sorted(vga_colors)[::-1]
  37. lc = len(vga_colors) - 1
  38.  
  39. MIN_SPEED = 0.00001
  40. MAX_SPEED = 0.1
  41.  
  42. img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
  43. draw = ImageDraw.Draw(img)
  44. max_dist = math.hypot(CENTER, CENTER)
  45.  
  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. )[::-1]
  53.  
  54. speeds = [0.0] * (GRID * GRID)
  55. for i, (x, y) in enumerate(order):
  56.     speeds[y * GRID + x] = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * (1.0 - i / ((GRID * GRID) - 1))
  57.  
  58. cells = [0.0] * (GRID * GRID)
  59.  
  60. while 1:
  61.     i = 0
  62.     for y in range(GRID):
  63.         for x in range(GRID):
  64.             cells[i] += speeds[i]
  65.             if cells[i] >= lc:
  66.                 cells[i] = lc
  67.                 speeds[i] *= -1
  68.             elif cells[i] <= 0:
  69.                 cells[i] = 0
  70.                 speeds[i] *= -1
  71.  
  72.             col = vga_colors[int(cells[i])]
  73.             x0 = x * cell_size
  74.             y0 = y * cell_size
  75.             draw.rectangle((x0, y0, x0 + cell_size, y0 + cell_size), fill=col)
  76.             i += 1
  77.  
  78.     img_out = img.filter(ImageFilter.GaussianBlur(radius=40 * 0.2))
  79.     tk_img = ImageTk.PhotoImage(img_out)
  80.     cv.itemconfig(canvas_image_id, image=tk_img)
  81.     cv.tk_img = tk_img
  82.     rr.update_idletasks()
  83.     rr.update()
Advertisement
Add Comment
Please, Sign In to add comment