Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_flippant_spiral_16_colors.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')
- vga_colors = [
- "#FFFFFF", # Bright White
- "#AAAAAA", # Light Gray
- "#FFFF55", # Bright Yellow
- "#FFA500", # Orange
- "#AA5500", # Brown
- "#AA0000", # Bright Red
- "#FF5555", # Red
- "#AA00AA", # Magenta
- "#00AAAA", # Bright Cyan
- "#55FFFF", # Cyan
- "#00AA00", # Bright Green
- "#55FF55", # Green
- "#0000AA", # Bright Blue
- "#5555FF", # Blue
- "#555555", # Dark Gray
- "#000000" # Black
- ]
- vga_colors = sorted(vga_colors)[::-1]
- lc = len(vga_colors) - 1
- MIN_SPEED = 0.00001
- MAX_SPEED = 0.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)
- )
- )[::-1]
- 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 = vga_colors[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=40 * 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