Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_spiral_hue_ani.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw, ImageFilter
- import math
- import colorsys
- cell_size = 20
- SQ = 600 // cell_size * cell_size
- GRID = SQ // cell_size
- CENTER = (GRID - 1) / 2
- root = tk.Tk()
- root.title("# tk_spiral_hue_ani.py")
- canvas = tk.Canvas(root, width=SQ, height=SQ, bg='black')
- canvas.pack()
- canvas_image_id = canvas.create_image(0, 0, anchor='nw')
- colors = []
- loop = range(0, 256, 25)
- for r in loop:
- for g in loop:
- for b in loop:
- colors.append(f"#{r:02X}{g:02X}{b:02X}")
- def get_hue(hex_color):
- r = int(hex_color[1:3], 16) / 255.0
- g = int(hex_color[3:5], 16) / 255.0
- b = int(hex_color[5:7], 16) / 255.0
- h, _, _ = colorsys.rgb_to_hsv(r, g, b)
- return h
- colors = sorted(colors, key=get_hue)
- num_colors = len(colors)
- max_dist = math.hypot(CENTER, CENTER)
- order = sorted(
- [(x, y) for y in range(GRID) for x in range(GRID)],
- key=lambda xy: (
- math.hypot(xy[0] - CENTER, xy[1] - CENTER) / max_dist,
- math.atan2(xy[1] - CENTER, xy[0] - CENTER)
- )
- )
- frame_count = 0
- img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
- while 1:
- draw = ImageDraw.Draw(img)
- for i, (x, y) in enumerate(order):
- color_idx = (i + frame_count) % num_colors
- col = colors[color_idx]
- x0, y0 = x * cell_size, y * cell_size
- draw.rectangle([x0, y0, x0 + cell_size, y0 + cell_size], fill=col)
- img_blurred = img.filter(ImageFilter.GaussianBlur(radius=8))
- tk_img = ImageTk.PhotoImage(img_blurred)
- canvas.itemconfig(canvas_image_id, image=tk_img)
- frame_count += 1
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment