KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Color.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  * Opaque RGB Color without alpha value
61  *
62  * @author Dmitry Skavish
63  * @see AlphaColor
64  */

65 public class Color extends FlashItem {
66
67     protected int r;
68     protected int g;
69     protected int b;
70
71     public Color() {}
72
73     /**
74      * Creates color from int
75      *
76      * @param rgb color encoded as folows: red<<16 | green<<8 | blue
77      */

78     public Color( int rgb ) {
79         setRGB( rgb );
80     }
81
82     /**
83      * Creates Color from three integer
84      * components each on range 0, 255
85      *
86      * @param r red
87      * @param g green
88      * @param b blue
89      */

90     public Color( int r, int g, int b ) {
91         this.r = r;
92         this.g = g;
93         this.b = b;
94     }
95
96     /**
97      * Creates Color from three float
98      * components each on range 0.0, 1.0
99      *
100      * @param r red
101      * @param g green
102      * @param b blue
103      */

104     public Color( float r, float g, float b ) {
105         this.r = Math.round( r * 255 );
106         this.g = Math.round( g * 255 );
107         this.b = Math.round( b * 255 );
108     }
109
110     public int getRed() {
111         return r;
112     }
113
114     public int getGreen() {
115         return g;
116     }
117
118     public int getBlue() {
119         return b;
120     }
121
122     public int getAlpha() {
123         return 255;
124     }
125
126     /**
127      * Returns color encoded as int
128      *
129      * @return color encoded as follows: red<<16 | green<<8 | blue
130      */

131     public int getRGB() {
132         return r<<16 | g<<8 | b;
133     }
134
135     public void setRed( int r ) {
136         this.r = r;
137     }
138     public void setGreen( int g ) {
139         this.g = g;
140     }
141     public void setBlue( int b ) {
142         this.b = b;
143     }
144
145     /**
146      * Sets color encoded as int
147      *
148      * @param rgb color encoded as folows: red<<16 | green<<8 | blue
149      */

150     public void setRGB( int rgb ) {
151         r = (rgb&0x00ff0000)>>16;
152         g = (rgb&0x0000ff00)>>8;
153         b = (rgb&0x000000ff);
154     }
155
156     /**
157      * Returns true or false depending on whether this object has alpha or not
158      */

159     public boolean hasAlpha() { return false; }
160
161     /**
162      * Returns new color with changed brightness
163      *
164      * @param f brightness change:
165      * <UL>
166      * <LI>[-1..0] - makes it darker
167      * <LI>[0..1] - makes it brighter
168      * </UL>
169      * @return new color
170      */

171     public Color brighter( double f ) {
172         return CXForm.newBrightness(f, hasAlpha()).transform(this);
173     }
174
175     /**
176      * Returns java.awt.Color corresponding to this one
177      */

178     public java.awt.Color JavaDoc getAWTColor() {
179         java.awt.Color JavaDoc color;
180         if( hasAlpha() ) {
181             color = new java.awt.Color JavaDoc(r, g, b, getAlpha());
182         } else {
183             color = new java.awt.Color JavaDoc(r, g, b);
184         }
185         return color;
186     }
187
188     public void write( FlashOutput fob ) {
189         writeRGB( fob );
190     }
191
192     /**
193      * Writes RGB color to FlashBuffer
194      *
195      * @param fob flash buffer to write
196      */

197     public void writeRGB( FlashOutput fob ) {
198         fob.writeByte( r );
199         fob.writeByte( g );
200         fob.writeByte( b );
201     }
202
203     /**
204      * Writes RGBA color to FlashBuffer<BR>
205      * Alpha is 255, i.e. color is opaque
206      *
207      * @param fob flash buffer to write
208      */

209     public void writeRGBA( FlashOutput fob ) {
210         fob.writeByte( r );
211         fob.writeByte( g );
212         fob.writeByte( b );
213         fob.writeByte( 255 );
214     }
215
216     public void printContent( PrintStream out, String JavaDoc indent ) {
217         out.println( indent+"Color: (0x"+Util.b2h(r)+",0x"+Util.b2h(g)+",0x"+Util.b2h(b)+")" );
218     }
219
220     /**
221      * Parses color as RGB
222      *
223      * @param p Parser to parse from
224      * @return always Color
225      */

226     public static Color parseRGB( Parser p ) {
227         return new Color(p.getUByte(),p.getUByte(),p.getUByte());
228     }
229
230     /**
231      * Parses color as RGBA
232      *
233      * @param p Parser to parse from
234      * @return always AlphaColor
235      */

236     public static AlphaColor parseRGBA( Parser p ) {
237         return new AlphaColor(p.getUByte(),p.getUByte(),p.getUByte(),p.getUByte());
238     }
239
240     /**
241      * Parses color
242      *
243      * @param p parser to parse from
244      * @param withAlpha if true then parse as AlphaColor<BR>
245      * if false then parse as Color
246      * @return Color or AlphaColor depending on parameter withAlpha
247      */

248     public static Color parse( Parser p, boolean withAlpha ) {
249         if( withAlpha ) return parseRGBA(p);
250         return parseRGB(p);
251     }
252
253     public static void skip( Parser p ) {
254         p.skip(3);
255     }
256
257     public static void skip( Parser p, boolean withAlpha ) {
258         if( withAlpha ) p.skip(4);
259         else p.skip(3);
260     }
261
262     protected FlashItem copyInto( FlashItem item, ScriptCopier copier ) {
263         super.copyInto( item, copier );
264         ((Color)item).r = r;
265         ((Color)item).g = g;
266         ((Color)item).b = b;
267         return item;
268     }
269     public FlashItem getCopy( ScriptCopier copier ) {
270         return copyInto( new Color(), copier );
271     }
272
273     public boolean equals( Object JavaDoc o ) {
274         if( o instanceof Color ) {
275             Color c = (Color) o;
276             return r == c.r && g == c.g && b == c.b;
277         }
278         return false;
279     }
280
281 }
282
Popular Tags