001: /*
002: * $Id: RtfColor.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf.style;
052:
053: import java.awt.Color;
054: import java.io.ByteArrayOutputStream;
055: import java.io.IOException;
056: import java.io.OutputStream;
057:
058: import com.lowagie.text.rtf.RtfElement;
059: import com.lowagie.text.rtf.RtfExtendedElement;
060: import com.lowagie.text.rtf.document.RtfDocument;
061:
062: /**
063: * The RtfColor stores one rtf color value for a rtf document
064: *
065: * @version $Id: RtfColor.java 2776 2007-05-23 20:01:40Z hallm $
066: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
067: * @author Thomas Bickel (tmb99@inode.at)
068: */
069: public class RtfColor extends RtfElement implements RtfExtendedElement {
070:
071: /**
072: * Constant for RED value
073: */
074: private static final byte[] COLOR_RED = "\\red".getBytes();
075: /**
076: * Constant for GREEN value
077: */
078: private static final byte[] COLOR_GREEN = "\\green".getBytes();
079: /**
080: * Constant for BLUE value
081: */
082: private static final byte[] COLOR_BLUE = "\\blue".getBytes();
083: /**
084: * Constant for the end of one color entry
085: */
086: private static final byte COLON = (byte) ';';
087: /**
088: * Constant for the number of the colour in the list of colours
089: */
090: private static final byte[] COLOR_NUMBER = "\\cf".getBytes();
091:
092: /**
093: * The number of the colour in the list of colours
094: */
095: private int colorNumber = 0;
096: /**
097: * The red value
098: */
099: private int red = 0;
100: /**
101: * The green value
102: */
103: private int green = 0;
104: /**
105: * The blue value
106: */
107: private int blue = 0;
108:
109: /**
110: * Constructor only for use when initializing the RtfColorList
111: *
112: * @param doc The RtfDocument this RtfColor belongs to
113: * @param red The red value to use
114: * @param green The green value to use
115: * @param blue The blue value to use
116: * @param colorNumber The number of the colour in the colour list
117: */
118: protected RtfColor(RtfDocument doc, int red, int green, int blue,
119: int colorNumber) {
120: super (doc);
121: this .red = red;
122: this .blue = blue;
123: this .green = green;
124: this .colorNumber = colorNumber;
125: }
126:
127: /**
128: * Constructs a RtfColor as a clone of an existing RtfColor
129: *
130: * @param doc The RtfDocument this RtfColor belongs to
131: * @param col The RtfColor to use as a base
132: */
133: public RtfColor(RtfDocument doc, RtfColor col) {
134: super (doc);
135: if (col != null) {
136: this .red = col.getRed();
137: this .green = col.getGreen();
138: this .blue = col.getBlue();
139: }
140: if (this .document != null) {
141: this .colorNumber = this .document.getDocumentHeader()
142: .getColorNumber(this );
143: }
144: }
145:
146: /**
147: * Constructs a RtfColor based on the Color
148: *
149: * @param doc The RtfDocument this RtfColor belongs to
150: * @param col The Color to base this RtfColor on
151: */
152: public RtfColor(RtfDocument doc, Color col) {
153: super (doc);
154: if (col != null) {
155: this .red = col.getRed();
156: this .blue = col.getBlue();
157: this .green = col.getGreen();
158: }
159: if (this .document != null) {
160: this .colorNumber = this .document.getDocumentHeader()
161: .getColorNumber(this );
162: }
163: }
164:
165: /**
166: * Constructs a RtfColor based on the red/green/blue values
167: *
168: * @param doc The RtfDocument this RtfColor belongs to
169: * @param red The red value to use
170: * @param green The green value to use
171: * @param blue The blue value to use
172: */
173: public RtfColor(RtfDocument doc, int red, int green, int blue) {
174: super (doc);
175: this .red = red;
176: this .blue = blue;
177: this .green = green;
178: if (this .document != null) {
179: this .colorNumber = this .document.getDocumentHeader()
180: .getColorNumber(this );
181: }
182: }
183:
184: /**
185: * unused
186: * @deprecated replaced by {@link #writeContent(OutputStream)}
187: */
188: public byte[] write() {
189: return (new byte[0]);
190: }
191:
192: /**
193: * unused
194: */
195: public void writeContent(final OutputStream out) throws IOException {
196: }
197:
198: /**
199: * Write the definition part of this RtfColor.
200: *
201: * @return A byte array with the definition of this colour
202: * @deprecated replaced by {@link #writeDefinition(OutputStream)}
203: */
204: public byte[] writeDefinition() {
205: ByteArrayOutputStream result = new ByteArrayOutputStream();
206: try {
207: writeDefinition(result);
208: } catch (IOException ioe) {
209: ioe.printStackTrace();
210: }
211: return result.toByteArray();
212: }
213:
214: /**
215: * Write the definition part of this RtfColor.
216: */
217: public void writeDefinition(final OutputStream result)
218: throws IOException {
219: result.write(COLOR_RED);
220: result.write(intToByteArray(red));
221: result.write(COLOR_GREEN);
222: result.write(intToByteArray(green));
223: result.write(COLOR_BLUE);
224: result.write(intToByteArray(blue));
225: result.write(COLON);
226: }
227:
228: /**
229: * Writes the beginning of this RtfColor
230: *
231: * @return A byte array with the colour start data
232: */
233: public byte[] writeBegin() {
234: ByteArrayOutputStream result = new ByteArrayOutputStream();
235: try {
236: result.write(COLOR_NUMBER);
237: result.write(intToByteArray(colorNumber));
238: } catch (IOException ioe) {
239: ioe.printStackTrace();
240: }
241: return result.toByteArray();
242: }
243:
244: /**
245: * Unused
246: *
247: * @return An empty (<code>byte[0]</code>) byte array
248: */
249: public byte[] writeEnd() {
250: return new byte[0];
251: }
252:
253: /**
254: * Tests if this RtfColor is equal to another RtfColor.
255: *
256: * @param obj another RtfColor
257: * @return <code>True</code> if red, green and blue values of the two colours match,
258: * <code>false</code> otherwise.
259: */
260: public boolean equals(Object obj) {
261: if (!(obj instanceof RtfColor)) {
262: return false;
263: }
264: RtfColor color = (RtfColor) obj;
265: return (this .red == color.getRed()
266: && this .green == color.getGreen() && this .blue == color
267: .getBlue());
268: }
269:
270: /**
271: * Returns the hash code of this RtfColor. The hash code is
272: * an integer with the lowest three bytes containing the values
273: * of red, green and blue.
274: *
275: * @return The hash code of this RtfColor
276: */
277: public int hashCode() {
278: return (this .red << 16) | (this .green << 8) | this .blue;
279: }
280:
281: /**
282: * Get the blue value of this RtfColor
283: *
284: * @return The blue value
285: */
286: public int getBlue() {
287: return blue;
288: }
289:
290: /**
291: * Get the green value of this RtfColor
292: *
293: * @return The green value
294: */
295: public int getGreen() {
296: return green;
297: }
298:
299: /**
300: * Get the red value of this RtfColor
301: *
302: * @return The red value
303: */
304: public int getRed() {
305: return red;
306: }
307:
308: /**
309: * Gets the number of this RtfColor in the list of colours
310: *
311: * @return Returns the colorNumber.
312: */
313: public int getColorNumber() {
314: return colorNumber;
315: }
316:
317: /**
318: * Sets the RtfDocument this RtfColor belongs to
319: *
320: * @param doc The RtfDocument to use
321: */
322: public void setRtfDocument(RtfDocument doc) {
323: super.setRtfDocument(doc);
324: if (document != null) {
325: this.colorNumber = document.getDocumentHeader()
326: .getColorNumber(this);
327: }
328: }
329: }
|