Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_flippant_spiral_rainbow.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageDraw, ImageFilter
- import math
- cell_size = 20
- SQ = 600 // cell_size * cell_size
- GRID = SQ // cell_size
- CENTER = (GRID - 1) / 2
- rr = tk.Tk()
- rr.geometry('+0+0')
- cv = tk.Canvas(rr, width=SQ, height=SQ, bg='black')
- cv.pack(side='left')
- canvas_image_id = cv.create_image(0, 0, anchor='nw')
- MIN_SPEED = 1.0
- MAX_SPEED = 20.0
- rainbow = []
- def z(r, g, b):
- rainbow.append(f"#{r:02X}{g:02X}{b:02X}")
- r, g, b = 255, 0, 0
- for g in range(256):
- z(r, g, b)
- for r in range(254, -1, -1):
- z(r, g, b)
- for b in range(256):
- z(r, g, b)
- for g in range(254, -1, -1):
- z(r, g, b)
- for r in range(256):
- z(r, g, b)
- for b in range(254, -1, -1):
- z(r, g, b)
- lc = len(rainbow) - 1
- img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
- draw = ImageDraw.Draw(img)
- 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)
- )
- )
- speeds = [0.0] * (GRID * GRID)
- for i, (x, y) in enumerate(order):
- speeds[y * GRID + x] = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * (1.0 - i / ((GRID * GRID) - 1))
- cells = [0.0] * (GRID * GRID)
- while 1:
- i = 0
- for y in range(GRID):
- for x in range(GRID):
- cells[i] += speeds[i]
- if cells[i] >= lc:
- cells[i] = lc
- speeds[i] *= -1
- elif cells[i] <= 0:
- cells[i] = 0
- speeds[i] *= -1
- col = rainbow[int(cells[i])]
- x0 = x * cell_size
- y0 = y * cell_size
- draw.rectangle(
- (x0, y0, x0 + cell_size, y0 + cell_size),
- fill=col
- )
- i += 1
- img_out = img.filter(ImageFilter.GaussianBlur(radius=50 * 0.2))
- tk_img = ImageTk.PhotoImage(img_out)
- cv.itemconfig(canvas_image_id, image=tk_img)
- cv.tk_img = tk_img
- rr.update_idletasks()
- rr.update()
Advertisement
Add Comment
Please, Sign In to add comment