KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > style > RtfColor


1 /*
2  * $Id: RtfColor.java 2776 2007-05-23 20:01:40Z hallm $
3  * $Name$
4  *
5  * Copyright 2001, 2002, 2003, 2004 by Mark Hall
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.rtf.style;
52
53 import java.awt.Color JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.OutputStream JavaDoc;
57
58 import com.lowagie.text.rtf.RtfElement;
59 import com.lowagie.text.rtf.RtfExtendedElement;
60 import com.lowagie.text.rtf.document.RtfDocument;
61
62
63 /**
64  * The RtfColor stores one rtf color value for a rtf document
65  *
66  * @version $Id: RtfColor.java 2776 2007-05-23 20:01:40Z hallm $
67  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
68  * @author Thomas Bickel (tmb99@inode.at)
69  */

70 public class RtfColor extends RtfElement implements RtfExtendedElement {
71
72     /**
73      * Constant for RED value
74      */

75     private static final byte[] COLOR_RED = "\\red".getBytes();
76     /**
77      * Constant for GREEN value
78      */

79     private static final byte[] COLOR_GREEN = "\\green".getBytes();
80     /**
81      * Constant for BLUE value
82      */

83     private static final byte[] COLOR_BLUE = "\\blue".getBytes();
84     /**
85      * Constant for the end of one color entry
86      */

87     private static final byte COLON = (byte) ';';
88     /**
89      * Constant for the number of the colour in the list of colours
90      */

91     private static final byte[] COLOR_NUMBER = "\\cf".getBytes();
92
93     /**
94      * The number of the colour in the list of colours
95      */

96     private int colorNumber = 0;
97     /**
98      * The red value
99      */

100     private int red = 0;
101     /**
102      * The green value
103      */

104     private int green = 0;
105     /**
106      * The blue value
107      */

108     private int blue = 0;
109     
110     /**
111      * Constructor only for use when initializing the RtfColorList
112      *
113      * @param doc The RtfDocument this RtfColor belongs to
114      * @param red The red value to use
115      * @param green The green value to use
116      * @param blue The blue value to use
117      * @param colorNumber The number of the colour in the colour list
118      */

119     protected RtfColor(RtfDocument doc, int red, int green, int blue, 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().getColorNumber(this);
142         }
143     }
144     
145     /**
146      * Constructs a RtfColor based on the Color
147      *
148      * @param doc The RtfDocument this RtfColor belongs to
149      * @param col The Color to base this RtfColor on
150      */

151     public RtfColor(RtfDocument doc, Color JavaDoc col) {
152         super(doc);
153         if(col != null) {
154             this.red = col.getRed();
155             this.blue = col.getBlue();
156             this.green = col.getGreen();
157         }
158         if(this.document != null) {
159             this.colorNumber = this.document.getDocumentHeader().getColorNumber(this);
160         }
161     }
162     
163     /**
164      * Constructs a RtfColor based on the red/green/blue values
165      *
166      * @param doc The RtfDocument this RtfColor belongs to
167      * @param red The red value to use
168      * @param green The green value to use
169      * @param blue The blue value to use
170      */

171     public RtfColor(RtfDocument doc, int red, int green, int blue) {
172         super(doc);
173         this.red = red;
174         this.blue = blue;
175         this.green = green;
176         if(this.document != null) {
177             this.colorNumber = this.document.getDocumentHeader().getColorNumber(this);
178         }
179     }
180
181     /**
182      * unused
183      * @deprecated replaced by {@link #writeContent(OutputStream)}
184      */

185     public byte[] write()
186     {
187         return(new byte[0]);
188     }
189     /**
190      * unused
191      */

192     public void writeContent(final OutputStream JavaDoc out) throws IOException JavaDoc
193     {
194     }
195     
196     /**
197      * Write the definition part of this RtfColor.
198      *
199      * @return A byte array with the definition of this colour
200      * @deprecated replaced by {@link #writeDefinition(OutputStream)}
201      */

202     public byte[] writeDefinition() {
203         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
204         try {
205             writeDefinition(result);
206         } catch(IOException JavaDoc ioe) {
207             ioe.printStackTrace();
208         }
209         return result.toByteArray();
210     }
211     
212     /**
213      * Write the definition part of this RtfColor.
214      */

215     public void writeDefinition(final OutputStream JavaDoc result) throws IOException JavaDoc
216     {
217         result.write(COLOR_RED);
218         result.write(intToByteArray(red));
219         result.write(COLOR_GREEN);
220         result.write(intToByteArray(green));
221         result.write(COLOR_BLUE);
222         result.write(intToByteArray(blue));
223         result.write(COLON);
224     }
225
226     /**
227      * Writes the beginning of this RtfColor
228      *
229      * @return A byte array with the colour start data
230      */

231     public byte[] writeBegin() {
232         ByteArrayOutputStream JavaDoc result = new ByteArrayOutputStream JavaDoc();
233         try {
234             result.write(COLOR_NUMBER);
235             result.write(intToByteArray(colorNumber));
236         } catch(IOException JavaDoc ioe) {
237             ioe.printStackTrace();
238         }
239         return result.toByteArray();
240     }
241     
242     /**
243      * Unused
244      *
245      * @return An empty (<code>byte[0]</code>) byte array
246      */

247     public byte[] writeEnd() {
248         return new byte[0];
249     }
250     
251     /**
252      * Tests if this RtfColor is equal to another RtfColor.
253      *
254      * @param obj another RtfColor
255      * @return <code>True</code> if red, green and blue values of the two colours match,
256      * <code>false</code> otherwise.
257      */

258     public boolean equals(Object JavaDoc obj) {
259         if(!(obj instanceof RtfColor)) {
260             return false;
261         }
262         RtfColor color = (RtfColor) obj;
263         return (this.red == color.getRed() && this.green == color.getGreen() && this.blue == color.getBlue());
264     }
265
266     /**
267      * Returns the hash code of this RtfColor. The hash code is
268      * an integer with the lowest three bytes containing the values
269      * of red, green and blue.
270      *
271      * @return The hash code of this RtfColor
272      */

273     public int hashCode() {
274         return (this.red << 16) | (this.green << 8) | this.blue;
275     }
276     
277     /**
278      * Get the blue value of this RtfColor
279      *
280      * @return The blue value
281      */

282     public int getBlue() {
283         return blue;
284     }
285
286     /**
287      * Get the green value of this RtfColor
288      *
289      * @return The green value
290      */

291     public int getGreen() {
292         return green;
293     }
294
295     /**
296      * Get the red value of this RtfColor
297      *
298      * @return The red value
299      */

300     public int getRed() {
301         return red;
302     }
303     
304     /**
305      * Gets the number of this RtfColor in the list of colours
306      *
307      * @return Returns the colorNumber.
308      */

309     public int getColorNumber() {
310         return colorNumber;
311     }
312
313     /**
314      * Sets the RtfDocument this RtfColor belongs to
315      *
316      * @param doc The RtfDocument to use
317      */

318     public void setRtfDocument(RtfDocument doc) {
319         super.setRtfDocument(doc);
320         if(document != null) {
321             this.colorNumber = document.getDocumentHeader().getColorNumber(this);
322         }
323     }
324 }
325
Popular Tags