Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_rgb_jelly.py
- import tkinter as tk
- from PIL import Image, ImageTk, ImageFilter, ImageDraw
- import math
- import random
- ww = 500
- hh = 500
- blank_image = Image.new("RGBA", (ww, hh), "black")
- draw = ImageDraw.Draw(blank_image)
- colors = ['#%02X%02X%02X'%(r,g,b) for r in range(0, 256, 15) for g in range(0, 256, 15) for b in range(0, 256, 15)]
- random.shuffle(colors)
- Lc = len(colors)
- print(Lc)
- def blur_canvas(radius):
- image = blank_image.filter(ImageFilter.GaussianBlur(radius))
- image = image.point(lambda x: x // 30 * 30)
- photo = ImageTk.PhotoImage(image)
- canvas.create_image(0, 0, anchor=tk.NW, image=photo)
- canvas.image = photo
- def rotate_polygon(polygon, angle_degrees):
- angle_radians = math.radians(angle_degrees)
- centroid_x, centroid_y = 0, 0
- for x, y in polygon:
- centroid_x += x
- centroid_y += y
- centroid_x /= len(polygon)
- centroid_y /= len(polygon)
- translated_coords = [(x - centroid_x, y - centroid_y) for x, y in polygon]
- rotated_coords = []
- for x, y in translated_coords:
- new_x = x * math.cos(angle_radians) - y * math.sin(angle_radians)
- new_y = x * math.sin(angle_radians) + y * math.cos(angle_radians)
- rotated_coords.append((new_x, new_y))
- final_coords = [(coord[0] + centroid_x, coord[1] + centroid_y) for coord in rotated_coords]
- return final_coords
- all_triangles = []
- def draw_triangles(size):
- height = size * math.sqrt(3) / 2
- row = 0
- while row * height < ww:
- half_offset_x = -2 if row % 2 else size // 2 - 2
- col = -1
- while col * size < hh + size * 2:
- x0 = (col - 1) * size - 2 - half_offset_x
- y0 = row * height
- x1 = x0 + size
- y1 = y0 + height
- triangle = [
- (x0, y0), (x1, y0), (x1 - size / 2, y1)
- ]
- rotated_triangle = rotate_polygon(triangle, 180)
- rotated_triangle = [(coord[0] + size // 2, coord[1] + size * 0.3) for coord in rotated_triangle]
- all_triangles.append(triangle) ###
- all_triangles.append(rotated_triangle)
- col += 1
- row += 1
- change_triangle_colors(0)
- def change_triangle_colors(event):
- random.shuffle(colors)
- color_index = 0
- for filled_triangle in all_triangles:
- draw.polygon(filled_triangle, outline='black', fill=colors[color_index])
- color_index = (color_index + 1) % Lc
- blur_canvas(16)
- root = tk.Tk()
- root.title("tk_rgb_jelly")
- canvas = tk.Canvas(root, width=ww, height=hh, bg='black')
- canvas.pack()
- root.bind("<space>", change_triangle_colors)
- triangle_size = 50
- draw_triangles(triangle_size)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement