KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DuoDerivedColor.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 java.awt.Color JavaDoc;
10 import java.beans.PropertyChangeEvent JavaDoc;
11 import java.beans.PropertyChangeListener JavaDoc;
12
13 /**
14  * DuoDerivedColor - Is a derived color that is derived of two parent UIManager
15  * defaults colors and offets and a value of the position between the two
16  * colors. This is designed for use in gradients that need to have a calculated
17  * color that is at some mid point between two derived colors. Property change
18  * events are fired for the "rgb" property when the derived color changes.
19  *
20  * @author Jasper Potts
21  */

22 class DuoDerivedColor extends Color JavaDoc implements PropertyChangeListener JavaDoc {
23     private final Color JavaDoc color1, color2;
24     private final float midPoint;
25     private int argbValue;
26
27     DuoDerivedColor(Color JavaDoc color1, Color JavaDoc color2, float midPoint) {
28         super(0);
29         this.color1 = color1;
30         this.color2 = color2;
31         this.midPoint = midPoint;
32         if (color1 instanceof DerivedColor)
33             ((DerivedColor)color1).addPropertyChangeListener(this);
34         if (color2 instanceof DerivedColor)
35             ((DerivedColor)color2).addPropertyChangeListener(this);
36         rederiveColor();
37     }
38
39     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
40         rederiveColor();
41     }
42
43     /**
44      * Recalculate the derived color from the UIManager parent color and
45      * offsets
46      */

47     public void rederiveColor() {
48         int r = color1.getRed() +
49                 (int) ((color2.getRed() - color1.getRed()) * midPoint + 0.5f);
50         int g = color1.getGreen() +
51                 (int) ((color2.getGreen() - color1.getGreen()) * midPoint +
52                         0.5f);
53         int b = color1.getBlue() +
54                 (int) ((color2.getBlue() - color1.getBlue()) * midPoint + 0.5f);
55         int a = color1.getAlpha() +
56                 (int) ((color2.getAlpha() - color1.getAlpha()) * midPoint +
57                         0.5f);
58         argbValue = ((a & 0xFF) << 24) |
59                 ((r & 0xFF) << 16) |
60                 ((g & 0xFF) << 8) |
61                 (b & 0xFF);
62     }
63
64     @Override JavaDoc
65     public boolean equals(Object JavaDoc o) {
66         if (this == o) return true;
67         if (!(o instanceof DuoDerivedColor)) return false;
68         DuoDerivedColor that = (DuoDerivedColor) o;
69         if (Float.compare(that.midPoint, midPoint) != 0) return false;
70         if (color1 != null ? !color1.equals(that.color1) : that.color1 != null)
71             return false;
72         if (color2 != null ? !color2.equals(that.color2) : that.color2 != null)
73             return false;
74         return true;
75     }
76
77     @Override JavaDoc
78     public int hashCode() {
79         int result = (color1 != null ? color1.hashCode() : 0);
80         result = 31 * result + (color2 != null ? color2.hashCode() : 0);
81         result = 31 * result + midPoint != +0.0f ?
82                 Float.floatToIntBits(midPoint) : 0;
83         return result;
84     }
85
86     /**
87      * Returns the RGB value representing the color in the default sRGB {@link
88      * java.awt.image.ColorModel}. (Bits 24-31 are alpha, 16-23 are red, 8-15
89      * are green, 0-7 are blue).
90      *
91      * @return the RGB value of the color in the default sRGB
92      * <code>ColorModel</code>.
93      * @see java.awt.image.ColorModel#getRGBdefault
94      * @see #getRed
95      * @see #getGreen
96      * @see #getBlue
97      */

98     @Override JavaDoc public int getRGB() {
99         return argbValue;
100     }
101     
102     /**
103      * UIResource version of duo derived color
104      */

105     static class UIResource extends DuoDerivedColor implements javax.swing.plaf.UIResource JavaDoc {
106         UIResource(Color JavaDoc c1, Color JavaDoc c2, float midPoint) {
107             super(c1, c2, midPoint);
108         }
109     }
110 }
111
Popular Tags