KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwt > awt > Color


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9
10    $Log: Color.java,v $
11    Revision 1.14 2004/04/29 12:49:26 bobintetley
12    Additional JOptionePane constants, missing JTree methods and improved awt.Image support
13
14    Revision 1.13 2004/03/23 09:58:56 bobintetley
15    SystemColor and FileDialog implementation, plus JOptionPane.showOptionDialog support
16
17    Revision 1.12 2004/01/26 08:10:59 bobintetley
18    Many bugfixes and addition of SwingSet
19
20    Revision 1.11 2004/01/20 15:52:56 bobintetley
21    Code from an anonymous developer
22
23    Revision 1.10 2004/01/15 15:20:29 bobintetley
24    Java2D work
25
26    Revision 1.9 2003/12/14 09:13:38 bobintetley
27    Added CVS log to source headers
28
29 */

30
31
32 package swingwt.awt;
33
34 import swingwtx.swing.SwingWTUtils;
35
36 public class Color implements Paint {
37    
38     protected org.eclipse.swt.graphics.Color swtColour = null;
39  
40     public static final Color black = new Color(0, 0, 0);
41     public static final Color BLACK = new Color(0, 0, 0);
42     public static final Color blue = new Color(0, 0, 255);
43     public static final Color BLUE = new Color(0, 0, 255);
44     public static final Color cyan = new Color(0, 255, 255);
45     public static final Color CYAN = new Color(0, 255, 255);
46     public static final Color darkGray = new Color(64, 64, 64);
47     public static final Color DARK_GRAY = new Color(64, 64, 64);
48     public static final Color gray = new Color(128, 128, 128);
49     public static final Color GRAY = new Color(128, 128, 128);
50     public static final Color green = new Color(0, 255, 0);
51     public static final Color GREEN = new Color(0, 255, 0);
52     public static final Color lightGray = new Color(192, 192, 192);
53     public static final Color LIGHT_GRAY = new Color(192, 192, 192);
54     public static final Color magenta = new Color(255, 0, 255);
55     public static final Color MAGENTA = new Color(255, 0, 255);
56     public static final Color orange = new Color(255, 200, 0);
57     public static final Color ORANGE = new Color(255, 200, 0);
58     public static final Color pink = new Color(255, 175, 175);
59     public static final Color PINK = new Color(255, 175, 175);
60     public static final Color red = new Color(255, 0, 0);
61     public static final Color RED = new Color(255, 0, 0);
62     public static final Color white = new Color(255, 255, 255);
63     public static final Color WHITE = new Color(255, 255, 255);
64     public static final Color yellow = new Color(255, 255, 0);
65     public static final Color YELLOW = new Color(255, 255, 0);
66     
67     protected static final double colourScale = 0.7;
68     
69     public Color(org.eclipse.swt.graphics.Color swtColour) {
70         this.swtColour = swtColour;
71     }
72     
73     public Color(float r, float g, float b) {
74         this((int) r, (int) g, (int) b, 0);
75     }
76     
77     public Color(float r, float g, float b, float a) {
78         this((int) r, (int) g, (int) b, (int) a);
79     }
80     
81     public Color(int r, int g, int b) {
82         this(r, g, b, 0);
83     }
84     
85     public Color(int r, int g, int b, int a) {
86         this.swtColour = new org.eclipse.swt.graphics.Color(SwingWTUtils.getDisplay(), new org.eclipse.swt.graphics.RGB(r, g, b));
87     }
88     
89     public Color(int rgb) {
90         int[] comps = breakRGBComponents(rgb);
91         this.swtColour = new org.eclipse.swt.graphics.Color(
92             SwingWTUtils.getDisplay(), comps[0], comps[1], comps[2]);
93     }
94     
95     public org.eclipse.swt.graphics.Color getSWTColor() {
96         return swtColour;
97     }
98     
99     public void dispose() {
100         swtColour.dispose();
101     }
102     
103     public int getRed() { return swtColour.getRed(); }
104     public int getGreen() { return swtColour.getGreen(); }
105     public int getBlue() { return swtColour.getBlue(); }
106     
107     public int getRGB() {
108         return ((255 & 0xFF) << 24) |
109                 ((getRed() & 0xFF) << 16) |
110                 ((getGreen() & 0xFF) << 8) |
111                 ((getBlue() & 0xFF) << 0);
112     }
113     
114     /**
115      * Converts the big single integer containing all colour values
116      * into the separate RGB bits and returns them in an array.
117      */

118     protected int[] breakRGBComponents(int value) {
119         int[] cols = new int[3];
120         cols[0] = 255;
121         cols[1] = 255;
122         cols[2] = 255;
123         return cols;
124     }
125
126     
127     public Color brighter() {
128         int r = getRed();
129         int g = getGreen();
130         int b = getBlue();
131         int i = (int)(1.0/(1.0-colourScale));
132         if ( r == 0 && g == 0 && b == 0) {
133            return new Color(i, i, i);
134         }
135         if ( r > 0 && r < i ) r = i;
136         if ( g > 0 && g < i ) g = i;
137         if ( b > 0 && b < i ) b = i;
138         return new Color(Math.min((int)(r/colourScale), 255), Math.min((int)(g/colourScale), 255), Math.min((int)(b/colourScale), 255));
139     }
140     
141     public Color darker() {
142     return new Color(Math.max((int)(getRed() * colourScale), 0), Math.max((int)(getGreen() * colourScale), 0), Math.max((int)(getBlue() * colourScale), 0));
143     }
144     
145     public String JavaDoc toString() {
146         if (swtColour == null) return "null";
147         return "Color: " + swtColour.getRed() + ", " + swtColour.getGreen() + ", " + swtColour.getBlue();
148     }
149     
150     public PaintContext createContext(swingwt.awt.image.ColorModel cm,
151         Rectangle deviceBounds,
152         swingwt.awt.geom.Rectangle2D userBounds,
153         swingwt.awt.geom.AffineTransform xform,
154         RenderingHints hints) {
155             return null;
156     }
157     
158     public int getTransparency() { return OPAQUE; }
159     
160 }
161
Popular Tags