Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 1 | """DetailsViewer class. |
| 2 | |
| 3 | This class implements a pure input window which allows you to meticulously |
| 4 | edit the current color. You have both mouse control of the color (via the |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 5 | buttons along the bottom row), and there are keyboard bindings for each of the |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 6 | increment/decrement buttons. |
| 7 | |
| 8 | The top three check buttons allow you to specify which of the three color |
| 9 | variations are tied together when incrementing and decrementing. Red, green, |
| 10 | and blue are self evident. By tying together red and green, you can modify |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 11 | the yellow level of the color. By tying together red and blue, you can modify |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 12 | the magenta level of the color. By tying together green and blue, you can |
| 13 | modify the cyan level, and by tying all three together, you can modify the |
| 14 | grey level. |
| 15 | |
| 16 | The behavior at the boundaries (0 and 255) are defined by the `At boundary' |
| 17 | option menu: |
| 18 | |
| 19 | Stop |
| 20 | When the increment or decrement would send any of the tied variations |
| 21 | out of bounds, the entire delta is discarded. |
| 22 | |
| 23 | Wrap Around |
| 24 | When the increment or decrement would send any of the tied variations |
| 25 | out of bounds, the out of bounds variation is wrapped around to the |
| 26 | other side. Thus if red were at 238 and 25 were added to it, red |
| 27 | would have the value 7. |
| 28 | |
| 29 | Preseve Distance |
| 30 | When the increment or decrement would send any of the tied variations |
| 31 | out of bounds, all tied variations are wrapped as one, so as to |
| 32 | preserve the distance between them. Thus if green and blue were tied, |
| 33 | and green was at 238 while blue was at 223, and an increment of 25 |
| 34 | were applied, green would be at 15 and blue would be at 0. |
| 35 | |
| 36 | Squash |
| 37 | When the increment or decrement would send any of the tied variations |
| 38 | out of bounds, the out of bounds variation is set to the ceiling of |
| 39 | 255 or floor of 0, as appropriate. In this way, all tied variations |
| 40 | are squashed to one edge or the other. |
| 41 | |
| 42 | The following key bindings can be used as accelerators. Note that Pynche can |
| 43 | fall behind if you hold the key down as a key repeat: |
| 44 | |
| 45 | Left arrow == -1 |
| 46 | Right arrow == +1 |
| 47 | |
| 48 | Control + Left == -10 |
| 49 | Control + Right == 10 |
| 50 | |
| 51 | Shift + Left == -25 |
| 52 | Shift + Right == +25 |
| 53 | """ |
| 54 | |
Georg Brandl | bf76ce1 | 2010-08-02 22:08:58 +0000 | [diff] [blame] | 55 | from tkinter import * |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 56 | |
| 57 | STOP = 'Stop' |
| 58 | WRAP = 'Wrap Around' |
| 59 | RATIO = 'Preserve Distance' |
| 60 | GRAV = 'Squash' |
| 61 | |
Barry Warsaw | f3ebf90 | 1999-04-27 18:54:12 +0000 | [diff] [blame] | 62 | ADDTOVIEW = 'Details Window...' |
| 63 | |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 64 | |
| 65 | class DetailsViewer: |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 66 | def __init__(self, switchboard, master=None): |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 67 | self.__sb = switchboard |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 68 | optiondb = switchboard.optiondb() |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 69 | self.__red, self.__green, self.__blue = switchboard.current_rgb() |
| 70 | # GUI |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 71 | root = self.__root = Toplevel(master, class_='Pynche') |
| 72 | root.protocol('WM_DELETE_WINDOW', self.withdraw) |
Barry Warsaw | 7c51a9a | 1998-10-05 21:31:37 +0000 | [diff] [blame] | 73 | root.title('Pynche Details Window') |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 74 | root.iconname('Pynche Details Window') |
| 75 | root.bind('<Alt-q>', self.__quit) |
| 76 | root.bind('<Alt-Q>', self.__quit) |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 77 | root.bind('<Alt-w>', self.withdraw) |
| 78 | root.bind('<Alt-W>', self.withdraw) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 79 | # accelerators |
| 80 | root.bind('<KeyPress-Left>', self.__minus1) |
| 81 | root.bind('<KeyPress-Right>', self.__plus1) |
| 82 | root.bind('<Control-KeyPress-Left>', self.__minus10) |
| 83 | root.bind('<Control-KeyPress-Right>', self.__plus10) |
| 84 | root.bind('<Shift-KeyPress-Left>', self.__minus25) |
| 85 | root.bind('<Shift-KeyPress-Right>', self.__plus25) |
| 86 | # |
| 87 | # color ties |
| 88 | frame = self.__frame = Frame(root) |
| 89 | frame.pack(expand=YES, fill=X) |
Barry Warsaw | 332aa4c | 1998-10-06 18:29:22 +0000 | [diff] [blame] | 90 | self.__l1 = Label(frame, text='Move Sliders:') |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 91 | self.__l1.grid(row=1, column=0, sticky=E) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 92 | self.__rvar = IntVar() |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 93 | self.__rvar.set(optiondb.get('RSLIDER', 4)) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 94 | self.__radio1 = Checkbutton(frame, text='Red', |
| 95 | variable=self.__rvar, |
| 96 | command=self.__effect, |
| 97 | onvalue=4, offvalue=0) |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 98 | self.__radio1.grid(row=1, column=1, sticky=W) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 99 | self.__gvar = IntVar() |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 100 | self.__gvar.set(optiondb.get('GSLIDER', 2)) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 101 | self.__radio2 = Checkbutton(frame, text='Green', |
| 102 | variable=self.__gvar, |
| 103 | command=self.__effect, |
| 104 | onvalue=2, offvalue=0) |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 105 | self.__radio2.grid(row=2, column=1, sticky=W) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 106 | self.__bvar = IntVar() |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 107 | self.__bvar.set(optiondb.get('BSLIDER', 1)) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 108 | self.__radio3 = Checkbutton(frame, text='Blue', |
| 109 | variable=self.__bvar, |
| 110 | command=self.__effect, |
| 111 | onvalue=1, offvalue=0) |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 112 | self.__radio3.grid(row=3, column=1, sticky=W) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 113 | self.__l2 = Label(frame) |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 114 | self.__l2.grid(row=4, column=1, sticky=W) |
Barry Warsaw | 7c51a9a | 1998-10-05 21:31:37 +0000 | [diff] [blame] | 115 | self.__effect() |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 116 | # |
| 117 | # Boundary behavior |
| 118 | self.__l3 = Label(frame, text='At boundary:') |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 119 | self.__l3.grid(row=5, column=0, sticky=E) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 120 | self.__boundvar = StringVar() |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 121 | self.__boundvar.set(optiondb.get('ATBOUND', STOP)) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 122 | self.__omenu = OptionMenu(frame, self.__boundvar, |
| 123 | STOP, WRAP, RATIO, GRAV) |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 124 | self.__omenu.grid(row=5, column=1, sticky=W) |
| 125 | self.__omenu.configure(width=17) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 126 | # |
Barry Warsaw | 332aa4c | 1998-10-06 18:29:22 +0000 | [diff] [blame] | 127 | # Buttons |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 128 | frame = self.__btnframe = Frame(frame) |
| 129 | frame.grid(row=0, column=0, columnspan=2, sticky='EW') |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 130 | self.__down25 = Button(frame, text='-25', |
| 131 | command=self.__minus25) |
| 132 | self.__down10 = Button(frame, text='-10', |
| 133 | command=self.__minus10) |
| 134 | self.__down1 = Button(frame, text='-1', |
| 135 | command=self.__minus1) |
| 136 | self.__up1 = Button(frame, text='+1', |
| 137 | command=self.__plus1) |
| 138 | self.__up10 = Button(frame, text='+10', |
| 139 | command=self.__plus10) |
| 140 | self.__up25 = Button(frame, text='+25', |
| 141 | command=self.__plus25) |
Barry Warsaw | c958043 | 1998-10-07 03:19:33 +0000 | [diff] [blame] | 142 | self.__down25.pack(expand=YES, fill=X, side=LEFT) |
| 143 | self.__down10.pack(expand=YES, fill=X, side=LEFT) |
| 144 | self.__down1.pack(expand=YES, fill=X, side=LEFT) |
| 145 | self.__up1.pack(expand=YES, fill=X, side=LEFT) |
| 146 | self.__up10.pack(expand=YES, fill=X, side=LEFT) |
| 147 | self.__up25.pack(expand=YES, fill=X, side=LEFT) |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 148 | |
| 149 | def __effect(self, event=None): |
| 150 | tie = self.__rvar.get() + self.__gvar.get() + self.__bvar.get() |
| 151 | if tie in (0, 1, 2, 4): |
| 152 | text = '' |
| 153 | else: |
Barry Warsaw | ae4ad6e | 1998-10-06 15:41:31 +0000 | [diff] [blame] | 154 | text = '(= %s Level)' % {3: 'Cyan', |
| 155 | 5: 'Magenta', |
| 156 | 6: 'Yellow', |
| 157 | 7: 'Grey'}[tie] |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 158 | self.__l2.configure(text=text) |
| 159 | |
| 160 | def __quit(self, event=None): |
Barry Warsaw | 45c8d34 | 1998-10-06 19:48:35 +0000 | [diff] [blame] | 161 | self.__root.quit() |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 162 | |
Barry Warsaw | ca07ba0 | 1998-10-22 03:25:59 +0000 | [diff] [blame] | 163 | def withdraw(self, event=None): |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 164 | self.__root.withdraw() |
| 165 | |
| 166 | def deiconify(self, event=None): |
| 167 | self.__root.deiconify() |
| 168 | |
| 169 | def __minus25(self, event=None): |
| 170 | self.__delta(-25) |
| 171 | |
| 172 | def __minus10(self, event=None): |
| 173 | self.__delta(-10) |
| 174 | |
| 175 | def __minus1(self, event=None): |
| 176 | self.__delta(-1) |
| 177 | |
| 178 | def __plus1(self, event=None): |
| 179 | self.__delta(1) |
| 180 | |
| 181 | def __plus10(self, event=None): |
| 182 | self.__delta(10) |
| 183 | |
| 184 | def __plus25(self, event=None): |
| 185 | self.__delta(25) |
| 186 | |
| 187 | def __delta(self, delta): |
| 188 | tie = [] |
| 189 | if self.__rvar.get(): |
| 190 | red = self.__red + delta |
| 191 | tie.append(red) |
| 192 | else: |
| 193 | red = self.__red |
| 194 | if self.__gvar.get(): |
| 195 | green = self.__green + delta |
| 196 | tie.append(green) |
| 197 | else: |
| 198 | green = self.__green |
| 199 | if self.__bvar.get(): |
| 200 | blue = self.__blue + delta |
| 201 | tie.append(blue) |
| 202 | else: |
| 203 | blue = self.__blue |
| 204 | # now apply at boundary behavior |
| 205 | atbound = self.__boundvar.get() |
| 206 | if atbound == STOP: |
| 207 | if red < 0 or green < 0 or blue < 0 or \ |
| 208 | red > 255 or green > 255 or blue > 255: |
| 209 | # then |
| 210 | red, green, blue = self.__red, self.__green, self.__blue |
| 211 | elif atbound == WRAP or (atbound == RATIO and len(tie) < 2): |
| 212 | if red < 0: |
Barry Warsaw | b4d8612 | 2001-07-10 21:39:18 +0000 | [diff] [blame] | 213 | red += 256 |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 214 | if green < 0: |
Barry Warsaw | b4d8612 | 2001-07-10 21:39:18 +0000 | [diff] [blame] | 215 | green += 256 |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 216 | if blue < 0: |
Barry Warsaw | b4d8612 | 2001-07-10 21:39:18 +0000 | [diff] [blame] | 217 | blue += 256 |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 218 | if red > 255: |
Barry Warsaw | b4d8612 | 2001-07-10 21:39:18 +0000 | [diff] [blame] | 219 | red -= 256 |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 220 | if green > 255: |
Barry Warsaw | b4d8612 | 2001-07-10 21:39:18 +0000 | [diff] [blame] | 221 | green -= 256 |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 222 | if blue > 255: |
Barry Warsaw | b4d8612 | 2001-07-10 21:39:18 +0000 | [diff] [blame] | 223 | blue -= 256 |
Barry Warsaw | c078b03 | 1998-10-05 21:29:04 +0000 | [diff] [blame] | 224 | elif atbound == RATIO: |
| 225 | # for when 2 or 3 colors are tied together |
| 226 | dir = 0 |
| 227 | for c in tie: |
| 228 | if c < 0: |
| 229 | dir = -1 |
| 230 | elif c > 255: |
| 231 | dir = 1 |
| 232 | if dir == -1: |
| 233 | delta = max(tie) |
| 234 | if self.__rvar.get(): |
| 235 | red = red + 255 - delta |
| 236 | if self.__gvar.get(): |
| 237 | green = green + 255 - delta |
| 238 | if self.__bvar.get(): |
| 239 | blue = blue + 255 - delta |
| 240 | elif dir == 1: |
| 241 | delta = min(tie) |
| 242 | if self.__rvar.get(): |
| 243 | red = red - delta |
| 244 | if self.__gvar.get(): |
| 245 | green = green - delta |
| 246 | if self.__bvar.get(): |
| 247 | blue = blue - delta |
| 248 | elif atbound == GRAV: |
| 249 | if red < 0: |
| 250 | red = 0 |
| 251 | if green < 0: |
| 252 | green = 0 |
| 253 | if blue < 0: |
| 254 | blue = 0 |
| 255 | if red > 255: |
| 256 | red = 255 |
| 257 | if green > 255: |
| 258 | green = 255 |
| 259 | if blue > 255: |
| 260 | blue = 255 |
| 261 | self.__sb.update_views(red, green, blue) |
| 262 | self.__root.update_idletasks() |
| 263 | |
| 264 | def update_yourself(self, red, green, blue): |
| 265 | self.__red = red |
| 266 | self.__green = green |
| 267 | self.__blue = blue |
Barry Warsaw | 8a09e1c | 1998-10-20 20:45:46 +0000 | [diff] [blame] | 268 | |
| 269 | def save_options(self, optiondb): |
| 270 | optiondb['RSLIDER'] = self.__rvar.get() |
| 271 | optiondb['GSLIDER'] = self.__gvar.get() |
| 272 | optiondb['BSLIDER'] = self.__bvar.get() |
| 273 | optiondb['ATBOUND'] = self.__boundvar.get() |