1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.component; |
16 | |
17 | import java.awt.Color; |
18 | |
19 | import javax.swing.colorchooser.DefaultColorSelectionModel; |
20 | |
21 | /** |
22 | * Special ColorSelectionModel that supports the alpha channel. |
23 | * It is supposed to be used in conjunction with special RGBAChooserPanel |
24 | * (instead of swing DefaultRGBChooserPanel). |
25 | * |
26 | * @author Jean-Francois Poilpret |
27 | */ |
28 | public class AlphaColorSelectionModel extends DefaultColorSelectionModel |
29 | { |
30 | /** |
31 | * Allows to explicitely set the alpha channel of the color (transparency). |
32 | * This is the only way to set the alpha (do not use |
33 | * <code>setSelectedColor</code> it will ignore the alpha channel of its |
34 | * <code>color</code> argument). |
35 | */ |
36 | public void setAlpha(int alpha, boolean refresh) |
37 | { |
38 | _alpha = alpha; |
39 | if (refresh) |
40 | { |
41 | // The following line enforces update of the color in the parent |
42 | // class and notification of all listeners. |
43 | setSelectedColor(getSelectedColor()); |
44 | } |
45 | } |
46 | |
47 | @Override public void setSelectedColor(Color color) |
48 | { |
49 | super.setSelectedColor(new Color( color.getRed(), |
50 | color.getGreen(), |
51 | color.getBlue(), |
52 | _alpha)); |
53 | } |
54 | |
55 | static private final int OPAQUE = 0xFF; |
56 | private int _alpha = OPAQUE; |
57 | } |