KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > DerivedColor


1 /*
2  * @(#)DerivedColor.java 1.4 08/02/04
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.nimbus;
8
9 import javax.swing.UIManager JavaDoc;
10 import java.awt.Color JavaDoc;
11 import java.beans.PropertyChangeSupport JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13
14 /**
15  * DerivedColor - A color implementation that is derived from a UIManager
16  * defaults table color and a set of offsets. It can be rederived at any point
17  * by calling rederiveColor(). For example when its parent color changes and it
18  * value will update to reflect the new derived color. Property change events
19  * are fired for the "rgb" property when the derived color changes.
20  *
21  * @author Jasper Potts
22  */

23 class DerivedColor extends Color JavaDoc {
24     private final PropertyChangeSupport JavaDoc changeSupport =
25             new PropertyChangeSupport JavaDoc(this);
26     private final String JavaDoc uiDefaultParentName;
27     private final float hOffset, sOffset, bOffset;
28     private final int aOffset;
29     private int argbValue;
30
31     DerivedColor(String JavaDoc uiDefaultParentName, float hOffset, float sOffset, float bOffset, int aOffset) {
32         super(0);
33         this.uiDefaultParentName = uiDefaultParentName;
34         this.hOffset = hOffset;
35         this.sOffset = sOffset;
36         this.bOffset = bOffset;
37         this.aOffset = aOffset;
38     }
39
40     public String JavaDoc getUiDefaultParentName() {
41         return uiDefaultParentName;
42     }
43
44     public float getHueOffset() {
45         return hOffset;
46     }
47
48     public float getSaturationOffset() {
49         return sOffset;
50     }
51
52     public float getBrightnessOffset() {
53         return bOffset;
54     }
55
56     public int getAlphaOffset() {
57         return aOffset;
58     }
59
60     /**
61      * Recalculate the derived color from the UIManager parent color and offsets
62      */

63     public void rederiveColor() {
64         int old = argbValue;
65         Color JavaDoc src = UIManager.getColor(uiDefaultParentName);
66         if (src != null) {
67             float[] tmp = Color.RGBtoHSB(src.getRed(), src.getGreen(), src.getBlue(), null);
68             // apply offsets
69
tmp[0] = clamp(tmp[0] + hOffset);
70             tmp[1] = clamp(tmp[1] + sOffset);
71             tmp[2] = clamp(tmp[2] + bOffset);
72             int alpha = clamp(src.getAlpha() + aOffset);
73             argbValue = (Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha << 24);
74         } else {
75             float[] tmp = new float[3];
76             tmp[0] = clamp(hOffset);
77             tmp[1] = clamp(sOffset);
78             tmp[2] = clamp(bOffset);
79             int alpha = clamp(aOffset);
80             argbValue = (Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha << 24);
81         }
82         changeSupport.firePropertyChange("rgb", old, argbValue);
83     }
84
85     /**
86      * Returns the RGB value representing the color in the default sRGB {@link java.awt.image.ColorModel}. (Bits 24-31
87      * are alpha, 16-23 are red, 8-15 are green, 0-7 are blue).
88      *
89      * @return the RGB value of the color in the default sRGB <code>ColorModel</code>.
90      * @see java.awt.image.ColorModel#getRGBdefault
91      * @see #getRed
92      * @see #getGreen
93      * @see #getBlue
94      * @since JDK1.0
95      */

96     @Override JavaDoc public int getRGB() {
97         return argbValue;
98     }
99
100     @Override JavaDoc
101     public boolean equals(Object JavaDoc o) {
102         if (this == o) return true;
103         if (!(o instanceof DerivedColor)) return false;
104         DerivedColor that = (DerivedColor) o;
105         if (aOffset != that.aOffset) return false;
106         if (Float.compare(that.bOffset, bOffset) != 0) return false;
107         if (Float.compare(that.hOffset, hOffset) != 0) return false;
108         if (Float.compare(that.sOffset, sOffset) != 0) return false;
109         if (!uiDefaultParentName.equals(that.uiDefaultParentName)) return false;
110         return true;
111     }
112
113     @Override JavaDoc
114     public int hashCode() {
115         int result = uiDefaultParentName.hashCode();
116         result = 31 * result + hOffset != +0.0f ?
117                 Float.floatToIntBits(hOffset) : 0;
118         result = 31 * result + sOffset != +0.0f ?
119                 Float.floatToIntBits(sOffset) : 0;
120         result = 31 * result + bOffset != +0.0f ?
121                 Float.floatToIntBits(bOffset) : 0;
122         result = 31 * result + aOffset;
123         return result;
124     }
125
126      /**
127      * Add a PropertyChangeListener to the listener list.
128      * The listener is registered for all properties.
129      * The same listener object may be added more than once, and will be called
130      * as many times as it is added.
131      * If <code>listener</code> is null, no exception is thrown and no action
132      * is taken.
133      *
134      * @param listener The PropertyChangeListener to be added
135      */

136     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
137     changeSupport.addPropertyChangeListener(listener);
138     }
139
140     /**
141      * Remove a PropertyChangeListener from the listener list.
142      * This removes a PropertyChangeListener that was registered
143      * for all properties.
144      * If <code>listener</code> was added more than once to the same event
145      * source, it will be notified one less time after being removed.
146      * If <code>listener</code> is null, or was never added, no exception is
147      * thrown and no action is taken.
148      *
149      * @param listener The PropertyChangeListener to be removed
150      */

151     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
152         changeSupport.removePropertyChangeListener(listener);
153     }
154
155     private float clamp(float value) {
156         if (value < 0) {
157             value = 0;
158         } else if (value > 1) {
159             value = 1;
160         }
161         return value;
162     }
163
164     private int clamp(int value) {
165         if (value < 0) {
166             value = 0;
167         } else if (value > 255) {
168             value = 255;
169         }
170         return value;
171     }
172     
173     /**
174      * Returns a string representation of this <code>Color</code>. This method
175      * is intended to be used only for debugging purposes. The content and
176      * format of the returned string might vary between implementations. The
177      * returned string might be empty but cannot be <code>null</code>.
178      *
179      * @return a String representation of this <code>Color</code>.
180      */

181     @Override JavaDoc
182     public String JavaDoc toString() {
183         Color JavaDoc src = UIManager.getColor(uiDefaultParentName);
184         String JavaDoc s = "DerivedColor(color=" + getRed() + "," + getGreen() + "," + getBlue() +
185                 " parent=" + uiDefaultParentName +
186                 " offsets=" + getHueOffset() + "," + getSaturationOffset() + ","
187                 + getBrightnessOffset() + "," + getAlphaOffset();
188         return src == null ? s : s + " pColor=" + src.getRed() + "," + src.getGreen() + "," + src.getBlue();
189     }
190     
191     static class UIResource extends DerivedColor implements javax.swing.plaf.UIResource JavaDoc {
192         UIResource(String JavaDoc uiDefaultParentName, float hOffset, float sOffset,
193                    float bOffset, int aOffset) {
194             super(uiDefaultParentName, hOffset, sOffset, bOffset, aOffset);
195         }
196     }
197 }
198
Popular Tags