KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > api > AlphaColor


1 /*
2  * $Id: AlphaColor.java,v 1.2 2002/02/15 23:44:27 skavish Exp $
3  *
4  * ==========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.api;
52
53 import java.io.*;
54 import java.util.*;
55 import java.lang.Math JavaDoc;
56 import org.openlaszlo.iv.flash.parser.*;
57 import org.openlaszlo.iv.flash.util.*;
58
59 /**
60  * Color with alpha value
61  *
62  * @author Dmitry Skavish
63  * @see Color
64  */

65 public final class AlphaColor extends Color {
66
67     // some predefined colors
68
public static final AlphaColor white = new AlphaColor( 255, 255, 255 );
69     public static final AlphaColor black = new AlphaColor( 0, 0, 0 );
70     public static final AlphaColor red = new AlphaColor( 255, 0, 0 );
71     public static final AlphaColor blue = new AlphaColor( 0, 0, 255 );
72     public static final AlphaColor green = new AlphaColor( 0, 255, 0 );
73     public static final AlphaColor lightgray = new AlphaColor( 180, 180, 180 );
74     public static final AlphaColor gray = new AlphaColor( 150, 150, 150 );
75     public static final AlphaColor darkgray = new AlphaColor( 90, 90, 90 );
76
77     private int a;
78
79     public AlphaColor() {}
80
81     /**
82      * Creates AlphaColor from int
83      *
84      * @param rgba color encoded as follows: alpha<<24 | red<<16 | green<<8 | blue
85      */

86     public AlphaColor( int rgba ) {
87         setRGBA( rgba );
88     }
89
90     /**
91      * Creates opaque AlphaColor from three components
92      *
93      * @param r red
94      * @param g green
95      * @param b blue
96      */

97     public AlphaColor( int r, int g, int b ) {
98         this( r, g, b, 255 );
99     }
100
101     /**
102      * Creates opaque AlphaColor from three float components
103      *
104      * @param r red
105      * @param g green
106      * @param b blue
107      */

108     public AlphaColor( float r, float g, float b ) {
109         this( r, g, b, 1.0f );
110     }
111
112     /**
113      * Creates AlphaColor from four components
114      *
115      * @param r red
116      * @param g green
117      * @param b blue
118      * @param a alpha
119      */

120     public AlphaColor( int r, int g, int b, int a ) {
121         super( r, g, b );
122         this.a = a;
123     }
124
125     /**
126      * Creates AlphaColor from four components
127      *
128      * @param r red
129      * @param g green
130      * @param b blue
131      * @param a alpha
132      */

133     public AlphaColor( float r, float g, float b, float a ) {
134         super( r, g, b );
135         this.a = Math.round( a * 255 );
136     }
137
138     public int getAlpha() {
139         return a;
140     }
141
142     /**
143      * Returns color encoded into int
144      *
145      * @return color encoded as follows: alpha<<24 | red<<16 | green<<8 | blue
146      */

147     public int getRGBA() {
148         return a<<24 | getRGB();
149     }
150
151     public void setAlpha( int a ) {
152         this.a = a;
153     }
154
155     /**
156      * Sets color encoded into int
157      *
158      * @param rgba color encoded as folows: alpha<<24 | red<<16 | green<<8 | blue
159      */

160     public void setRGBA( int rgba ) {
161         setRGB( rgba );
162         this.a = (rgba&0xff000000)>>>24;
163     }
164
165     /**
166      * Writes object into flash buffer
167      *
168      * @param fob buffer to write
169      */

170     public void write( FlashOutput fob ) {
171         writeRGBA( fob );
172     }
173
174     public void writeRGBA( FlashOutput fob ) {
175         super.writeRGB( fob );
176         fob.writeByte( a );
177     }
178
179     public void printContent( PrintStream out, String JavaDoc indent ) {
180         out.println( indent+"AlphaColor: (0x"+Util.b2h(r)+",0x"+Util.b2h(g)+",0x"+Util.b2h(b)+",0x"+Util.b2h(a)+")" );
181     }
182
183     public static AlphaColor parse( Parser p ) {
184         return parseRGBA(p);
185     }
186
187     /**
188      * Skips color without parsing
189      *
190      * @param p parser to skip in
191      */

192     public static void skip( Parser p ) {
193         p.skip(4);
194     }
195
196     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
197         super.copyInto( item, copier );
198         ((AlphaColor)item).a = a;
199         return item;
200     }
201
202     public FlashItem getCopy( ScriptCopier copier ) {
203         return copyInto( new AlphaColor(), copier );
204     }
205
206     public boolean equals( Object JavaDoc o ) {
207         if( o instanceof AlphaColor ) {
208             AlphaColor c = (AlphaColor) o;
209             return r == c.r && g == c.g && b == c.b && a == c.a;
210         } else
211             return a == 255 && super.equals( o );
212     }
213
214     /**
215      * Returns true if this color has alpha<BR>
216      * Always returns true
217      *
218      * @return true
219      */

220     public boolean hasAlpha() { return true; }
221
222     /**
223      * Creates new AlphaColor from this one with reduced alpha component by half
224      *
225      * @return reduced color
226      */

227     public AlphaColor getReducedColor() {
228         double k = 0.5;
229         // return new Color( (int)(r*k), (int)(g*k), (int)(b*k), a );
230
return new AlphaColor( r, g, b, (int)(a*k) );
231     }
232
233     /**
234      * Returns color by name
235      *
236      * @param name name of the color<BR>
237      * see static initalizer
238      * @return color or null
239      */

240     public static AlphaColor getColor( String JavaDoc name ) {
241         return (AlphaColor) colors.get(name.toLowerCase());
242     }
243
244     public static Enumeration getAllColors() {
245         return colors.elements();
246     }
247
248     // static stuff
249
private static final Hashtable colors = new Hashtable();
250
251     private static void add( String JavaDoc name, int rgb ) {
252         AlphaColor c = new AlphaColor();
253         c.setRGB(rgb);
254         c.setAlpha(255);
255         colors.put(name.toLowerCase(), c);
256     }
257
258     static {
259         add("black",0x000000);
260         add("navy",0x000080);
261         add("blue",0x0000FF);
262         add("green",0x008000);
263         add("dark cyan",0x008B8B);
264         add("dark turquoise",0x00CED1);
265         add("lime",0x00FF00);
266         add("aqua",0x00FFFF);
267         add("midnight blue",0x191970);
268         add("light seagreen",0x20B2AA);
269         add("forest green",0x228B22);
270         add("lime green",0x32CD32);
271         add("turquoise",0x40E0D0);
272         add("dark slateblue",0x4682B4);
273         add("medium turquoise",0x48D1CC);
274         add("cadet blue",0x5F9EA0);
275         add("medium aquamarine",0x66CDAA);
276         add("slate blue",0x6A5ACD);
277         add("slate gray",0x708090);
278         add("medium slateblue",0x7B68EE);
279         add("chartreuse",0x7FFF00);
280         add("maroon",0x800000);
281         add("olive",0x80800);
282         add("sky blue",0x87CEEB);
283         add("dark red",0x8B0000);
284         add("saddle brown",0x8B4513);
285         add("light green",0x90EE90);
286         add("dark violet",0x9400D3);
287         add("dark orchid",0x9932CC);
288         add("sienna",0xA0522D);
289         add("dark gray",0xA9A9A9);
290         add("green yellow",0xADFF2F);
291         add("pale turquoise",0xAFEEEE);
292         add("firebrick",0xB22222);
293         add("dark goldenrod",0xB8860B);
294         add("medium orchid",0xBA55D);
295         add("medium violetred",0xC71585);
296         add("indian red",0xCD5C5C);
297         add("peru",0xCD853F);
298         add("thistle",0xD8BFD8);
299         add("orchid",0xDA70D6);
300         add("goldenrod",0xDAA520);
301         add("pale violetred",0xDB7093);
302         add("crimson",0xDC143C);
303         add("dark salmon",0xE9967A);
304         add("violet",0xEE82EE);
305         add("pale goldenrod",0xEEE8AA);
306         add("light coral",0xF08080);
307         add("sandy brown",0xF4A460);
308         add("wheat",0xF5DEB3);
309         add("ghost white",0xF8F8FF);
310         add("salmon",0xFA8072);
311         add("antique white",0xFAEBD7);
312         add("red",0xFF0000);
313         add("fuchsia",0xFF00FF);
314         add("magenta",0xFF00FF);
315         add("hot pink",0xFF69B4);
316         add("coral",0xFF7F50);
317         add("dark orange",0xFF8C00);
318         add("light salmon",0xFFA07A);
319         add("orange",0xFFA500);
320         add("light pink",0xFFB6C1);
321         add("pink",0xFFC0CB);
322         add("papaya whip",0xFFEFD5);
323         add("lavender blush",0xFFF0F5);
324         add("seashel",0xFFF5EE);
325         add("snow",0xFFFAFA);
326         add("yellow",0xFFFF00);
327         add("white",0xFFFFFF);
328         add("dark blue",0x00008B);
329         add("medium blue",0x0000CD);
330         add("dark green",0x006400);
331         add("teal",0x008080);
332         add("deep skyblue",0x00BFFF);
333         add("medium springgreen",0x00FA9A);
334         add("spring green",0x00FF7F);
335         add("cyan",0x00FFFF);
336         add("dodger blue",0x1E90FF);
337         add("sea green",0x2E8B57);
338         add("dark slategray",0x2F4F4F);
339         add("medium seagreen",0x3CB371);
340         add("royal blue",0x4169E1);
341         add("steel blue",0x4682B4);
342         add("indigo",0x4B0082);
343         add("cornflower blue",0x6495ED);
344         add("dim gray",0x696969);
345         add("olive drab",0x6B8E23);
346         add("light slategray",0x778899);
347         add("lawn green",0x7CFC00);
348         add("aquamarine",0x7FFFD4);
349         add("purple",0x800080);
350         add("gray",0x808080);
351         add("blue violet",0x87CEFA);
352         add("dark magenta",0x8B008B);
353         add("dark seagreen",0x8FBC8F);
354         add("medium purple",0x9370DB);
355         add("pale green",0x98FB98);
356         add("yellow green",0x9ACD32);
357         add("brown",0xA52A2A);
358         add("light blue",0xADD8E6);
359         add("light steelblue",0xB0C4DE);
360         add("powder blue",0xB0E0E6);
361         add("rosy brown",0xBC8F8F);
362         add("dark khaki",0xBC8F8F);
363         add("silver",0xC0C0C0);
364         add("chocolate",0xD2691E);
365         add("tan",0xD2B48C);
366         add("light grey",0xD3D3D3);
367         add("gainsboro",0xDCDCDC);
368         add("plum",0xDDA0DD);
369         add("burlywood",0xDEB887);
370         add("light cyan",0xE0FFFF);
371         add("lavender",0xE6E6FA);
372         add("khaki",0xF0E68C);
373         add("alice blue",0xF0F8FF);
374         add("honeydew",0xF0FFF0);
375         add("azure",0xF0FFFF);
376         add("white smoke",0xF5F5F5);
377         add("mint cream",0xF5FFFA);
378         add("linen",0xFAF0E6);
379         add("light goldenrodyellow",0xFAFAD2);
380         add("old lace",0xFDF5E6);
381         add("deep pink",0xFF1493);
382         add("orange red",0xFF4500);
383         add("tomato",0xFF6347);
384         add("gold",0xFFD700);
385         add("peach puff",0xFFDAB9);
386         add("navajo white",0xFFDEAD);
387         add("moccasin",0xFFE4B5);
388         add("bisque",0xFFE4C4);
389         add("misty rose",0xFFE4E1);
390         add("blanched almond",0xFFEBCD);
391         add("cornsilk",0xFFF8DC);
392         add("lemon chiffon",0xFFFACD);
393         add("floral white",0xFFFAF0);
394         add("light yellow",0xFFFFE0);
395         add("ivory",0xFFFFF0);
396     }
397
398 }
399
Popular Tags