here2share

# tk_combo_gradients.py

Jul 19th, 2026 (edited)
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. # tk_combo_gradients.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageTk
  5. from itertools import combinations
  6.  
  7. WW, HH = 1200, 120
  8.  
  9. root = tk.Tk()
  10. root.geometry('+0+0')
  11. root.title("tk_combo_gradients (Click image to toggle resample)")
  12.  
  13. container = tk.Frame(root)
  14. container.pack(fill=tk.BOTH, expand=True)
  15.  
  16. canvas = tk.Canvas(container, width=WW, height=HH)
  17. canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
  18.  
  19. h_scrollbar = tk.Scrollbar(container, orient=tk.HORIZONTAL, command=canvas.xview)
  20. h_scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
  21. canvas.configure(xscrollcommand=h_scrollbar.set)
  22.  
  23. base = {
  24.     (0, 0): (255, 255, 255),    (0, 1): (255, 255,   0),    (0, 2): (0, 255,   0),
  25.     (1, 0): (255, 128,   0),    (1, 1): (128, 128, 128),    (1, 2): (0,   0, 255),
  26.     (2, 0): (255,   0,   0),    (2, 1): (128,   0, 128),    (2, 2): (0,   0,   0)
  27. }
  28.  
  29. colors = [(255, 192, 203), (165,  42,  42), (  0, 255, 255)]
  30.  
  31. colors = list(base.values()) + colors
  32.  
  33. pairs = list(combinations(colors, 2))
  34. combos = []
  35. used_pairs = set()
  36.  
  37. current_pair = pairs.pop(0)
  38. combos.extend(current_pair)
  39. used_pairs.add(current_pair)
  40.  
  41. while pairs:
  42.     last_color = combos[-1]
  43.     next_pair = None
  44.    
  45.     for p in pairs:
  46.         if p not in used_pairs:
  47.             if p[0] == last_color:
  48.                 next_pair = p
  49.                 rev = False
  50.                 break
  51.             elif p[1] == last_color:
  52.                 next_pair = p
  53.                 rev = True
  54.                 break
  55.                
  56.     if next_pair:
  57.         pairs.remove(next_pair)
  58.         used_pairs.add(next_pair)
  59.         if rev:
  60.             combos.append(next_pair[0])
  61.         else:
  62.             combos.append(next_pair[1])
  63.     else:
  64.         p = pairs.pop(0)
  65.         used_pairs.add(p)
  66.         combos.extend(p)
  67.  
  68. gradient_width = 50
  69. total_width = len(combos) * gradient_width
  70. total_height = 100
  71.  
  72. base_img = Image.new("RGB", (len(combos), total_height))
  73.  
  74. for i, color in enumerate(combos):
  75.     for y in range(total_height):
  76.         base_img.putpixel((i, y), color)
  77.  
  78. # State tracking for resampling mode
  79. resample_modes = [
  80.     ("LANCZOS", Image.LANCZOS),
  81.     ("NEAREST", Image.NEAREST)
  82. ]
  83. current_mode_index = 0
  84.  
  85. photo = None
  86. image_item = None
  87.  
  88. def update_image():
  89.     global photo, image_item, current_mode_index
  90.    
  91.     mode_name, mode_val = resample_modes[current_mode_index]
  92.    
  93.     # Resize using current mode
  94.     resized_img = base_img.resize((total_width, total_height), resample=mode_val)
  95.    
  96.     photo = ImageTk.PhotoImage(resized_img)
  97.    
  98.     if image_item is None:
  99.         image_item = canvas.create_image(0, 0, anchor=tk.NW, image=photo)
  100.     else:
  101.         canvas.itemconfig(image_item, image=photo)
  102.        
  103.     root.title(f"tk_combo_gradients - Resample: {mode_name} (Click image to toggle)")
  104.  
  105. def toggle_resample(event):
  106.     global current_mode_index
  107.     current_mode_index = (current_mode_index + 1) % len(resample_modes)
  108.     update_image()
  109.  
  110. update_image()
  111.  
  112. canvas.config(scrollregion=(0, 0, total_width, total_height))
  113.  
  114. canvas.bind("<Button-1>", toggle_resample)
  115.  
  116. def on_mouse_wheel(event):
  117.     canvas.xview_scroll(int(-1 * (event.delta / 120)), "units")
  118. canvas.bind_all("<MouseWheel>", on_mouse_wheel)
  119.  
  120. root.update()
  121. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment