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