Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_flippant_spiral_rgb_zigzag.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
- rgb_zigzag = []
- step = 15
- def z(r, g, b):
- rgb_zigzag.append(f"#{r:02X}{g:02X}{b:02X}")
- r, g, b = 0, 0, 0
- direction_r = 1
- direction_g = 1
- direction_b = 1
- while 1:
- z(r, g, b)
- r += step * direction_r
- r = max(0, min(255, r))
- if r in (0, 255):
- direction_r *= -1
- z(r, g, b)
- g += step * direction_g
- g = max(0, min(255, g))
- if g in (0, 255):
- direction_g *= -1
- z(r, g, b)
- b += step * direction_b
- b = max(0, min(255, b))
- if b in (0, 255):
- direction_b *= -1
- if (r, g, b) == (255, 255, 255):
- break
- lc = len(rgb_zigzag) - 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 = rgb_zigzag[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