Advertisement
here2share

# tk_rgb_jelly.py

Dec 23rd, 2023
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. # tk_rgb_jelly.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk, ImageFilter, ImageDraw
  5. import math
  6. import random
  7.  
  8. ww = 500
  9. hh = 500
  10.  
  11. blank_image = Image.new("RGBA", (ww, hh), "black")
  12. draw = ImageDraw.Draw(blank_image)
  13.  
  14. 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)]
  15. random.shuffle(colors)
  16. Lc = len(colors)
  17. print(Lc)
  18.  
  19. def blur_canvas(radius):
  20.     image = blank_image.filter(ImageFilter.GaussianBlur(radius))
  21.     image = image.point(lambda x: x // 30 * 30)
  22.     photo = ImageTk.PhotoImage(image)
  23.     canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  24.     canvas.image = photo
  25.  
  26. def rotate_polygon(polygon, angle_degrees):
  27.     angle_radians = math.radians(angle_degrees)
  28.     centroid_x, centroid_y = 0, 0
  29.     for x, y in polygon:
  30.         centroid_x += x
  31.         centroid_y += y
  32.  
  33.     centroid_x /= len(polygon)
  34.     centroid_y /= len(polygon)
  35.  
  36.     translated_coords = [(x - centroid_x, y - centroid_y) for x, y in polygon]
  37.  
  38.     rotated_coords = []
  39.     for x, y in translated_coords:
  40.         new_x = x * math.cos(angle_radians) - y * math.sin(angle_radians)
  41.         new_y = x * math.sin(angle_radians) + y * math.cos(angle_radians)
  42.         rotated_coords.append((new_x, new_y))
  43.  
  44.     final_coords = [(coord[0] + centroid_x, coord[1] + centroid_y) for coord in rotated_coords]
  45.  
  46.     return final_coords
  47.  
  48. all_triangles = []
  49. def draw_triangles(size):
  50.     height = size * math.sqrt(3) / 2
  51.     row = 0
  52.     while row * height < ww:
  53.         half_offset_x = -2 if row % 2 else size // 2 - 2
  54.         col = -1
  55.         while col * size < hh + size * 2:
  56.             x0 = (col - 1) * size - 2 - half_offset_x
  57.             y0 = row * height
  58.             x1 = x0 + size
  59.             y1 = y0 + height
  60.             triangle = [
  61.                 (x0, y0), (x1, y0), (x1 - size / 2, y1)
  62.             ]
  63.             rotated_triangle = rotate_polygon(triangle, 180)
  64.             rotated_triangle = [(coord[0] + size // 2, coord[1] + size * 0.3) for coord in rotated_triangle]
  65.  
  66.             all_triangles.append(triangle) ###
  67.             all_triangles.append(rotated_triangle)
  68.             col += 1
  69.         row += 1
  70.     change_triangle_colors(0)
  71.  
  72. def change_triangle_colors(event):
  73.     random.shuffle(colors)
  74.     color_index = 0
  75.     for filled_triangle in all_triangles:
  76.         draw.polygon(filled_triangle, outline='black', fill=colors[color_index])
  77.         color_index = (color_index + 1) % Lc
  78.     blur_canvas(16)
  79.  
  80. root = tk.Tk()
  81. root.title("tk_rgb_jelly")
  82.  
  83. canvas = tk.Canvas(root, width=ww, height=hh, bg='black')
  84. canvas.pack()
  85.  
  86. root.bind("<space>", change_triangle_colors)
  87.  
  88. triangle_size = 50
  89. draw_triangles(triangle_size)
  90.  
  91. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement