Advertisement
here2share

# tk_gradientify2.py

Aug 15th, 2024
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. # tk_gradientify2.py
  2.  
  3. import tkinter as tk
  4. import random
  5. import time
  6.  
  7. # Constants
  8. WIDTH, HEIGHT = 600, 600
  9. CELL_SIZE = 15
  10. GRID_SIZE = WIDTH // CELL_SIZE
  11. num_steps = GRID_SIZE**2
  12. step_size = (256**3) // (num_steps - 1)
  13.  
  14. root = tk.Tk()
  15. canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT)
  16. root.geometry("%dx%d+-10+0"%(WIDTH, HEIGHT))
  17. canvas.pack()
  18.  
  19. def convert_int_to_rgb(value):
  20.     b = value % 256
  21.     value //= 256
  22.     g = value % 256
  23.     value //= 256
  24.     r = value % 256
  25.     return (r, g, b)
  26.  
  27. COLORS = []
  28.  
  29. steps = 0
  30. while len(COLORS) < num_steps:
  31.     COLORS.append(convert_int_to_rgb(steps))
  32.     steps += step_size
  33. COLORS[-1] = (255, 255, 255)
  34.  
  35. def color_difference(color1, color2):
  36.     return sum(abs(a - b) for a, b in zip(color1, color2))
  37.  
  38. def find_best_neighbor(x, y):
  39.     current_color = grid[x][y]
  40.     best_neighbor = None
  41.     target = (x, y)
  42.     min_difference = float('inf')
  43.     reason = ""
  44.  
  45.     for x0, y0 in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
  46.         x2, y2 = x0 + x, y0 + y
  47.         ttt = [((x2 + j * x0 if x0 else x + i), (y2 + j * y0 if y0 else y + i)) for i in (-1, 0, 1) for j in [1, 2, 3]]
  48.         for dx, dy in ttt:
  49.             if min(dx, dy) > -1:
  50.                 try:
  51.                     neighbor_color = grid[dx][dy]
  52.                     difference = color_difference(current_color, neighbor_color)
  53.                     if difference < min_difference:
  54.                         min_difference = difference
  55.                         best_neighbor = (x2, y2)
  56.                         target = (dx, dy)
  57.                 except:
  58.                     0
  59.  
  60.     return best_neighbor, target, reason
  61.  
  62. def plot():
  63.     for y in range(GRID_SIZE):
  64.         for x in range(GRID_SIZE):
  65.             color = "#{:02x}{:02x}{:02x}".format(*grid[x][y])
  66.             canvas.create_rectangle(x * CELL_SIZE, y * CELL_SIZE,
  67.                               (x + 1) * CELL_SIZE, (y + 1) * CELL_SIZE,
  68.                               fill=color, outline="", tags=("colors"))
  69.  
  70. # Initialize the grid with random colors
  71. grid = [[COLORS.pop() for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
  72.  
  73. XYo = []
  74.  
  75. for y in range(GRID_SIZE):
  76.     for x in range(GRID_SIZE):
  77.         XYo += [(x, y)]
  78.  
  79. delay = 1
  80. plot()
  81. while 1:
  82.     random.shuffle(XYo)
  83.     for x, y in XYo:
  84.         best_neighbor, target, reason = find_best_neighbor(x, y)
  85.         if best_neighbor:
  86.             nx, ny = best_neighbor
  87.             dx, dy = target
  88.             grid[x][y], grid[nx][ny] = grid[nx][ny], grid[x][y]
  89.             canvas.delete("swap")
  90.             canvas.delete("colors")
  91.             plot()
  92.             canvas.update()
  93.  
  94. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement