/Tools/pynche/ChipViewer.py

http://unladen-swallow.googlecode.com/ · Python · 130 lines · 85 code · 14 blank · 31 comment · 4 complexity · ed64e655c18f5af237791a56a15e3418 MD5 · raw file

  1. """Chip viewer and widget.
  2. In the lower left corner of the main Pynche window, you will see two
  3. ChipWidgets, one for the selected color and one for the nearest color. The
  4. selected color is the actual RGB value expressed as an X11 #COLOR name. The
  5. nearest color is the named color from the X11 database that is closest to the
  6. selected color in 3D space. There may be other colors equally close, but the
  7. nearest one is the first one found.
  8. Clicking on the nearest color chip selects that named color.
  9. The ChipViewer class includes the entire lower left quandrant; i.e. both the
  10. selected and nearest ChipWidgets.
  11. """
  12. from Tkinter import *
  13. import ColorDB
  14. class ChipWidget:
  15. _WIDTH = 150
  16. _HEIGHT = 80
  17. def __init__(self,
  18. master = None,
  19. width = _WIDTH,
  20. height = _HEIGHT,
  21. text = 'Color',
  22. initialcolor = 'blue',
  23. presscmd = None,
  24. releasecmd = None):
  25. # create the text label
  26. self.__label = Label(master, text=text)
  27. self.__label.grid(row=0, column=0)
  28. # create the color chip, implemented as a frame
  29. self.__chip = Frame(master, relief=RAISED, borderwidth=2,
  30. width=width,
  31. height=height,
  32. background=initialcolor)
  33. self.__chip.grid(row=1, column=0)
  34. # create the color name
  35. self.__namevar = StringVar()
  36. self.__namevar.set(initialcolor)
  37. self.__name = Entry(master, textvariable=self.__namevar,
  38. relief=FLAT, justify=CENTER, state=DISABLED,
  39. font=self.__label['font'])
  40. self.__name.grid(row=2, column=0)
  41. # create the message area
  42. self.__msgvar = StringVar()
  43. self.__name = Entry(master, textvariable=self.__msgvar,
  44. relief=FLAT, justify=CENTER, state=DISABLED,
  45. font=self.__label['font'])
  46. self.__name.grid(row=3, column=0)
  47. # set bindings
  48. if presscmd:
  49. self.__chip.bind('<ButtonPress-1>', presscmd)
  50. if releasecmd:
  51. self.__chip.bind('<ButtonRelease-1>', releasecmd)
  52. def set_color(self, color):
  53. self.__chip.config(background=color)
  54. def get_color(self):
  55. return self.__chip['background']
  56. def set_name(self, colorname):
  57. self.__namevar.set(colorname)
  58. def set_message(self, message):
  59. self.__msgvar.set(message)
  60. def press(self):
  61. self.__chip.configure(relief=SUNKEN)
  62. def release(self):
  63. self.__chip.configure(relief=RAISED)
  64. class ChipViewer:
  65. def __init__(self, switchboard, master=None):
  66. self.__sb = switchboard
  67. self.__frame = Frame(master, relief=RAISED, borderwidth=1)
  68. self.__frame.grid(row=3, column=0, ipadx=5, sticky='NSEW')
  69. # create the chip that will display the currently selected color
  70. # exactly
  71. self.__sframe = Frame(self.__frame)
  72. self.__sframe.grid(row=0, column=0)
  73. self.__selected = ChipWidget(self.__sframe, text='Selected')
  74. # create the chip that will display the nearest real X11 color
  75. # database color name
  76. self.__nframe = Frame(self.__frame)
  77. self.__nframe.grid(row=0, column=1)
  78. self.__nearest = ChipWidget(self.__nframe, text='Nearest',
  79. presscmd = self.__buttonpress,
  80. releasecmd = self.__buttonrelease)
  81. def update_yourself(self, red, green, blue):
  82. # Selected always shows the #rrggbb name of the color, nearest always
  83. # shows the name of the nearest color in the database. BAW: should
  84. # an exact match be indicated in some way?
  85. #
  86. # Always use the #rrggbb style to actually set the color, since we may
  87. # not be using X color names (e.g. "web-safe" names)
  88. colordb = self.__sb.colordb()
  89. rgbtuple = (red, green, blue)
  90. rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple)
  91. # find the nearest
  92. nearest = colordb.nearest(red, green, blue)
  93. nearest_tuple = colordb.find_byname(nearest)
  94. nearest_rrggbb = ColorDB.triplet_to_rrggbb(nearest_tuple)
  95. self.__selected.set_color(rrggbb)
  96. self.__nearest.set_color(nearest_rrggbb)
  97. # set the name and messages areas
  98. self.__selected.set_name(rrggbb)
  99. if rrggbb == nearest_rrggbb:
  100. self.__selected.set_message(nearest)
  101. else:
  102. self.__selected.set_message('')
  103. self.__nearest.set_name(nearest_rrggbb)
  104. self.__nearest.set_message(nearest)
  105. def __buttonpress(self, event=None):
  106. self.__nearest.press()
  107. def __buttonrelease(self, event=None):
  108. self.__nearest.release()
  109. rrggbb = self.__nearest.get_color()
  110. red, green, blue = ColorDB.rrggbb_to_triplet(rrggbb)
  111. self.__sb.update_views(red, green, blue)