author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 438 | 2ae294e4518c |
child 1299 | 027d966d5658 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
package com.sun.java.swing.plaf.gtk; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.event.*; |
|
29 |
import java.awt.image.*; |
|
30 |
import javax.swing.*; |
|
31 |
import javax.swing.colorchooser.*; |
|
32 |
import javax.swing.event.*; |
|
33 |
import javax.swing.plaf.*; |
|
34 |
||
35 |
/** |
|
36 |
* A color chooser panel mimicking that of GTK's: a color wheel showing |
|
37 |
* hue and a triangle that varies saturation and brightness. |
|
38 |
* |
|
39 |
* @author Scott Violet |
|
40 |
*/ |
|
41 |
class GTKColorChooserPanel extends AbstractColorChooserPanel implements |
|
42 |
ChangeListener { |
|
43 |
private static final float PI_3 = (float)(Math.PI / 3); |
|
44 |
||
45 |
private ColorTriangle triangle; |
|
46 |
private JLabel lastLabel; |
|
47 |
private JLabel label; |
|
48 |
||
49 |
private JSpinner hueSpinner; |
|
50 |
private JSpinner saturationSpinner; |
|
51 |
private JSpinner valueSpinner; |
|
52 |
||
53 |
private JSpinner redSpinner; |
|
54 |
private JSpinner greenSpinner; |
|
55 |
private JSpinner blueSpinner; |
|
56 |
||
57 |
private JTextField colorNameTF; |
|
58 |
||
59 |
private boolean settingColor; |
|
60 |
||
61 |
// The colors are mirrored to avoid creep in adjusting an individual |
|
62 |
// value. |
|
63 |
private float hue; |
|
64 |
private float saturation; |
|
65 |
private float brightness; |
|
66 |
||
67 |
||
68 |
||
69 |
/** |
|
70 |
* Convenience method to transfer focus to the next child of component. |
|
71 |
*/ |
|
72 |
// PENDING: remove this when a variant of this is added to awt. |
|
73 |
static void compositeRequestFocus(Component component, boolean direction) { |
|
74 |
if (component instanceof Container) { |
|
75 |
Container container = (Container)component; |
|
76 |
if (container.isFocusCycleRoot()) { |
|
77 |
FocusTraversalPolicy policy = container. |
|
78 |
getFocusTraversalPolicy(); |
|
79 |
Component comp = policy.getDefaultComponent(container); |
|
80 |
if (comp!=null) { |
|
81 |
comp.requestFocus(); |
|
82 |
return; |
|
83 |
} |
|
84 |
} |
|
85 |
Container rootAncestor = container.getFocusCycleRootAncestor(); |
|
86 |
if (rootAncestor!=null) { |
|
87 |
FocusTraversalPolicy policy = rootAncestor. |
|
88 |
getFocusTraversalPolicy(); |
|
89 |
Component comp; |
|
90 |
||
91 |
if (direction) { |
|
92 |
comp = policy.getComponentAfter(rootAncestor, container); |
|
93 |
} |
|
94 |
else { |
|
95 |
comp = policy.getComponentBefore(rootAncestor, container); |
|
96 |
} |
|
97 |
if (comp != null) { |
|
98 |
comp.requestFocus(); |
|
99 |
return; |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |
component.requestFocus(); |
|
104 |
} |
|
105 |
||
106 |
||
107 |
/** |
|
108 |
* Returns a user presentable description of this GTKColorChooserPane. |
|
109 |
*/ |
|
110 |
public String getDisplayName() { |
|
111 |
return (String)UIManager.get("GTKColorChooserPanel.nameText"); |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* Returns the mnemonic to use with <code>getDisplayName</code>. |
|
116 |
*/ |
|
117 |
public int getMnemonic() { |
|
118 |
String m = (String)UIManager.get("GTKColorChooserPanel.mnemonic"); |
|
119 |
||
120 |
if (m != null) { |
|
121 |
try { |
|
122 |
int value = Integer.parseInt(m); |
|
123 |
||
124 |
return value; |
|
125 |
} catch (NumberFormatException nfe) {} |
|
126 |
} |
|
127 |
return -1; |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Character to underline that represents the mnemonic. |
|
132 |
*/ |
|
133 |
public int getDisplayedMnemonicIndex() { |
|
134 |
String m = (String)UIManager.get( |
|
135 |
"GTKColorChooserPanel.displayedMnemonicIndex"); |
|
136 |
||
137 |
if (m != null) { |
|
138 |
try { |
|
139 |
int value = Integer.parseInt(m); |
|
140 |
||
141 |
return value; |
|
142 |
} catch (NumberFormatException nfe) {} |
|
143 |
} |
|
144 |
return -1; |
|
145 |
} |
|
146 |
||
147 |
public Icon getSmallDisplayIcon() { |
|
148 |
return null; |
|
149 |
} |
|
150 |
||
151 |
public Icon getLargeDisplayIcon() { |
|
152 |
return null; |
|
153 |
} |
|
154 |
||
155 |
public void uninstallChooserPanel(JColorChooser enclosingChooser) { |
|
156 |
super.uninstallChooserPanel(enclosingChooser); |
|
157 |
removeAll(); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Builds and configures the widgets for the GTKColorChooserPanel. |
|
162 |
*/ |
|
163 |
protected void buildChooser() { |
|
164 |
triangle = new ColorTriangle(); |
|
165 |
triangle.setName("GTKColorChooserPanel.triangle"); |
|
166 |
||
167 |
// PENDING: when we straighten out user setting opacity, this should |
|
168 |
// be changed. |
|
169 |
label = new OpaqueLabel(); |
|
170 |
label.setName("GTKColorChooserPanel.colorWell"); |
|
171 |
label.setOpaque(true); |
|
172 |
label.setMinimumSize(new Dimension(67, 32)); |
|
173 |
label.setPreferredSize(new Dimension(67, 32)); |
|
174 |
label.setMaximumSize(new Dimension(67, 32)); |
|
175 |
||
176 |
// PENDING: when we straighten out user setting opacity, this should |
|
177 |
// be changed. |
|
178 |
lastLabel = new OpaqueLabel(); |
|
179 |
lastLabel.setName("GTKColorChooserPanel.lastColorWell"); |
|
180 |
lastLabel.setOpaque(true); |
|
181 |
lastLabel.setMinimumSize(new Dimension(67, 32)); |
|
182 |
lastLabel.setPreferredSize(new Dimension(67, 32)); |
|
183 |
lastLabel.setMaximumSize(new Dimension(67, 32)); |
|
184 |
||
185 |
hueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 360, 1)); |
|
186 |
configureSpinner(hueSpinner, "GTKColorChooserPanel.hueSpinner"); |
|
187 |
saturationSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1)); |
|
188 |
configureSpinner(saturationSpinner, |
|
189 |
"GTKColorChooserPanel.saturationSpinner"); |
|
190 |
valueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1)); |
|
191 |
configureSpinner(valueSpinner, "GTKColorChooserPanel.valueSpinner"); |
|
192 |
redSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1)); |
|
193 |
configureSpinner(redSpinner, "GTKColorChooserPanel.redSpinner"); |
|
194 |
greenSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1)); |
|
195 |
configureSpinner(greenSpinner, "GTKColorChooserPanel.greenSpinner"); |
|
196 |
blueSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 255, 1)); |
|
197 |
configureSpinner(blueSpinner, "GTKColorChooserPanel.blueSpinner"); |
|
198 |
||
199 |
colorNameTF = new JTextField(8); |
|
200 |
||
201 |
setLayout(new GridBagLayout()); |
|
202 |
||
203 |
add(this, "GTKColorChooserPanel.hue", hueSpinner, -1, -1); |
|
204 |
add(this, "GTKColorChooserPanel.red", redSpinner, -1, -1); |
|
205 |
add(this, "GTKColorChooserPanel.saturation", saturationSpinner, -1,-1); |
|
206 |
add(this, "GTKColorChooserPanel.green", greenSpinner, -1, -1); |
|
207 |
add(this, "GTKColorChooserPanel.value", valueSpinner, -1, -1); |
|
208 |
add(this, "GTKColorChooserPanel.blue", blueSpinner, -1, -1); |
|
209 |
||
210 |
add(new JSeparator(SwingConstants.HORIZONTAL), new |
|
211 |
GridBagConstraints(1, 3, 4, 1, 1, 0, |
|
212 |
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, |
|
213 |
new Insets(14, 0, 0, 0), 0, 0)); |
|
214 |
||
215 |
add(this, "GTKColorChooserPanel.colorName", colorNameTF, 0, 4); |
|
216 |
||
217 |
add(triangle, new GridBagConstraints(0, 0, 1, 5, 0, 0, |
|
218 |
GridBagConstraints.LINE_START, GridBagConstraints.NONE, |
|
219 |
new Insets(14, 20, 2, 9), 0, 0)); |
|
220 |
||
221 |
Box hBox = Box.createHorizontalBox(); |
|
222 |
hBox.add(lastLabel); |
|
223 |
hBox.add(label); |
|
224 |
add(hBox, new GridBagConstraints(0, 5, 1, 1, 0, 0, |
|
225 |
GridBagConstraints.CENTER, GridBagConstraints.NONE, |
|
226 |
new Insets(0, 0, 0, 0), 0, 0)); |
|
227 |
||
228 |
add(new JSeparator(SwingConstants.HORIZONTAL), new |
|
229 |
GridBagConstraints(0, 6, 5, 1, 1, 0, |
|
230 |
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL, |
|
231 |
new Insets(12, 0, 0, 0), 0, 0)); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Configures the spinner. |
|
236 |
*/ |
|
237 |
private void configureSpinner(JSpinner spinner, String name) { |
|
238 |
spinner.addChangeListener(this); |
|
239 |
spinner.setName(name); |
|
240 |
JComponent editor = spinner.getEditor(); |
|
241 |
if (editor instanceof JSpinner.DefaultEditor) { |
|
242 |
JFormattedTextField ftf = ((JSpinner.DefaultEditor)editor). |
|
243 |
getTextField(); |
|
244 |
||
245 |
ftf.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT); |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Adds the widget creating a JLabel with the specified name. |
|
251 |
*/ |
|
252 |
private void add(Container parent, String key, JComponent widget, |
|
253 |
int x, int y) { |
|
254 |
JLabel label = new JLabel(UIManager.getString(key + "Text", |
|
255 |
getLocale())); |
|
256 |
String mnemonic = (String)UIManager.get(key + "Mnemonic", getLocale()); |
|
257 |
||
258 |
if (mnemonic != null) { |
|
259 |
try { |
|
260 |
label.setDisplayedMnemonic(Integer.parseInt(mnemonic)); |
|
261 |
} catch (NumberFormatException nfe) { |
|
262 |
} |
|
263 |
String mnemonicIndex = (String)UIManager.get(key + "MnemonicIndex", |
|
264 |
getLocale()); |
|
265 |
||
266 |
if (mnemonicIndex != null) { |
|
267 |
try { |
|
268 |
label.setDisplayedMnemonicIndex(Integer.parseInt( |
|
269 |
mnemonicIndex)); |
|
270 |
} catch (NumberFormatException nfe) { |
|
271 |
} |
|
272 |
} |
|
273 |
} |
|
274 |
label.setLabelFor(widget); |
|
275 |
if (x < 0) { |
|
276 |
x = parent.getComponentCount() % 4; |
|
277 |
} |
|
278 |
if (y < 0) { |
|
279 |
y = parent.getComponentCount() / 4; |
|
280 |
} |
|
281 |
GridBagConstraints con = new GridBagConstraints(x + 1, y, 1, 1, 0, 0, |
|
282 |
GridBagConstraints.FIRST_LINE_END, GridBagConstraints.NONE, |
|
283 |
new Insets(4, 0, 0, 4), 0, 0); |
|
284 |
if (y == 0) { |
|
285 |
con.insets.top = 14; |
|
286 |
} |
|
287 |
parent.add(label, con); |
|
288 |
con.gridx++; |
|
289 |
parent.add(widget, con); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Refreshes the display from the model. |
|
294 |
*/ |
|
295 |
public void updateChooser() { |
|
296 |
if (!settingColor) { |
|
297 |
lastLabel.setBackground(getColorFromModel()); |
|
298 |
setColor(getColorFromModel(), true, true, false); |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Resets the red component of the selected color. |
|
304 |
*/ |
|
305 |
private void setRed(int red) { |
|
306 |
setRGB(red << 16 | getColor().getGreen() << 8 | getColor().getBlue()); |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Resets the green component of the selected color. |
|
311 |
*/ |
|
312 |
private void setGreen(int green) { |
|
313 |
setRGB(getColor().getRed() << 16 | green << 8 | getColor().getBlue()); |
|
314 |
} |
|
315 |
||
316 |
/** |
|
317 |
* Resets the blue component of the selected color. |
|
318 |
*/ |
|
319 |
private void setBlue(int blue) { |
|
320 |
setRGB(getColor().getRed() << 16 | getColor().getGreen() << 8 | blue); |
|
321 |
} |
|
322 |
||
323 |
/** |
|
324 |
* Sets the hue of the selected color and updates the display if |
|
325 |
* necessary. |
|
326 |
*/ |
|
327 |
private void setHue(float hue, boolean update) { |
|
328 |
setHSB(hue, saturation, brightness); |
|
329 |
if (update) { |
|
330 |
settingColor = true; |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
331 |
hueSpinner.setValue(Integer.valueOf((int)(hue * 360))); |
2 | 332 |
settingColor = false; |
333 |
} |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Returns the current amount of hue. |
|
338 |
*/ |
|
339 |
private float getHue() { |
|
340 |
return hue; |
|
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* Resets the saturation. |
|
345 |
*/ |
|
346 |
private void setSaturation(float saturation) { |
|
347 |
setHSB(hue, saturation, brightness); |
|
348 |
} |
|
349 |
||
350 |
/** |
|
351 |
* Returns the saturation. |
|
352 |
*/ |
|
353 |
private float getSaturation() { |
|
354 |
return saturation; |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Sets the brightness. |
|
359 |
*/ |
|
360 |
private void setBrightness(float brightness) { |
|
361 |
setHSB(hue, saturation, brightness); |
|
362 |
} |
|
363 |
||
364 |
/** |
|
365 |
* Returns the brightness. |
|
366 |
*/ |
|
367 |
private float getBrightness() { |
|
368 |
return brightness; |
|
369 |
} |
|
370 |
||
371 |
/** |
|
372 |
* Sets the saturation and brightness and updates the display if |
|
373 |
* necessary. |
|
374 |
*/ |
|
375 |
private void setSaturationAndBrightness(float s, float b, boolean update) { |
|
376 |
setHSB(hue, s, b); |
|
377 |
if (update) { |
|
378 |
settingColor = true; |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
379 |
saturationSpinner.setValue(Integer.valueOf((int)(s * 255))); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
380 |
valueSpinner.setValue(Integer.valueOf((int)(b * 255))); |
2 | 381 |
settingColor = false; |
382 |
} |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Resets the rgb values. |
|
387 |
*/ |
|
388 |
private void setRGB(int rgb) { |
|
389 |
Color color = new Color(rgb); |
|
390 |
||
391 |
setColor(color, false, true, true); |
|
392 |
||
393 |
settingColor = true; |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
394 |
hueSpinner.setValue(Integer.valueOf((int)(hue * 360))); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
395 |
saturationSpinner.setValue(Integer.valueOf((int)(saturation * 255))); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
396 |
valueSpinner.setValue(Integer.valueOf((int)(brightness * 255))); |
2 | 397 |
settingColor = false; |
398 |
} |
|
399 |
||
400 |
/** |
|
401 |
* Resets the hsb values. |
|
402 |
*/ |
|
403 |
private void setHSB(float h, float s, float b) { |
|
404 |
Color color = Color.getHSBColor(h, s, b); |
|
405 |
||
406 |
this.hue = h; |
|
407 |
this.saturation = s; |
|
408 |
this.brightness = b; |
|
409 |
setColor(color, false, false, true); |
|
410 |
||
411 |
settingColor = true; |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
412 |
redSpinner.setValue(Integer.valueOf(color.getRed())); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
413 |
greenSpinner.setValue(Integer.valueOf(color.getGreen())); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
414 |
blueSpinner.setValue(Integer.valueOf(color.getBlue())); |
2 | 415 |
settingColor = false; |
416 |
} |
|
417 |
||
418 |
||
419 |
/** |
|
420 |
* Rests the color. |
|
421 |
* |
|
422 |
* @param color new Color |
|
423 |
* @param updateSpinners whether or not to update the spinners. |
|
424 |
* @param updateHSB if true, the hsb fields are updated based on the |
|
425 |
* new color |
|
426 |
* @param updateModel if true, the model is set. |
|
427 |
*/ |
|
428 |
private void setColor(Color color, boolean updateSpinners, |
|
429 |
boolean updateHSB, boolean updateModel) { |
|
430 |
if (color == null) { |
|
431 |
color = Color.BLACK; |
|
432 |
} |
|
433 |
||
434 |
settingColor = true; |
|
435 |
||
436 |
if (updateHSB) { |
|
437 |
float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(), |
|
438 |
color.getBlue(), null); |
|
439 |
hue = hsb[0]; |
|
440 |
saturation = hsb[1]; |
|
441 |
brightness = hsb[2]; |
|
442 |
} |
|
443 |
||
444 |
if (updateModel) { |
|
445 |
getColorSelectionModel().setSelectedColor(color); |
|
446 |
} |
|
447 |
||
448 |
triangle.setColor(hue, saturation, brightness); |
|
449 |
label.setBackground(color); |
|
450 |
// Force Integer to pad the string with 0's by adding 0x1000000 and |
|
451 |
// then removing the first character. |
|
452 |
String hexString = Integer.toHexString( |
|
453 |
(color.getRGB() & 0xFFFFFF) | 0x1000000); |
|
454 |
colorNameTF.setText("#" + hexString.substring(1)); |
|
455 |
||
456 |
if (updateSpinners) { |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
457 |
redSpinner.setValue(Integer.valueOf(color.getRed())); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
458 |
greenSpinner.setValue(Integer.valueOf(color.getGreen())); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
459 |
blueSpinner.setValue(Integer.valueOf(color.getBlue())); |
2 | 460 |
|
438
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
461 |
hueSpinner.setValue(Integer.valueOf((int)(hue * 360))); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
462 |
saturationSpinner.setValue(Integer.valueOf((int)(saturation * 255))); |
2ae294e4518c
6613529: Avoid duplicate object creation within JDK packages
dav
parents:
2
diff
changeset
|
463 |
valueSpinner.setValue(Integer.valueOf((int)(brightness * 255))); |
2 | 464 |
} |
465 |
settingColor = false; |
|
466 |
} |
|
467 |
||
468 |
public Color getColor() { |
|
469 |
return label.getBackground(); |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* ChangeListener method, updates the necessary display widgets. |
|
474 |
*/ |
|
475 |
public void stateChanged(ChangeEvent e) { |
|
476 |
if (settingColor) { |
|
477 |
return; |
|
478 |
} |
|
479 |
Color color = getColor(); |
|
480 |
||
481 |
if (e.getSource() == hueSpinner) { |
|
482 |
setHue(((Number)hueSpinner.getValue()).floatValue() / 360, false); |
|
483 |
} |
|
484 |
else if (e.getSource() == saturationSpinner) { |
|
485 |
setSaturation(((Number)saturationSpinner.getValue()). |
|
486 |
floatValue() / 255); |
|
487 |
} |
|
488 |
else if (e.getSource() == valueSpinner) { |
|
489 |
setBrightness(((Number)valueSpinner.getValue()). |
|
490 |
floatValue() / 255); |
|
491 |
} |
|
492 |
else if (e.getSource() == redSpinner) { |
|
493 |
setRed(((Number)redSpinner.getValue()).intValue()); |
|
494 |
} |
|
495 |
else if (e.getSource() == greenSpinner) { |
|
496 |
setGreen(((Number)greenSpinner.getValue()).intValue()); |
|
497 |
} |
|
498 |
else if (e.getSource() == blueSpinner) { |
|
499 |
setBlue(((Number)blueSpinner.getValue()).intValue()); |
|
500 |
} |
|
501 |
} |
|
502 |
||
503 |
||
504 |
||
505 |
/** |
|
506 |
* Flag indicating the angle, or hue, has changed and the triangle |
|
507 |
* needs to be recreated. |
|
508 |
*/ |
|
509 |
private static final int FLAGS_CHANGED_ANGLE = 1 << 0; |
|
510 |
/** |
|
511 |
* Indicates the wheel is being dragged. |
|
512 |
*/ |
|
513 |
private static final int FLAGS_DRAGGING = 1 << 1; |
|
514 |
/** |
|
515 |
* Indicates the triangle is being dragged. |
|
516 |
*/ |
|
517 |
private static final int FLAGS_DRAGGING_TRIANGLE = 1 << 2; |
|
518 |
/** |
|
519 |
* Indicates a color is being set and we should ignore setColor |
|
520 |
*/ |
|
521 |
private static final int FLAGS_SETTING_COLOR = 1 << 3; |
|
522 |
/** |
|
523 |
* Indicates the wheel has focus. |
|
524 |
*/ |
|
525 |
private static final int FLAGS_FOCUSED_WHEEL = 1 << 4; |
|
526 |
/** |
|
527 |
* Indicates the triangle has focus. |
|
528 |
*/ |
|
529 |
private static final int FLAGS_FOCUSED_TRIANGLE = 1 << 5; |
|
530 |
||
531 |
||
532 |
/** |
|
533 |
* Class responsible for rendering a color wheel and color triangle. |
|
534 |
*/ |
|
535 |
private class ColorTriangle extends JPanel { |
|
536 |
/** |
|
537 |
* Cached image of the wheel. |
|
538 |
*/ |
|
539 |
private Image wheelImage; |
|
540 |
||
541 |
/** |
|
542 |
* Cached image of the triangle. |
|
543 |
*/ |
|
544 |
private Image triangleImage; |
|
545 |
||
546 |
/** |
|
547 |
* Angle triangle is rotated by. |
|
548 |
*/ |
|
549 |
private double angle; |
|
550 |
||
551 |
/** |
|
552 |
* Boolean bitmask. |
|
553 |
*/ |
|
554 |
private int flags; |
|
555 |
||
556 |
/** |
|
557 |
* X location of selected color indicator. |
|
558 |
*/ |
|
559 |
private int circleX; |
|
560 |
/** |
|
561 |
* Y location of selected color indicator. |
|
562 |
*/ |
|
563 |
private int circleY; |
|
564 |
||
565 |
||
566 |
public ColorTriangle() { |
|
567 |
enableEvents(AWTEvent.FOCUS_EVENT_MASK); |
|
568 |
enableEvents(AWTEvent.MOUSE_EVENT_MASK); |
|
569 |
enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); |
|
570 |
||
571 |
setMinimumSize(new Dimension(getWheelRadius() * 2 + 2, |
|
572 |
getWheelRadius() * 2 + 2)); |
|
573 |
setPreferredSize(new Dimension(getWheelRadius() * 2 + 2, |
|
574 |
getWheelRadius() * 2 + 2)); |
|
575 |
||
576 |
// We want to handle tab ourself. |
|
577 |
setFocusTraversalKeysEnabled(false); |
|
578 |
||
579 |
// PENDING: this should come from the style. |
|
580 |
getInputMap().put(KeyStroke.getKeyStroke("UP"), "up"); |
|
581 |
getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "down"); |
|
582 |
getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "left"); |
|
583 |
getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "right"); |
|
584 |
||
585 |
getInputMap().put(KeyStroke.getKeyStroke("KP_UP"), "up"); |
|
586 |
getInputMap().put(KeyStroke.getKeyStroke("KP_DOWN"), "down"); |
|
587 |
getInputMap().put(KeyStroke.getKeyStroke("KP_LEFT"), "left"); |
|
588 |
getInputMap().put(KeyStroke.getKeyStroke("KP_RIGHT"), "right"); |
|
589 |
||
590 |
getInputMap().put(KeyStroke.getKeyStroke("TAB"), "focusNext"); |
|
591 |
getInputMap().put(KeyStroke.getKeyStroke("shift TAB"),"focusLast"); |
|
592 |
||
593 |
ActionMap map = (ActionMap)UIManager.get( |
|
594 |
"GTKColorChooserPanel.actionMap"); |
|
595 |
||
596 |
if (map == null) { |
|
597 |
map = new ActionMapUIResource(); |
|
598 |
map.put("left", new ColorAction("left", 2)); |
|
599 |
map.put("right", new ColorAction("right", 3)); |
|
600 |
map.put("up", new ColorAction("up", 0)); |
|
601 |
map.put("down", new ColorAction("down", 1)); |
|
602 |
map.put("focusNext", new ColorAction("focusNext", 4)); |
|
603 |
map.put("focusLast", new ColorAction("focusLast", 5)); |
|
604 |
UIManager.getLookAndFeelDefaults().put( |
|
605 |
"GTKColorChooserPanel.actionMap", map); |
|
606 |
} |
|
607 |
SwingUtilities.replaceUIActionMap(this, map); |
|
608 |
} |
|
609 |
||
610 |
/** |
|
611 |
* Returns the GTKColorChooserPanel. |
|
612 |
*/ |
|
613 |
GTKColorChooserPanel getGTKColorChooserPanel() { |
|
614 |
return GTKColorChooserPanel.this; |
|
615 |
} |
|
616 |
||
617 |
/** |
|
618 |
* Gives focus to the wheel. |
|
619 |
*/ |
|
620 |
void focusWheel() { |
|
621 |
setFocusType(1); |
|
622 |
} |
|
623 |
||
624 |
/** |
|
625 |
* Gives focus to the triangle. |
|
626 |
*/ |
|
627 |
void focusTriangle() { |
|
628 |
setFocusType(2); |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* Returns true if the wheel currently has focus. |
|
633 |
*/ |
|
634 |
boolean isWheelFocused() { |
|
635 |
return isSet(FLAGS_FOCUSED_WHEEL); |
|
636 |
} |
|
637 |
||
638 |
/** |
|
639 |
* Resets the selected color. |
|
640 |
*/ |
|
641 |
public void setColor(float h, float s, float b) { |
|
642 |
if (isSet(FLAGS_SETTING_COLOR)) { |
|
643 |
return; |
|
644 |
} |
|
645 |
||
646 |
setAngleFromHue(h); |
|
647 |
setSaturationAndBrightness(s, b); |
|
648 |
} |
|
649 |
||
650 |
/** |
|
651 |
* Returns the selected color. |
|
652 |
*/ |
|
653 |
public Color getColor() { |
|
654 |
return GTKColorChooserPanel.this.getColor(); |
|
655 |
} |
|
656 |
||
657 |
/** |
|
658 |
* Returns the x location of the selected color indicator. |
|
659 |
*/ |
|
660 |
int getColorX() { |
|
661 |
return circleX + getIndicatorSize() / 2 - getWheelXOrigin(); |
|
662 |
} |
|
663 |
||
664 |
/** |
|
665 |
* Returns the y location of the selected color indicator. |
|
666 |
*/ |
|
667 |
int getColorY() { |
|
668 |
return circleY + getIndicatorSize() / 2 - getWheelYOrigin(); |
|
669 |
} |
|
670 |
||
671 |
protected void processEvent(AWTEvent e) { |
|
672 |
if (e.getID() == MouseEvent.MOUSE_PRESSED || |
|
673 |
((isSet(FLAGS_DRAGGING) ||isSet(FLAGS_DRAGGING_TRIANGLE)) && |
|
674 |
e.getID() == MouseEvent.MOUSE_DRAGGED)) { |
|
675 |
// Assign focus to either the wheel or triangle and attempt |
|
676 |
// to drag either the wheel or triangle. |
|
677 |
int size = getWheelRadius(); |
|
678 |
int x = ((MouseEvent)e).getX() - size; |
|
679 |
int y = ((MouseEvent)e).getY() - size; |
|
680 |
||
681 |
if (!hasFocus()) { |
|
682 |
requestFocus(); |
|
683 |
} |
|
684 |
if (!isSet(FLAGS_DRAGGING_TRIANGLE) && |
|
685 |
adjustHue(x, y, e.getID() == MouseEvent.MOUSE_PRESSED)) { |
|
686 |
setFlag(FLAGS_DRAGGING, true); |
|
687 |
setFocusType(1); |
|
688 |
} |
|
689 |
else if (adjustSB(x, y, e.getID() == |
|
690 |
MouseEvent.MOUSE_PRESSED)) { |
|
691 |
setFlag(FLAGS_DRAGGING_TRIANGLE, true); |
|
692 |
setFocusType(2); |
|
693 |
} |
|
694 |
else { |
|
695 |
setFocusType(2); |
|
696 |
} |
|
697 |
} |
|
698 |
else if (e.getID() == MouseEvent.MOUSE_RELEASED) { |
|
699 |
// Stopped dragging |
|
700 |
setFlag(FLAGS_DRAGGING_TRIANGLE, false); |
|
701 |
setFlag(FLAGS_DRAGGING, false); |
|
702 |
} |
|
703 |
else if (e.getID() == FocusEvent.FOCUS_LOST) { |
|
704 |
// Reset the flags to indicate no one has focus |
|
705 |
setFocusType(0); |
|
706 |
} |
|
707 |
else if (e.getID() == FocusEvent.FOCUS_GAINED) { |
|
708 |
// Gained focus, reassign focus to the wheel if no one |
|
709 |
// currently has focus. |
|
710 |
if (!isSet(FLAGS_FOCUSED_TRIANGLE) && |
|
711 |
!isSet(FLAGS_FOCUSED_WHEEL)) { |
|
712 |
setFlag(FLAGS_FOCUSED_WHEEL, true); |
|
713 |
setFocusType(1); |
|
714 |
} |
|
715 |
repaint(); |
|
716 |
} |
|
717 |
super.processEvent(e); |
|
718 |
} |
|
719 |
||
720 |
public void paintComponent(Graphics g) { |
|
721 |
super.paintComponent(g); |
|
722 |
||
723 |
// Draw the wheel and triangle |
|
724 |
int size = getWheelRadius(); |
|
725 |
int width = getWheelWidth(); |
|
726 |
Image image = getImage(size); |
|
727 |
g.drawImage(image, getWheelXOrigin() - size, |
|
728 |
getWheelYOrigin() - size, null); |
|
729 |
||
730 |
// Draw the focus indicator for the wheel |
|
731 |
if (hasFocus() && isSet(FLAGS_FOCUSED_WHEEL)) { |
|
732 |
g.setColor(Color.BLACK); |
|
733 |
g.drawOval(getWheelXOrigin() - size, getWheelYOrigin() - size, |
|
734 |
2 * size, 2 * size); |
|
735 |
g.drawOval(getWheelXOrigin() - size + width, getWheelYOrigin()- |
|
736 |
size + width, 2 * (size - width), 2 * |
|
737 |
(size - width)); |
|
738 |
} |
|
739 |
||
740 |
// Draw a line on the wheel indicating the selected hue. |
|
741 |
if (Math.toDegrees(Math.PI * 2 - angle) <= 20 || |
|
742 |
Math.toDegrees(Math.PI * 2 - angle) >= 201) { |
|
743 |
g.setColor(Color.WHITE); |
|
744 |
} |
|
745 |
else { |
|
746 |
g.setColor(Color.BLACK); |
|
747 |
} |
|
748 |
int lineX0 = (int)(Math.cos(angle) * size); |
|
749 |
int lineY0 = (int)(Math.sin(angle) * size); |
|
750 |
int lineX1 = (int)(Math.cos(angle) * (size - width)); |
|
751 |
int lineY1 = (int)(Math.sin(angle) * (size - width)); |
|
752 |
g.drawLine(lineX0 + size, lineY0 + size, lineX1 + size, |
|
753 |
lineY1 + size); |
|
754 |
||
755 |
// Draw the focus indicator on the triangle |
|
756 |
if (hasFocus() && isSet(FLAGS_FOCUSED_TRIANGLE)) { |
|
757 |
Graphics g2 = g.create(); |
|
758 |
int innerR = getTriangleCircumscribedRadius(); |
|
759 |
int a = (int)(3 * innerR / Math.sqrt(3)); |
|
760 |
g2.translate(getWheelXOrigin(), getWheelYOrigin()); |
|
761 |
((Graphics2D)g2).rotate(angle + Math.PI / 2); |
|
762 |
g2.setColor(Color.BLACK); |
|
763 |
g2.drawLine(0, -innerR, a / 2, innerR / 2); |
|
764 |
g2.drawLine(a / 2, innerR / 2, -a / 2, innerR / 2); |
|
765 |
g2.drawLine(-a / 2, innerR / 2, 0, -innerR); |
|
766 |
g2.dispose(); |
|
767 |
} |
|
768 |
||
769 |
// Draw the selected color indicator. |
|
770 |
g.setColor(Color.BLACK); |
|
771 |
g.drawOval(circleX, circleY, getIndicatorSize() - 1, |
|
772 |
getIndicatorSize() - 1); |
|
773 |
g.setColor(Color.WHITE); |
|
774 |
g.drawOval(circleX + 1, circleY + 1, getIndicatorSize() - 3, |
|
775 |
getIndicatorSize() - 3); |
|
776 |
} |
|
777 |
||
778 |
/** |
|
779 |
* Returns an image representing the triangle and wheel. |
|
780 |
*/ |
|
781 |
private Image getImage(int size) { |
|
782 |
if (!isSet(FLAGS_CHANGED_ANGLE) && wheelImage != null && |
|
783 |
wheelImage.getWidth(null) == size * 2) { |
|
784 |
return wheelImage; |
|
785 |
} |
|
786 |
if (wheelImage == null || wheelImage.getWidth(null) != size) { |
|
787 |
wheelImage = getWheelImage(size); |
|
788 |
} |
|
789 |
int innerR = getTriangleCircumscribedRadius(); |
|
790 |
int triangleSize = (int)(innerR * 3.0 / 2.0); |
|
791 |
int a = (int)(2 * triangleSize / Math.sqrt(3)); |
|
792 |
if (triangleImage == null || triangleImage.getWidth(null) != a) { |
|
793 |
triangleImage = new BufferedImage(a, a, |
|
794 |
BufferedImage.TYPE_INT_ARGB); |
|
795 |
} |
|
796 |
Graphics g = triangleImage.getGraphics(); |
|
797 |
g.setColor(new Color(0, 0, 0, 0)); |
|
798 |
g.fillRect(0, 0, a, a); |
|
799 |
g.translate((int)(a / 2), 0); |
|
800 |
paintTriangle(g, triangleSize, getColor()); |
|
801 |
g.translate((int)(-a / 2), 0); |
|
802 |
g.dispose(); |
|
803 |
||
804 |
g = wheelImage.getGraphics(); |
|
805 |
g.setColor(new Color(0, 0, 0, 0)); |
|
806 |
g.fillOval(getWheelWidth(), getWheelWidth(), |
|
807 |
2 * (size - getWheelWidth()), |
|
808 |
2 * (size - getWheelWidth())); |
|
809 |
||
810 |
double rotate = Math.toRadians(-30.0) + angle; |
|
811 |
g.translate(size, size); |
|
812 |
((Graphics2D)g).rotate(rotate); |
|
813 |
g.drawImage(triangleImage, -a / 2, |
|
814 |
getWheelWidth() - size, null); |
|
815 |
((Graphics2D)g).rotate(-rotate); |
|
816 |
g.translate(a / 2, size - getWheelWidth()); |
|
817 |
||
818 |
setFlag(FLAGS_CHANGED_ANGLE, false); |
|
819 |
||
820 |
return wheelImage; |
|
821 |
} |
|
822 |
||
823 |
private void paintTriangle(Graphics g, int size, Color color) { |
|
824 |
float[] colors = Color.RGBtoHSB(color.getRed(), |
|
825 |
color.getGreen(), |
|
826 |
color.getBlue(), null); |
|
827 |
float hue = colors[0]; |
|
828 |
double dSize = (double)size; |
|
829 |
for (int y = 0; y < size; y++) { |
|
830 |
int maxX = (int)(y * Math.tan(Math.toRadians(30.0))); |
|
831 |
float factor = maxX * 2; |
|
832 |
if (maxX > 0) { |
|
833 |
float value = (float)(y / dSize); |
|
834 |
for (int x = -maxX; x <= maxX; x++) { |
|
835 |
float saturation = (float)x / factor + .5f; |
|
836 |
g.setColor(Color.getHSBColor(hue, saturation, value)); |
|
837 |
g.fillRect(x, y, 1, 1); |
|
838 |
} |
|
839 |
} |
|
840 |
else { |
|
841 |
g.setColor(color); |
|
842 |
g.fillRect(0, y, 1, 1); |
|
843 |
} |
|
844 |
} |
|
845 |
} |
|
846 |
||
847 |
/** |
|
848 |
* Returns a color wheel image for the specified size. |
|
849 |
* |
|
850 |
* @param size Integer giving size of color wheel. |
|
851 |
* @return Color wheel image |
|
852 |
*/ |
|
853 |
private Image getWheelImage(int size) { |
|
854 |
int minSize = size - getWheelWidth(); |
|
855 |
int doubleSize = size * 2; |
|
856 |
BufferedImage image = new BufferedImage(doubleSize, doubleSize, |
|
857 |
BufferedImage.TYPE_INT_ARGB); |
|
858 |
||
859 |
for (int y = -size; y < size; y++) { |
|
860 |
int ySquared = y * y; |
|
861 |
for (int x = -size; x < size; x++) { |
|
862 |
double rad = Math.sqrt(ySquared + x * x); |
|
863 |
||
864 |
if (rad < size && rad > minSize) { |
|
865 |
int rgb = colorWheelLocationToRGB(x, y, rad) | |
|
866 |
0xFF000000; |
|
867 |
image.setRGB(x + size, y + size, rgb); |
|
868 |
} |
|
869 |
} |
|
870 |
} |
|
871 |
wheelImage = image; |
|
872 |
return wheelImage; |
|
873 |
} |
|
874 |
||
875 |
/** |
|
876 |
* Adjusts the saturation and brightness. <code>x</code> and |
|
877 |
* <code>y</code> give the location to adjust to and are relative |
|
878 |
* to the origin of the wheel/triangle. |
|
879 |
* |
|
880 |
* @param x X coordinate on the triangle to adjust to |
|
881 |
* @param y Y coordinate on the triangle to adjust to |
|
882 |
* @param checkLoc if true the location is checked to make sure |
|
883 |
* it is contained in the triangle, if false the location is |
|
884 |
* constrained to fit in the triangle. |
|
885 |
* @return true if the location is valid |
|
886 |
*/ |
|
887 |
boolean adjustSB(int x, int y, boolean checkLoc) { |
|
888 |
int innerR = getWheelRadius() - getWheelWidth(); |
|
889 |
boolean resetXY = false; |
|
890 |
// Invert the axis. |
|
891 |
y = -y; |
|
892 |
if (checkLoc && (x < -innerR || x > innerR || y < -innerR || |
|
893 |
y > innerR)) { |
|
894 |
return false; |
|
895 |
} |
|
896 |
// Rotate to origin and and verify x is valid. |
|
897 |
int triangleSize = (int)innerR * 3 / 2; |
|
898 |
double x1 = Math.cos(angle) * x - Math.sin(angle) * y; |
|
899 |
double y1 = Math.sin(angle) * x + Math.cos(angle) * y; |
|
900 |
if (x1 < -(innerR / 2)) { |
|
901 |
if (checkLoc) { |
|
902 |
return false; |
|
903 |
} |
|
904 |
x1 = -innerR / 2; |
|
905 |
resetXY = true; |
|
906 |
} |
|
907 |
else if ((int)x1 > innerR) { |
|
908 |
if (checkLoc) { |
|
909 |
return false; |
|
910 |
} |
|
911 |
x1 = innerR; |
|
912 |
resetXY = true; |
|
913 |
} |
|
914 |
// Verify y location is valid. |
|
915 |
int maxY = (int)((triangleSize - x1 - innerR / 2.0) * |
|
916 |
Math.tan(Math.toRadians(30.0))); |
|
917 |
if (y1 <= -maxY) { |
|
918 |
if (checkLoc) { |
|
919 |
return false; |
|
920 |
} |
|
921 |
y1 = -maxY; |
|
922 |
resetXY = true; |
|
923 |
} |
|
924 |
else if (y1 > maxY) { |
|
925 |
if (checkLoc) { |
|
926 |
return false; |
|
927 |
} |
|
928 |
y1 = maxY; |
|
929 |
resetXY = true; |
|
930 |
} |
|
931 |
// Rotate again to determine value and scale |
|
932 |
double x2 = Math.cos(Math.toRadians(-30.0)) * x1 - |
|
933 |
Math.sin(Math.toRadians(-30.0)) * y1; |
|
934 |
double y2 = Math.sin(Math.toRadians(-30.0)) * x1 + |
|
935 |
Math.cos(Math.toRadians(-30.0)) * y1; |
|
936 |
float value = Math.min(1.0f, (float)((innerR - y2) / |
|
937 |
(double)triangleSize)); |
|
938 |
float maxX = (float)(Math.tan(Math.toRadians(30)) * (innerR - y2)); |
|
939 |
float saturation = Math.min(1.0f, (float)(x2 / maxX / 2 + .5)); |
|
940 |
||
941 |
setFlag(FLAGS_SETTING_COLOR, true); |
|
942 |
if (resetXY) { |
|
943 |
setSaturationAndBrightness(saturation, value); |
|
944 |
} |
|
945 |
else { |
|
946 |
setSaturationAndBrightness(saturation, value, x + |
|
947 |
getWheelXOrigin(),getWheelYOrigin() - y); |
|
948 |
} |
|
949 |
GTKColorChooserPanel.this.setSaturationAndBrightness(saturation, |
|
950 |
value, true); |
|
951 |
setFlag(FLAGS_SETTING_COLOR, false); |
|
952 |
return true; |
|
953 |
} |
|
954 |
||
955 |
/** |
|
956 |
* Sets the saturation and brightness. |
|
957 |
*/ |
|
958 |
private void setSaturationAndBrightness(float s, float b) { |
|
959 |
int innerR = getTriangleCircumscribedRadius(); |
|
960 |
int triangleSize = (int)innerR * 3 / 2; |
|
961 |
double x = b * triangleSize; |
|
962 |
double maxY = x * Math.tan(Math.toRadians(30.0)); |
|
963 |
double y = 2 * maxY * s - maxY; |
|
964 |
x = x - innerR; |
|
965 |
double x1 = Math.cos(Math.toRadians(-60.0) - angle) * |
|
966 |
x - Math.sin(Math.toRadians(-60.0) - angle) * y; |
|
967 |
double y1 = Math.sin(Math.toRadians(-60.0) - angle) * x + |
|
968 |
Math.cos(Math.toRadians(-60.0) - angle) * y; |
|
969 |
int newCircleX = (int)x1 + getWheelXOrigin(); |
|
970 |
int newCircleY = getWheelYOrigin() - (int)y1; |
|
971 |
||
972 |
setSaturationAndBrightness(s, b, newCircleX, newCircleY); |
|
973 |
} |
|
974 |
||
975 |
||
976 |
/** |
|
977 |
* Sets the saturation and brightness. |
|
978 |
*/ |
|
979 |
private void setSaturationAndBrightness(float s, float b, |
|
980 |
int newCircleX, int newCircleY) { |
|
981 |
newCircleX -= getIndicatorSize() / 2; |
|
982 |
newCircleY -= getIndicatorSize() / 2; |
|
983 |
||
984 |
int minX = Math.min(newCircleX, circleX); |
|
985 |
int minY = Math.min(newCircleY, circleY); |
|
986 |
||
987 |
repaint(minX, minY, Math.max(circleX, newCircleX) - minX + |
|
988 |
getIndicatorSize() + 1, Math.max(circleY, newCircleY) - |
|
989 |
minY + getIndicatorSize() + 1); |
|
990 |
circleX = newCircleX; |
|
991 |
circleY = newCircleY; |
|
992 |
} |
|
993 |
||
994 |
/** |
|
995 |
* Adjusts the hue based on the passed in location. |
|
996 |
* |
|
997 |
* @param x X location to adjust to, relative to the origin of the |
|
998 |
* wheel |
|
999 |
* @param y Y location to adjust to, relative to the origin of the |
|
1000 |
* wheel |
|
1001 |
* @param check if true the location is checked to make sure |
|
1002 |
* it is contained in the wheel, if false the location is |
|
1003 |
* constrained to fit in the wheel |
|
1004 |
* @return true if the location is valid. |
|
1005 |
*/ |
|
1006 |
private boolean adjustHue(int x, int y, boolean check) { |
|
1007 |
double rad = Math.sqrt(x * x + y * y); |
|
1008 |
int size = getWheelRadius(); |
|
1009 |
||
1010 |
if (!check || (rad >= size - getWheelWidth() && rad < size)) { |
|
1011 |
// Map the location to an angle and reset hue |
|
1012 |
double angle; |
|
1013 |
if (x == 0) { |
|
1014 |
if (y > 0) { |
|
1015 |
angle = Math.PI / 2.0; |
|
1016 |
} |
|
1017 |
else { |
|
1018 |
angle = Math.PI + Math.PI / 2.0; |
|
1019 |
} |
|
1020 |
} |
|
1021 |
else { |
|
1022 |
angle = Math.atan((double)y / (double)x); |
|
1023 |
if (x < 0) { |
|
1024 |
angle += Math.PI; |
|
1025 |
} |
|
1026 |
else if (angle < 0) { |
|
1027 |
angle += 2 * Math.PI; |
|
1028 |
} |
|
1029 |
} |
|
1030 |
setFlag(FLAGS_SETTING_COLOR, true); |
|
1031 |
setHue((float)(1.0 - angle / Math.PI / 2), true); |
|
1032 |
setFlag(FLAGS_SETTING_COLOR, false); |
|
1033 |
setHueAngle(angle); |
|
1034 |
setSaturationAndBrightness(getSaturation(), getBrightness()); |
|
1035 |
return true; |
|
1036 |
} |
|
1037 |
return false; |
|
1038 |
} |
|
1039 |
||
1040 |
/** |
|
1041 |
* Rotates the triangle to accomodate the passed in hue. |
|
1042 |
*/ |
|
1043 |
private void setAngleFromHue(float hue) { |
|
1044 |
setHueAngle((1.0 - hue) * Math.PI * 2); |
|
1045 |
} |
|
1046 |
||
1047 |
/** |
|
1048 |
* Sets the angle representing the hue. |
|
1049 |
*/ |
|
1050 |
private void setHueAngle(double angle) { |
|
1051 |
double oldAngle = this.angle; |
|
1052 |
||
1053 |
this.angle = angle; |
|
1054 |
if (angle != oldAngle) { |
|
1055 |
setFlag(FLAGS_CHANGED_ANGLE, true); |
|
1056 |
repaint(); |
|
1057 |
} |
|
1058 |
} |
|
1059 |
||
1060 |
/** |
|
1061 |
* Returns the size of the color indicator. |
|
1062 |
*/ |
|
1063 |
private int getIndicatorSize() { |
|
1064 |
return 8; |
|
1065 |
} |
|
1066 |
||
1067 |
/** |
|
1068 |
* Returns the circumscribed radius of the triangle. |
|
1069 |
*/ |
|
1070 |
private int getTriangleCircumscribedRadius() { |
|
1071 |
return 72; |
|
1072 |
} |
|
1073 |
||
1074 |
/** |
|
1075 |
* Returns the x origin of the wheel and triangle. |
|
1076 |
*/ |
|
1077 |
private int getWheelXOrigin() { |
|
1078 |
return 85; |
|
1079 |
} |
|
1080 |
||
1081 |
/** |
|
1082 |
* Returns y origin of the wheel and triangle. |
|
1083 |
*/ |
|
1084 |
private int getWheelYOrigin() { |
|
1085 |
return 85; |
|
1086 |
} |
|
1087 |
||
1088 |
/** |
|
1089 |
* Returns the width of the wheel. |
|
1090 |
*/ |
|
1091 |
private int getWheelWidth() { |
|
1092 |
return 13; |
|
1093 |
} |
|
1094 |
||
1095 |
/** |
|
1096 |
* Sets the focus to one of: 0 no one, 1 the wheel or 2 the triangle. |
|
1097 |
*/ |
|
1098 |
private void setFocusType(int type) { |
|
1099 |
if (type == 0) { |
|
1100 |
setFlag(FLAGS_FOCUSED_WHEEL, false); |
|
1101 |
setFlag(FLAGS_FOCUSED_TRIANGLE, false); |
|
1102 |
repaint(); |
|
1103 |
} |
|
1104 |
else { |
|
1105 |
int toSet = FLAGS_FOCUSED_WHEEL; |
|
1106 |
int toUnset = FLAGS_FOCUSED_TRIANGLE; |
|
1107 |
||
1108 |
if (type == 2) { |
|
1109 |
toSet = FLAGS_FOCUSED_TRIANGLE; |
|
1110 |
toUnset = FLAGS_FOCUSED_WHEEL; |
|
1111 |
} |
|
1112 |
if (!isSet(toSet)) { |
|
1113 |
setFlag(toSet, true); |
|
1114 |
repaint(); |
|
1115 |
setFlag(toUnset, false); |
|
1116 |
} |
|
1117 |
} |
|
1118 |
} |
|
1119 |
||
1120 |
/** |
|
1121 |
* Returns the radius of the wheel. |
|
1122 |
*/ |
|
1123 |
private int getWheelRadius() { |
|
1124 |
// As far as I can tell, GTK doesn't allow stretching this |
|
1125 |
// widget |
|
1126 |
return 85; |
|
1127 |
} |
|
1128 |
||
1129 |
/** |
|
1130 |
* Updates the flags bitmask. |
|
1131 |
*/ |
|
1132 |
private void setFlag(int flag, boolean value) { |
|
1133 |
if (value) { |
|
1134 |
flags |= flag; |
|
1135 |
} |
|
1136 |
else { |
|
1137 |
flags &= ~flag; |
|
1138 |
} |
|
1139 |
} |
|
1140 |
||
1141 |
/** |
|
1142 |
* Returns true if a particular flag has been set. |
|
1143 |
*/ |
|
1144 |
private boolean isSet(int flag) { |
|
1145 |
return ((flags & flag) == flag); |
|
1146 |
} |
|
1147 |
||
1148 |
/** |
|
1149 |
* Returns the RGB color to use for the specified location. The |
|
1150 |
* passed in point must be on the color wheel and be relative to the |
|
1151 |
* origin of the color wheel. |
|
1152 |
* |
|
1153 |
* @param x X location to get color for |
|
1154 |
* @param y Y location to get color for |
|
1155 |
* @param rad Radius from center of color wheel |
|
1156 |
* @param integer with red, green and blue components |
|
1157 |
*/ |
|
1158 |
private int colorWheelLocationToRGB(int x, int y, double rad) { |
|
1159 |
double angle = Math.acos((double)x / rad); |
|
1160 |
int rgb; |
|
1161 |
||
1162 |
if (angle < PI_3) { |
|
1163 |
if (y < 0) { |
|
1164 |
// FFFF00 - FF0000 |
|
1165 |
rgb = 0xFF0000 | (int)Math.min(255, |
|
1166 |
(int)(255 * angle / PI_3)) << 8; |
|
1167 |
} |
|
1168 |
else { |
|
1169 |
// FF0000 - FF00FF |
|
1170 |
rgb = 0xFF0000 | (int)Math.min(255, |
|
1171 |
(int)(255 * angle / PI_3)); |
|
1172 |
} |
|
1173 |
} |
|
1174 |
else if (angle < 2 * PI_3) { |
|
1175 |
angle -= PI_3; |
|
1176 |
if (y < 0) { |
|
1177 |
// 00FF00 - FFFF00 |
|
1178 |
rgb = 0x00FF00 | (int)Math.max(0, 255 - |
|
1179 |
(int)(255 * angle / PI_3)) << 16; |
|
1180 |
} |
|
1181 |
else { |
|
1182 |
// FF00FF - 0000FF |
|
1183 |
rgb = 0x0000FF | (int)Math.max(0, 255 - |
|
1184 |
(int)(255 * angle / PI_3)) << 16; |
|
1185 |
} |
|
1186 |
} |
|
1187 |
else { |
|
1188 |
angle -= 2 * PI_3; |
|
1189 |
if (y < 0) { |
|
1190 |
// 00FFFF - 00FF00 |
|
1191 |
rgb = 0x00FF00 | (int)Math.min(255, |
|
1192 |
(int)(255 * angle / PI_3)); |
|
1193 |
} |
|
1194 |
else { |
|
1195 |
// 0000FF - 00FFFF |
|
1196 |
rgb = 0x0000FF | (int)Math.min(255, |
|
1197 |
(int)(255 * angle / PI_3)) << 8; |
|
1198 |
} |
|
1199 |
} |
|
1200 |
return rgb; |
|
1201 |
} |
|
1202 |
||
1203 |
/** |
|
1204 |
* Increments the hue. |
|
1205 |
*/ |
|
1206 |
void incrementHue(boolean positive) { |
|
1207 |
float hue = triangle.getGTKColorChooserPanel().getHue(); |
|
1208 |
||
1209 |
if (positive) { |
|
1210 |
hue += 1.0f / 360.0f; |
|
1211 |
} |
|
1212 |
else { |
|
1213 |
hue -= 1.0f / 360.0f; |
|
1214 |
} |
|
1215 |
if (hue > 1) { |
|
1216 |
hue -= 1; |
|
1217 |
} |
|
1218 |
else if (hue < 0) { |
|
1219 |
hue += 1; |
|
1220 |
} |
|
1221 |
getGTKColorChooserPanel().setHue(hue, true); |
|
1222 |
} |
|
1223 |
} |
|
1224 |
||
1225 |
||
1226 |
/** |
|
1227 |
* Action class used for colors. |
|
1228 |
*/ |
|
1229 |
private static class ColorAction extends AbstractAction { |
|
1230 |
private int type; |
|
1231 |
||
1232 |
ColorAction(String name, int type) { |
|
1233 |
super(name); |
|
1234 |
this.type = type; |
|
1235 |
} |
|
1236 |
||
1237 |
public void actionPerformed(ActionEvent e) { |
|
1238 |
ColorTriangle triangle = (ColorTriangle)e.getSource(); |
|
1239 |
||
1240 |
if (triangle.isWheelFocused()) { |
|
1241 |
float hue = triangle.getGTKColorChooserPanel().getHue(); |
|
1242 |
||
1243 |
switch (type) { |
|
1244 |
case 0: |
|
1245 |
case 2: |
|
1246 |
triangle.incrementHue(true); |
|
1247 |
break; |
|
1248 |
case 1: |
|
1249 |
case 3: |
|
1250 |
triangle.incrementHue(false); |
|
1251 |
break; |
|
1252 |
case 4: |
|
1253 |
triangle.focusTriangle(); |
|
1254 |
break; |
|
1255 |
case 5: |
|
1256 |
compositeRequestFocus(triangle, false); |
|
1257 |
break; |
|
1258 |
} |
|
1259 |
} |
|
1260 |
else { |
|
1261 |
int xDelta = 0; |
|
1262 |
int yDelta = 0; |
|
1263 |
||
1264 |
switch (type) { |
|
1265 |
case 0: |
|
1266 |
// up |
|
1267 |
yDelta--; |
|
1268 |
break; |
|
1269 |
case 1: |
|
1270 |
// down |
|
1271 |
yDelta++; |
|
1272 |
break; |
|
1273 |
case 2: |
|
1274 |
// left |
|
1275 |
xDelta--; |
|
1276 |
break; |
|
1277 |
case 3: |
|
1278 |
// right |
|
1279 |
xDelta++; |
|
1280 |
break; |
|
1281 |
case 4: |
|
1282 |
compositeRequestFocus(triangle, true); |
|
1283 |
return; |
|
1284 |
case 5: |
|
1285 |
triangle.focusWheel(); |
|
1286 |
return; |
|
1287 |
} |
|
1288 |
triangle.adjustSB(triangle.getColorX() + xDelta, |
|
1289 |
triangle.getColorY() + yDelta, true); |
|
1290 |
} |
|
1291 |
} |
|
1292 |
} |
|
1293 |
||
1294 |
||
1295 |
private class OpaqueLabel extends JLabel { |
|
1296 |
public boolean isOpaque() { |
|
1297 |
return true; |
|
1298 |
} |
|
1299 |
} |
|
1300 |
} |