Advertisement
here2share

# tk_gradientify.py

Aug 13th, 2024 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. # tk_gradientify.py # just a very basic demo
  2.  
  3. import tkinter as tk
  4. import random
  5. import time
  6.  
  7. # Constants
  8. WIDTH, HEIGHT = 600, 600
  9. GRID_SIZE = 12
  10. CELL_WIDTH = WIDTH // GRID_SIZE
  11. CELL_HEIGHT = HEIGHT // GRID_SIZE
  12. zz = 3
  13. num_steps = GRID_SIZE**2
  14. step_size = (256**3) // (num_steps - 1)
  15.  
  16. root = tk.Tk()
  17. canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT)
  18. root.geometry("%dx%d+-10+0"%(WIDTH, HEIGHT))
  19. canvas.pack()
  20.  
  21. def convert_int_to_rgb(value):
  22.     b = value % 256
  23.     value //= 256
  24.     g = value % 256
  25.     value //= 256
  26.     r = value % 256
  27.     return (r, g, b)
  28.  
  29. COLORS = []
  30.  
  31. steps = 0
  32. while len(COLORS) < num_steps:
  33.     COLORS.append(convert_int_to_rgb(steps))
  34.     steps += step_size
  35. COLORS[-1] = (255, 255, 255)
  36.  
  37. def seconds(i):
  38.     t = time.time() + i
  39.     while t > time.time() and delay:
  40.         canvas.update()
  41.     canvas.update()
  42.  
  43. def color_difference(color1, color2):
  44.     return sum(abs(a - b) for a, b in zip(color1, color2))
  45.  
  46. def find_best_neighbor(x, y):
  47.     current_color = grid[x][y]
  48.     best_neighbor = None
  49.     target = (x, y)
  50.     min_difference = float('inf')
  51.     reason = ""
  52.  
  53.     for x0, y0 in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
  54.         x2, y2 = x0 + x, y0 + y
  55.         ttt = [((x2 + 1 * x0 if x0 else x + i), (y2 + 1 * y0 if y0 else y + i)) for i in (-1, 0, 1)]
  56.         for dx, dy in ttt:
  57.             if min(dx, dy) > -1:
  58.                 try:
  59.                     neighbor_color = grid[dx][dy]
  60.                     difference = color_difference(current_color, neighbor_color)
  61.                     if difference < min_difference:
  62.                         min_difference = difference
  63.                         best_neighbor = (x2, y2)
  64.                         target = (dx, dy)
  65.                         reason = f"Selected cell ({x2}, {y2}) because it has the largest color difference with cell ({x}, {y})"
  66.                 except:
  67.                     0
  68.  
  69.     return best_neighbor, target, reason
  70.  
  71. def toggle_mode(event):
  72.     global delay
  73.     delay = not delay
  74. root.bind("<space>", toggle_mode)
  75.  
  76. def plot():
  77.     for y in range(GRID_SIZE):
  78.         for x in range(GRID_SIZE):
  79.             color = "#{:02x}{:02x}{:02x}".format(*grid[x][y])
  80.             canvas.create_oval(x * CELL_WIDTH, y * CELL_HEIGHT,
  81.                               (x + 1) * CELL_WIDTH, (y + 1) * CELL_HEIGHT,
  82.                               fill=color, outline="", tags=("colors"))
  83.  
  84. # Initialize the grid with random colors
  85. grid = [[COLORS.pop() for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
  86.  
  87. XYo = []
  88.  
  89. for y in range(GRID_SIZE):
  90.     for x in range(GRID_SIZE):
  91.         XYo += [(x, y)]
  92.  
  93. delay = 1
  94. plot()
  95. while 1:
  96.     random.shuffle(XYo)
  97.     for x, y in XYo:
  98.         best_neighbor, target, reason = find_best_neighbor(x, y)
  99.         if best_neighbor:
  100.             nx, ny = best_neighbor
  101.             dx, dy = target
  102.             grid[x][y], grid[nx][ny] = grid[nx][ny], grid[x][y]
  103.  
  104.             if delay:
  105.                 canvas.create_rectangle(dx * CELL_WIDTH + zz, dy * CELL_HEIGHT + zz,
  106.                                         (dx + 1) * CELL_WIDTH - zz, (dy + 1) * CELL_HEIGHT - zz,
  107.                                         fill="", outline="orange", width=3, tags=("swap"))
  108.                 canvas.create_rectangle(x * CELL_WIDTH + zz, y * CELL_HEIGHT + zz,
  109.                                         (x + 1) * CELL_WIDTH - zz, (y + 1) * CELL_HEIGHT - zz,
  110.                                         fill="", outline="red", width=3, tags=("swap"))
  111.                 canvas.create_rectangle(nx * CELL_WIDTH + zz, ny * CELL_HEIGHT + zz,
  112.                                         (nx + 1) * CELL_WIDTH - zz, (ny + 1) * CELL_HEIGHT - zz,
  113.                                         fill="", outline="green", width=3, tags=("swap"))
  114.                
  115.                 seconds(5)
  116.             canvas.delete("swap")
  117.             canvas.delete("colors")
  118.             plot()
  119.             seconds(1)
  120.  
  121. root.mainloop()
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement