Revision 1632 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultColorChooserController.java
DefaultColorChooserController.java | ||
---|---|---|
2 | 2 |
package org.gvsig.tools.swing.impl; |
3 | 3 |
|
4 | 4 |
import java.awt.Color; |
5 |
import java.awt.Font; |
|
5 | 6 |
import java.awt.event.ActionEvent; |
6 | 7 |
import java.awt.event.ActionListener; |
7 | 8 |
|
... | ... | |
16 | 17 |
import org.gvsig.tools.ToolsLocator; |
17 | 18 |
import org.gvsig.tools.i18n.I18nManager; |
18 | 19 |
import org.gvsig.tools.swing.api.ColorChooserController; |
20 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
|
21 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
|
19 | 22 |
|
20 | 23 |
public class DefaultColorChooserController implements ColorChooserController { |
21 | 24 |
|
... | ... | |
46 | 49 |
} |
47 | 50 |
}); |
48 | 51 |
if( this.sldAlpha != null ) { |
52 |
this.sldAlpha.setMinimum(0); |
|
53 |
this.sldAlpha.setMaximum(255); |
|
49 | 54 |
this.sldAlpha.addChangeListener(new ChangeListener() { |
50 | 55 |
@Override |
51 | 56 |
public void stateChanged(ChangeEvent e) { |
... | ... | |
53 | 58 |
} |
54 | 59 |
}); |
55 | 60 |
} |
61 |
Font font = this.txtLabel.getFont(); |
|
62 |
try { |
|
63 |
font = new Font(Font.MONOSPACED, Font.PLAIN, font.getSize()); |
|
64 |
this.txtLabel.setFont(font); |
|
65 |
} catch(Exception ex) { |
|
66 |
// If dont set a monospaced font ignore it. |
|
67 |
font = null; |
|
68 |
} |
|
56 | 69 |
this.allowNull = allowNull; |
57 | 70 |
if( !this.allowNull ) { |
58 | 71 |
this.color = Color.BLACK; |
... | ... | |
122 | 135 |
} |
123 | 136 |
color = Color.BLACK; |
124 | 137 |
} |
138 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
|
125 | 139 |
this.color = color; |
126 | 140 |
String text = String.format("%02x %02x%02x%02x",color.getAlpha(),color.getRed(),color.getGreen(),color.getBlue()); |
127 |
this.txtLabel.setBackground(opaqueColor(this.color)); |
|
141 |
this.txtLabel.setBackground(manager.opaqueColor(this.color));
|
|
128 | 142 |
this.txtLabel.setText(text); |
129 | 143 |
if( this.sldAlpha != null ) { |
130 | 144 |
this.sldAlpha.setValue(this.color.getAlpha()); |
... | ... | |
132 | 146 |
this.txtLabel.invalidate(); |
133 | 147 |
} |
134 | 148 |
|
135 |
private Color opaqueColor(Color src) { |
|
136 |
// Based on |
|
137 |
// https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending |
|
138 |
// |
|
139 |
// when the destination background is opaque |
|
140 |
// |
|
141 |
double srca = src.getAlpha() / 255.0; |
|
142 |
double srca_1 = (1 - srca); |
|
143 |
|
|
144 |
Color color = new Color( |
|
145 |
(int)(src.getRed() * srca + 255 * srca_1) & 0xff, |
|
146 |
(int)(src.getGreen()* srca + 255 * srca_1) & 0xff, |
|
147 |
(int)(src.getBlue() * srca + 255 * srca_1) & 0xff |
|
148 |
); |
|
149 |
return color; |
|
150 |
} |
|
151 |
|
|
152 | 149 |
@Override |
153 | 150 |
public Color get() { |
154 | 151 |
return this.color; |
Also available in: Unified diff