Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 1 | """Main Pynche (Pythonically Natural Color and Hue Editor) widget. |
| 2 | """ |
| 3 | |
| 4 | from Tkinter import * |
| 5 | import Pmw |
| 6 | import ColorDB |
| 7 | from ChipWidget import ChipWidget |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame^] | 8 | from TypeinWidget import TypeinWidget |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 9 | |
| 10 | class PyncheWidget(Pmw.MegaWidget): |
| 11 | def __init__(self, colordb, parent=None, **kw): |
| 12 | self.__colordb = colordb |
| 13 | |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame^] | 14 | options = (('color', (128, 128, 128), self.__set_color), |
| 15 | ('delegate', None, None), |
| 16 | ) |
| 17 | self.defineoptions(kw, options) |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 18 | |
| 19 | # initialize base class -- after defining options |
| 20 | Pmw.MegaWidget.__init__(self, parent) |
| 21 | interiorarg = (self.interior(),) |
| 22 | |
| 23 | # create color selectors |
| 24 | self.__selectors = Pmw.Group(parent, tag_text='Color Selectors') |
| 25 | self.__selectors.pack(side=TOP, expand=YES, fill=BOTH) |
| 26 | |
| 27 | # create chip window |
| 28 | group = Pmw.Group(parent, tag_text='Current Color') |
| 29 | group.pack(side=LEFT, fill=Y) |
| 30 | self.__selected = ChipWidget(group.interior(), |
| 31 | label_text='Selected') |
| 32 | self.__selected.grid() |
| 33 | self.__nearest = ChipWidget(group.interior(), |
| 34 | label_text='Nearest') |
| 35 | self.__nearest.grid(row=0, column=1) |
| 36 | |
| 37 | # create the options window |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame^] | 38 | group = Pmw.Group(parent, tag_text='Options') |
| 39 | group.pack(expand=YES, fill=BOTH) |
| 40 | self.__typein = TypeinWidget(group.interior()) |
| 41 | self.__typein.grid() |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 42 | |
| 43 | # Check keywords and initialize options |
| 44 | self.initialiseoptions(PyncheWidget) |
| 45 | |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame^] | 46 | self.__typein.configure(delegate=self) |
| 47 | |
| 48 | # |
| 49 | # PUBLIC INTERFACE |
| 50 | # |
| 51 | |
| 52 | |
| 53 | def set_color(self, obj, rgbtuple): |
| 54 | print 'setting color to:', rgbtuple |
| 55 | nearest = self.__colordb.nearest(rgbtuple) |
| 56 | red, green, blue, rrggbb = self.__colordb.find_byname(nearest) |
| 57 | # for an exact match, use the color name |
| 58 | if (red, green, blue) == rgbtuple: |
| 59 | self.__selected.configure(color=nearest) |
| 60 | # otherwise, use the #rrggbb name |
| 61 | else: |
| 62 | rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple) |
| 63 | self.__selected.configure(color=rrggbb) |
| 64 | self.__nearest.configure(color=nearest) |
| 65 | self.__typein.configure(color=rgbtuple) |
| 66 | delegate = self['delegate'] |
| 67 | if delegate: |
| 68 | delegate.set_color(self, rgbtuple) |
| 69 | |
| 70 | # |
| 71 | # PRIVATE INTERFACE |
| 72 | # |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 73 | |
| 74 | def __set_color(self): |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame^] | 75 | self.set_color(self, self['color']) |