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 | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 9 | from StripWidget import StripWidget |
Barry Warsaw | a114eb5 | 1998-03-16 23:08:35 +0000 | [diff] [blame] | 10 | from OptionsWidget import PyncheOptions |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 11 | |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 12 | |
| 13 | |
| 14 | ABOUTTEXT = '''Pynche 1.0 -- The Pythonically Natural Color and Hue Editor |
| 15 | Copyright (C) 1998 Barry A. Warsaw |
| 16 | |
| 17 | Pynche is based on ICE 1.2 (Interactive Color Editor), written by |
| 18 | Barry A. Warsaw for the SunView window system in 1987.''' |
| 19 | |
| 20 | |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 21 | |
| 22 | def constant(numchips): |
| 23 | step = 255.0 / (numchips - 1) |
| 24 | start = 0.0 |
| 25 | seq = [] |
| 26 | while numchips > 0: |
| 27 | seq.append(int(start)) |
| 28 | start = start + step |
| 29 | numchips = numchips - 1 |
| 30 | return seq |
| 31 | |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 32 | # red variations, green+blue = cyan constant |
| 33 | def constant_cyan_generator(numchips, rgbtuple): |
| 34 | red, green, blue = rgbtuple |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 35 | seq = constant(numchips) |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 36 | return map(None, seq, [green] * numchips, [blue] * numchips) |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 37 | |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 38 | # green variations, red+blue = magenta constant |
| 39 | def constant_magenta_generator(numchips, rgbtuple): |
| 40 | red, green, blue = rgbtuple |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 41 | seq = constant(numchips) |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 42 | return map(None, [red] * numchips, seq, [blue] * numchips) |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 43 | |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 44 | # blue variations, red+green = yellow constant |
| 45 | def constant_yellow_generator(numchips, rgbtuple): |
| 46 | red, green, blue = rgbtuple |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 47 | seq = constant(numchips) |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 48 | return map(None, [red] * numchips, [green] * numchips, seq) |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 52 | class PyncheWidget(Pmw.MegaWidget): |
| 53 | def __init__(self, colordb, parent=None, **kw): |
| 54 | self.__colordb = colordb |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 55 | self.__parent = parent |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 56 | self.__about_dialog = None |
| 57 | self.__options_dialog = None |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 58 | |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 59 | options = (('color', (128, 128, 128), self.__set_color), |
| 60 | ('delegate', None, None), |
| 61 | ) |
| 62 | self.defineoptions(kw, options) |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 63 | |
| 64 | # initialize base class -- after defining options |
| 65 | Pmw.MegaWidget.__init__(self, parent) |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 66 | |
| 67 | # create menubar |
| 68 | self.__menubar = Pmw.MenuBar(parent, |
| 69 | hull_relief=RAISED, |
| 70 | hull_borderwidth=1) |
| 71 | self.__menubar.pack(side=TOP, expand=YES, fill=BOTH) |
| 72 | self.__menubar.addmenu('File', None) |
| 73 | self.__menubar.addmenuitem('File', |
| 74 | type=COMMAND, |
| 75 | label='Quit', |
| 76 | command=self.__quit, |
| 77 | accelerator='Alt-Q') |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 78 | parent.bind('<Alt-q>', self.__quit) |
| 79 | parent.bind('<Alt-Q>', self.__quit) |
| 80 | |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 81 | self.__menubar.addmenu('Help', None, side=RIGHT) |
| 82 | self.__menubar.addmenuitem('Help', |
| 83 | type=COMMAND, |
| 84 | label='About...', |
| 85 | command=self.__popup_about, |
| 86 | accelerator='Alt-A') |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 87 | parent.bind('<Alt-a>', self.__popup_about) |
| 88 | parent.bind('<Alt-A>', self.__popup_about) |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 89 | |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 90 | self.__menubar.addmenu('Edit', None) |
| 91 | self.__menubar.addmenuitem('Edit', |
| 92 | type=COMMAND, |
| 93 | label='Options...', |
| 94 | command=self.__popup_options, |
| 95 | accelerator='Alt-O') |
| 96 | parent.bind('<Alt-o>', self.__popup_options) |
| 97 | parent.bind('<Alt-O>', self.__popup_options) |
| 98 | |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 99 | # create color selectors |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 100 | group = Pmw.Group(parent, tag_text='Variations') |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 101 | group.pack(side=TOP, expand=YES, fill=BOTH) |
| 102 | self.__reds = StripWidget(group.interior(), |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 103 | generator=constant_cyan_generator, |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 104 | axis=0, label='Red Variations') |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 105 | self.__reds.pack() |
| 106 | self.__blues = StripWidget(group.interior(), |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 107 | generator=constant_magenta_generator, |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 108 | axis=1, label='Green Variations') |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 109 | self.__blues.pack() |
| 110 | self.__greens = StripWidget(group.interior(), |
Barry Warsaw | 79a7879 | 1998-02-13 21:28:14 +0000 | [diff] [blame] | 111 | generator=constant_yellow_generator, |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 112 | axis=2, label='Blue Variations') |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 113 | self.__greens.pack() |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 114 | |
| 115 | # create chip window |
| 116 | group = Pmw.Group(parent, tag_text='Current Color') |
Barry Warsaw | a114eb5 | 1998-03-16 23:08:35 +0000 | [diff] [blame] | 117 | interior = group.interior() |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 118 | group.pack(side=LEFT, expand=YES, fill=BOTH) |
Barry Warsaw | a114eb5 | 1998-03-16 23:08:35 +0000 | [diff] [blame] | 119 | self.__selected = ChipWidget(interior, |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 120 | label_text='Selected') |
| 121 | self.__selected.grid() |
Barry Warsaw | a114eb5 | 1998-03-16 23:08:35 +0000 | [diff] [blame] | 122 | self.__nearest = ChipWidget(interior, |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 123 | label_text='Nearest') |
| 124 | self.__nearest.grid(row=0, column=1) |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 125 | |
| 126 | # TBD: this is somewhat bogus, as the code should be in a derived |
| 127 | # class of ChipWidget. |
| 128 | self.__chip = self.__nearest.component('chip') |
| 129 | self.__chip.bind('<ButtonPress-1>', self.__buttonpress) |
| 130 | self.__chip.bind('<ButtonRelease-1>', self.__buttonrelease) |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 131 | |
Barry Warsaw | a114eb5 | 1998-03-16 23:08:35 +0000 | [diff] [blame] | 132 | # create the type-in window |
| 133 | self.__typein = TypeinWidget(interior) |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 134 | self.__typein.grid(row=0, column=2) |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 135 | |
| 136 | # Check keywords and initialize options |
| 137 | self.initialiseoptions(PyncheWidget) |
| 138 | |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 139 | self.__typein.configure(delegate=self) |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 140 | self.__reds.configure(delegate=self) |
| 141 | self.__greens.configure(delegate=self) |
| 142 | self.__blues.configure(delegate=self) |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 143 | |
| 144 | # |
| 145 | # PUBLIC INTERFACE |
| 146 | # |
| 147 | |
| 148 | |
| 149 | def set_color(self, obj, rgbtuple): |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 150 | nearest = self.__colordb.nearest(rgbtuple) |
Barry Warsaw | 55d3ffa | 1998-02-11 18:55:50 +0000 | [diff] [blame] | 151 | red, green, blue = self.__colordb.find_byname(nearest) |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 152 | # for an exact match, use the color name |
| 153 | if (red, green, blue) == rgbtuple: |
| 154 | self.__selected.configure(color=nearest) |
| 155 | # otherwise, use the #rrggbb name |
| 156 | else: |
| 157 | rrggbb = ColorDB.triplet_to_rrggbb(rgbtuple) |
| 158 | self.__selected.configure(color=rrggbb) |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 159 | |
| 160 | # configure all subwidgets |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 161 | self.__nearest.configure(color=nearest) |
| 162 | self.__typein.configure(color=rgbtuple) |
Barry Warsaw | 29fe4dd | 1998-02-12 19:52:31 +0000 | [diff] [blame] | 163 | self.__reds.configure(color=rgbtuple) |
| 164 | self.__greens.configure(color=rgbtuple) |
| 165 | self.__blues.configure(color=rgbtuple) |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 166 | delegate = self['delegate'] |
| 167 | if delegate: |
| 168 | delegate.set_color(self, rgbtuple) |
| 169 | |
| 170 | # |
| 171 | # PRIVATE INTERFACE |
| 172 | # |
Barry Warsaw | fefbf79 | 1998-01-31 23:39:01 +0000 | [diff] [blame] | 173 | |
| 174 | def __set_color(self): |
Barry Warsaw | a6f591a | 1998-02-11 17:19:54 +0000 | [diff] [blame] | 175 | self.set_color(self, self['color']) |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 176 | |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 177 | def __buttonpress(self, event=None): |
| 178 | self.__chip.configure(relief=SUNKEN) |
| 179 | |
| 180 | def __buttonrelease(self, event=None): |
| 181 | self.__chip.configure(relief=RAISED) |
Barry Warsaw | 93737b2 | 1998-02-17 03:09:19 +0000 | [diff] [blame] | 182 | color = self.__nearest['color'] |
| 183 | rgbtuple = self.__colordb.find_byname(color) |
| 184 | self.set_color(self, rgbtuple) |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 185 | |
| 186 | def __quit(self, event=None): |
| 187 | self.__parent.quit() |
| 188 | |
| 189 | def __popup_about(self, event=None): |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 190 | if not self.__about_dialog: |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 191 | Pmw.aboutversion('1.0') |
| 192 | Pmw.aboutcopyright('Copyright (C) 1998 Barry A. Warsaw\n' |
| 193 | 'All rights reserved') |
| 194 | Pmw.aboutcontact('For information about Pynche contact:\n' |
| 195 | 'Barry A. Warsaw\n' |
| 196 | 'email: bwarsaw@python.org') |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 197 | self.__about_dialog = Pmw.AboutDialog( |
Barry Warsaw | 552ac17 | 1998-02-17 22:25:23 +0000 | [diff] [blame] | 198 | applicationname='Pynche -- the PYthonically Natural\n' |
| 199 | 'Color and Hue Editor') |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 200 | self.__about_dialog.show() |
| 201 | |
| 202 | def __popup_options(self, event=None): |
| 203 | if not self.__options_dialog: |
Barry Warsaw | a114eb5 | 1998-03-16 23:08:35 +0000 | [diff] [blame] | 204 | self.__options_dialog = PyncheOptions( |
| 205 | title='Pynche Options', |
| 206 | applycommand=self.__apply) |
| 207 | # pop up the window, non-modal |
| 208 | self.__options_dialog.deiconify() |
| 209 | |
| 210 | def __apply(self): |
Barry Warsaw | f67575d | 1998-03-10 00:17:01 +0000 | [diff] [blame] | 211 | self.__typein.set_update_on_typing( |
| 212 | self.__options_dialog.get_value('typing')) |
Barry Warsaw | a114eb5 | 1998-03-16 23:08:35 +0000 | [diff] [blame] | 213 | flag = self.__options_dialog.get_value('dragging') |
| 214 | for strip in (self.__reds, self.__greens, self.__blues): |
| 215 | strip.set_update_while_dragging(flag) |