001: /*
002: * $Id: Color.java,v 1.2 2002/02/15 23:44:27 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.api;
052:
053: import java.io.*;
054: import java.util.*;
055: import java.lang.Math;
056: import org.openlaszlo.iv.flash.parser.*;
057: import org.openlaszlo.iv.flash.util.*;
058:
059: /**
060: * Opaque RGB Color without alpha value
061: *
062: * @author Dmitry Skavish
063: * @see AlphaColor
064: */
065: public class Color extends FlashItem {
066:
067: protected int r;
068: protected int g;
069: protected int b;
070:
071: public Color() {
072: }
073:
074: /**
075: * Creates color from int
076: *
077: * @param rgb color encoded as folows: red<<16 | green<<8 | blue
078: */
079: public Color(int rgb) {
080: setRGB(rgb);
081: }
082:
083: /**
084: * Creates Color from three integer
085: * components each on range 0, 255
086: *
087: * @param r red
088: * @param g green
089: * @param b blue
090: */
091: public Color(int r, int g, int b) {
092: this .r = r;
093: this .g = g;
094: this .b = b;
095: }
096:
097: /**
098: * Creates Color from three float
099: * components each on range 0.0, 1.0
100: *
101: * @param r red
102: * @param g green
103: * @param b blue
104: */
105: public Color(float r, float g, float b) {
106: this .r = Math.round(r * 255);
107: this .g = Math.round(g * 255);
108: this .b = Math.round(b * 255);
109: }
110:
111: public int getRed() {
112: return r;
113: }
114:
115: public int getGreen() {
116: return g;
117: }
118:
119: public int getBlue() {
120: return b;
121: }
122:
123: public int getAlpha() {
124: return 255;
125: }
126:
127: /**
128: * Returns color encoded as int
129: *
130: * @return color encoded as follows: red<<16 | green<<8 | blue
131: */
132: public int getRGB() {
133: return r << 16 | g << 8 | b;
134: }
135:
136: public void setRed(int r) {
137: this .r = r;
138: }
139:
140: public void setGreen(int g) {
141: this .g = g;
142: }
143:
144: public void setBlue(int b) {
145: this .b = b;
146: }
147:
148: /**
149: * Sets color encoded as int
150: *
151: * @param rgb color encoded as folows: red<<16 | green<<8 | blue
152: */
153: public void setRGB(int rgb) {
154: r = (rgb & 0x00ff0000) >> 16;
155: g = (rgb & 0x0000ff00) >> 8;
156: b = (rgb & 0x000000ff);
157: }
158:
159: /**
160: * Returns true or false depending on whether this object has alpha or not
161: */
162: public boolean hasAlpha() {
163: return false;
164: }
165:
166: /**
167: * Returns new color with changed brightness
168: *
169: * @param f brightness change:
170: * <UL>
171: * <LI>[-1..0] - makes it darker
172: * <LI>[0..1] - makes it brighter
173: * </UL>
174: * @return new color
175: */
176: public Color brighter(double f) {
177: return CXForm.newBrightness(f, hasAlpha()).transform(this );
178: }
179:
180: /**
181: * Returns java.awt.Color corresponding to this one
182: */
183: public java.awt.Color getAWTColor() {
184: java.awt.Color color;
185: if (hasAlpha()) {
186: color = new java.awt.Color(r, g, b, getAlpha());
187: } else {
188: color = new java.awt.Color(r, g, b);
189: }
190: return color;
191: }
192:
193: public void write(FlashOutput fob) {
194: writeRGB(fob);
195: }
196:
197: /**
198: * Writes RGB color to FlashBuffer
199: *
200: * @param fob flash buffer to write
201: */
202: public void writeRGB(FlashOutput fob) {
203: fob.writeByte(r);
204: fob.writeByte(g);
205: fob.writeByte(b);
206: }
207:
208: /**
209: * Writes RGBA color to FlashBuffer<BR>
210: * Alpha is 255, i.e. color is opaque
211: *
212: * @param fob flash buffer to write
213: */
214: public void writeRGBA(FlashOutput fob) {
215: fob.writeByte(r);
216: fob.writeByte(g);
217: fob.writeByte(b);
218: fob.writeByte(255);
219: }
220:
221: public void printContent(PrintStream out, String indent) {
222: out.println(indent + "Color: (0x" + Util.b2h(r) + ",0x"
223: + Util.b2h(g) + ",0x" + Util.b2h(b) + ")");
224: }
225:
226: /**
227: * Parses color as RGB
228: *
229: * @param p Parser to parse from
230: * @return always Color
231: */
232: public static Color parseRGB(Parser p) {
233: return new Color(p.getUByte(), p.getUByte(), p.getUByte());
234: }
235:
236: /**
237: * Parses color as RGBA
238: *
239: * @param p Parser to parse from
240: * @return always AlphaColor
241: */
242: public static AlphaColor parseRGBA(Parser p) {
243: return new AlphaColor(p.getUByte(), p.getUByte(), p.getUByte(),
244: p.getUByte());
245: }
246:
247: /**
248: * Parses color
249: *
250: * @param p parser to parse from
251: * @param withAlpha if true then parse as AlphaColor<BR>
252: * if false then parse as Color
253: * @return Color or AlphaColor depending on parameter withAlpha
254: */
255: public static Color parse(Parser p, boolean withAlpha) {
256: if (withAlpha)
257: return parseRGBA(p);
258: return parseRGB(p);
259: }
260:
261: public static void skip(Parser p) {
262: p.skip(3);
263: }
264:
265: public static void skip(Parser p, boolean withAlpha) {
266: if (withAlpha)
267: p.skip(4);
268: else
269: p.skip(3);
270: }
271:
272: protected FlashItem copyInto(FlashItem item, ScriptCopier copier) {
273: super .copyInto(item, copier);
274: ((Color) item).r = r;
275: ((Color) item).g = g;
276: ((Color) item).b = b;
277: return item;
278: }
279:
280: public FlashItem getCopy(ScriptCopier copier) {
281: return copyInto(new Color(), copier);
282: }
283:
284: public boolean equals(Object o) {
285: if (o instanceof Color) {
286: Color c = (Color) o;
287: return r == c.r && g == c.g && b == c.b;
288: }
289: return false;
290: }
291:
292: }
|