''' dither.py: test program to simulate color dithering algorithms. ''' from Tkinter import * import random as r # Number of squares in x and y dimensions. nx = 40 ny = 40 # Size of square side in pixels. ss = 10 # Total dimensions of display. dx = nx * ss dy = ny * ss # Colors (hi, lo) red = ('ff', 'ee') green = ('ff', 'ee') blue = ('ff', 'ee') # Dithering pattern. dp = [0, 0, 0, 1] root = Tk() root.geometry("%sx%s" % (dx, dy)) c = Canvas(root, width=800, height=600) c.pack() def make_color(): '''Make a color. Red, green and blue components vary independently.''' color_red = red[r.choice(dp)] color_green = green[r.choice(dp)] color_blue = blue[r.choice(dp)] return '#' + color_red + color_green + color_blue def make_color2(): '''Make a color. Red, green and blue components vary together.''' select = r.choice(dp) color_red = red[select] color_green = green[select] color_blue = blue[select] return '#' + color_red + color_green + color_blue def make_square(x, y, color): return c.create_rectangle(x * ss, y * ss, (x+1) * ss, (y+1) * ss, fill=color, outline=color) def make_squares(): squares = [] for x in range(nx): squares.append([]) for y in range(ny): squares[x].append(make_square(x, y, make_color())) return squares squares = make_squares() def change_colors(squares): '''Change each square color independent of other squares.''' for x in range(len(squares)): for y in range(len(squares[x])): h = squares[x][y] color = make_color() c.itemconfig(h, fill=color, outline=color) def change_colors2(squares): '''Change the color of all squares simultaneously.''' color = make_color() for x in range(len(squares)): for y in range(len(squares[x])): h = squares[x][y] c.itemconfig(h, fill=color, outline=color) done = False def quit_handler(event): global done done = True # Left mouse button click on canvas terminates the program. root.bind('', quit_handler) while not done: change_colors(squares) root.update()