Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_flippant_cells_spiral.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
- 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 = 0.5
- MAX_SPEED = 5.0
- c = []
- loop = range(0, 256, int(MAX_SPEED))
- for r in loop:
- for g in loop:
- for b in loop:
- c.append(f"#{r:02X}{g:02X}{b:02X}")
- def hex_to_hue(hexcol):
- r = int(hexcol[1:3], 16) / 255.0
- g = int(hexcol[3:5], 16) / 255.0
- b = int(hexcol[5:7], 16) / 255.0
- h, _, _ = colorsys.rgb_to_hsv(r, g, b)
- return h
- c = sorted(c, key=hex_to_hue)
- lc = len(c) - 1
- img = Image.new('RGB', (SQ, SQ), (0, 0, 0))
- draw = ImageDraw.Draw(img)
- cos_cache = {}
- def cos_cached(v):
- if v not in cos_cache:
- cos_cache[v] = math.cos(v)
- return cos_cache[v]
- spiral_metric = []
- max_dist = math.hypot(CENTER, CENTER)
- for y in range(GRID):
- for x in range(GRID):
- dx = x - CENTER
- dy = y - CENTER
- r = math.hypot(dx, dy) / max_dist
- theta = math.atan2(dy, dx)
- spiral_metric.append(r + 0.5 * cos_cached(theta + r * 6.283185307179586))
- order = sorted(range(len(spiral_metric)), key=lambda i: spiral_metric[i])
- speeds = [0.0] * (GRID * GRID)
- for rank, i in enumerate(order):
- speeds[i] = MIN_SPEED + (MAX_SPEED - MIN_SPEED) * (1.0 - rank / (len(order) - 1))
- cells = [0.0] * (GRID * GRID)
- speeds = speeds[::-1]
- while 1:
- for y in range(GRID):
- for x in range(GRID):
- i = y * GRID + x
- 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 = c[int(cells[i])]
- x0 = x * cell_size
- y0 = y * cell_size
- draw.rectangle(
- (x0, y0, x0 + cell_size, y0 + cell_size),
- fill=col
- )
- 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