here2share

# tk_fading_hue_cycle_pad.py

May 2nd, 2026
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.26 KB | None | 0 0
  1. # tk_fading_hue_cycle_pad.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageDraw, ImageFilter
  5. import math
  6. import time
  7.  
  8. N = 180
  9. SCALE = 4
  10. P = 8
  11. FULL_N = N + P * 2
  12. PADDED_SIZE = FULL_N
  13. ellipse_size = 20
  14.  
  15. blur_kernel = ImageFilter.Kernel((3, 3), [1] * 9, scale=9)
  16.  
  17. def hsv_to_rgb(h, s, v):
  18.     h = float(h)
  19.     s = float(s)
  20.     v = float(v)
  21.  
  22.     h60 = h / 60.0
  23.     h60f = math.floor(h60)
  24.     hi = int(h60f) % 6
  25.     f = h60 - h60f
  26.  
  27.     p = v * (1 - s)
  28.     q = v * (1 - f * s)
  29.     t = v * (1 - (1 - f) * s)
  30.  
  31.     if hi == 0:
  32.         r, g, b = v, t, p
  33.     elif hi == 1:
  34.         r, g, b = q, v, p
  35.     elif hi == 2:
  36.         r, g, b = p, v, t
  37.     elif hi == 3:
  38.         r, g, b = p, q, v
  39.     elif hi == 4:
  40.         r, g, b = t, p, v
  41.     else:
  42.         r, g, b = v, p, q
  43.  
  44.     return int(r*255), int(g*255), int(b*255)
  45.  
  46. # Precompute hue table
  47. hue_cache = [hsv_to_rgb(i, 1.0, 1.0) for i in range(360)]
  48.  
  49. # Create RGBA image
  50. size = 511
  51. flow_field = Image.new("RGBA", (size, size), (0, 0, 0, 0))
  52. pixels = flow_field.load()
  53.  
  54. cx = cy = size // 2
  55. radius = 255
  56.  
  57. for y in range(size):
  58.     for x in range(size):
  59.         dx = x - cx
  60.         dy = y - cy
  61.         distance = math.sqrt(dx*dx + dy*dy)
  62.  
  63.         angle = (math.degrees(math.atan2(dy, dx)) + 360) % 360
  64.         r, g, b = hue_cache[int(angle)]
  65.  
  66.         alpha = int(255 - distance / 2)
  67.         pixels[x, y] = (r, g, b, alpha)
  68.  
  69. color_sources = [Image.new('RGB', (PADDED_SIZE, PADDED_SIZE), hue_cache[h]) for h in range(360)]
  70. black_image = Image.new('RGB', (PADDED_SIZE, PADDED_SIZE), (0, 0, 0))
  71.  
  72. mask_cache = {}
  73. point_mask_cache = {}
  74.  
  75. root = tk.Tk()
  76. density = Image.new('RGB', (PADDED_SIZE, PADDED_SIZE), (0, 0, 0))
  77. canvas = tk.Canvas(root, width=N*SCALE, height=N*SCALE)
  78. canvas.pack()
  79. img = density.crop((P, P, P+N, P+N)).resize((N*SCALE, N*SCALE), Image.NEAREST)
  80. photo = ImageTk.PhotoImage(img)
  81. img_id = canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  82.  
  83. last = None
  84. last_fade_time = time.time()
  85.  
  86. def fade_to_black():
  87.     global density, last_fade_time
  88.     current_time = time.time()
  89.     if current_time - last_fade_time >= 0.:
  90.         density = Image.blend(density, black_image, 0.01)
  91.         last_fade_time = current_time
  92.  
  93. def step():
  94.     global density
  95.     density = density.filter(blur_kernel)
  96.  
  97. def render():
  98.     return density.crop((P, P, P+N, P+N)).resize((N*SCALE, N*SCALE), Image.NEAREST)
  99.  
  100. def add_ellipse_at_point(x, y, hue_int):
  101.     """Add an ellipse at the given point"""
  102.     global density
  103.     px, py = x + P, y + P
  104.    
  105.     # Create cache key for this point and hue
  106.     key = (px, py, hue_int)
  107.    
  108.     if key not in point_mask_cache:
  109.         # Create a mask with an ellipse at the point
  110.         mask = Image.new('L', (PADDED_SIZE, PADDED_SIZE), 0)
  111.         draw = ImageDraw.Draw(mask)
  112.         # Draw ellipse (circle) with radius 4
  113.         draw.ellipse([(px - ellipse_size//2, py - ellipse_size//2),
  114.                       (px + ellipse_size//2, py + ellipse_size//2)], fill=255)
  115.         point_mask_cache[key] = mask
  116.    
  117.     # Composite the colored ellipse onto the density
  118.     density = Image.composite(color_sources[hue_int], density, point_mask_cache[key])
  119.  
  120. def on_drag(e):
  121.     x = max(0, min(N-1, e.x // SCALE))
  122.     y = max(0, min(N-1, e.y // SCALE))
  123.     hue_int = int((time.time() * 40) % 360)
  124.     global density, last
  125.    
  126.     # Add ellipse at the current point
  127.     add_ellipse_at_point(x, y, hue_int)
  128.    
  129.     if last:
  130.         lx, ly = last
  131.         px1, py1 = lx + P, ly + P
  132.         px2, py2 = x + P, y + P
  133.         key = (px1, py1, px2, py2)
  134.        
  135.         if key not in mask_cache:
  136.             mask = Image.new('L', (PADDED_SIZE, PADDED_SIZE), 0)
  137.             draw = ImageDraw.Draw(mask)
  138.             draw.line([(px1, py1), (px2, py2)], fill=255, width=ellipse_size, joint='curve')
  139.             mask_cache[key] = mask
  140.        
  141.         density = Image.composite(color_sources[hue_int], density, mask_cache[key])
  142.     last = (x, y)
  143.  
  144. def on_release(e):
  145.     global last
  146.     last = None
  147.  
  148. def update():
  149.     step()
  150.     fade_to_black()
  151.     img = render()
  152.     photo = ImageTk.PhotoImage(img)
  153.     canvas.itemconfig(img_id, image=photo)
  154.     canvas.image = photo
  155.     root.after(1, update)
  156.  
  157. canvas.bind("<B1-Motion>", on_drag)
  158. canvas.bind("<ButtonRelease-1>", on_release)
  159.  
  160. update()
  161. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment