Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

dither

By: mvanier on Dec 18th, 2013  |  syntax: Python  |  size: 2.22 KB  |  hits: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. '''
  2. dither.py: test program to simulate color dithering algorithms.
  3. '''
  4.  
  5. from Tkinter import *
  6. import random as r
  7.  
  8. # Number of squares in x and y dimensions.
  9. nx = 40
  10. ny = 40
  11.  
  12. # Size of square side in pixels.
  13. ss = 10
  14.  
  15. # Total dimensions of display.
  16. dx = nx * ss
  17. dy = ny * ss
  18.  
  19. # Colors (hi, lo)
  20. red   = ('ff', 'ee')
  21. green = ('ff', 'ee')
  22. blue  = ('ff', 'ee')
  23.  
  24. # Dithering pattern.
  25. dp = [0, 0, 0, 1]
  26.  
  27. root = Tk()
  28. root.geometry("%sx%s" % (dx, dy))
  29. c = Canvas(root, width=800, height=600)
  30. c.pack()
  31.  
  32. def make_color():
  33.     '''Make a color.  Red, green and blue components vary independently.'''
  34.     color_red   = red[r.choice(dp)]
  35.     color_green = green[r.choice(dp)]
  36.     color_blue  = blue[r.choice(dp)]
  37.     return '#' + color_red + color_green + color_blue
  38.  
  39. def make_color2():
  40.     '''Make a color.  Red, green and blue components vary together.'''
  41.     select = r.choice(dp)
  42.     color_red   = red[select]
  43.     color_green = green[select]
  44.     color_blue  = blue[select]
  45.     return '#' + color_red + color_green + color_blue
  46.  
  47. def make_square(x, y, color):
  48.     return c.create_rectangle(x * ss, y * ss, (x+1) * ss, (y+1) * ss,
  49.                               fill=color, outline=color)
  50.  
  51. def make_squares():
  52.     squares = []
  53.     for x in range(nx):
  54.         squares.append([])
  55.         for y in range(ny):
  56.             squares[x].append(make_square(x, y, make_color()))
  57.     return squares
  58.  
  59. squares = make_squares()
  60.  
  61. def change_colors(squares):
  62.     '''Change each square color independent of other squares.'''
  63.     for x in range(len(squares)):
  64.         for y in range(len(squares[x])):
  65.             h = squares[x][y]
  66.             color = make_color()
  67.             c.itemconfig(h, fill=color, outline=color)
  68.  
  69. def change_colors2(squares):
  70.     '''Change the color of all squares simultaneously.'''
  71.     color = make_color()
  72.     for x in range(len(squares)):
  73.         for y in range(len(squares[x])):
  74.             h = squares[x][y]
  75.             c.itemconfig(h, fill=color, outline=color)
  76.  
  77. done = False
  78. def quit_handler(event):
  79.     global done
  80.     done = True
  81.  
  82. # Left mouse button click on canvas terminates the program.
  83. root.bind('<Button-1>', quit_handler)
  84.  
  85. while not done:
  86.     change_colors(squares)
  87.     root.update()