Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_9_colors_interpolate.py
- import tkinter as tk
- from PIL import Image, ImageTk
- WW, HH = 600, 600
- root = tk.Tk()
- canvas = tk.Canvas(root, width=WW, height=HH)
- root.geometry("%dx%d+10+10" % (WW, HH))
- canvas.pack()
- image = Image.new("RGB", (WW, HH))
- def interpolate_color(color1, color2, factor):
- return tuple(int(color1[i] + (color2[i] - color1[i]) * factor) for i in range(3))
- grid_colors = {
- (0, 0): (255, 255, 255), # White
- (0, 1): (255, 255, 0), # Yellow
- (0, 2): (0, 255, 0), # Green
- (1, 0): (255, 165, 0), # Orange
- (1, 1): (128, 128, 128), # Gray (midpoint)
- (1, 2): (0, 0, 255), # Blue
- (2, 0): (255, 0, 0), # Red
- (2, 1): (128, 0, 128), # Purple
- (2, 2): (0, 0, 0) # Black
- }
- # Interpolate colors for each pixel
- for i in range(WW):
- for j in range(HH):
- x = i / (WW - 1) * 2
- y = j / (HH - 1) * 2
- x0, y0 = int(x), int(y)
- x1, y1 = min(x0 + 1, 2), min(y0 + 1, 2)
- fx, fy = x - x0, y - y0
- color_top = interpolate_color(grid_colors[(x0, y0)], grid_colors[(x1, y0)], fx)
- color_bottom = interpolate_color(grid_colors[(x0, y1)], grid_colors[(x1, y1)], fx)
- color = interpolate_color(color_top, color_bottom, fy)
- image.putpixel((i, j), color)
- gradient_photo = ImageTk.PhotoImage(image)
- canvas.create_image(0, 0, anchor=tk.NW, image=gradient_photo)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement