KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > PDFColor


1 /*
2  * $Id: PDFColor.java,v 1.10.2.3 2003/02/25 14:29:37 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.pdf;
52
53 // Java
54
import java.util.ArrayList JavaDoc;
55
56 // FOP
57
import org.apache.fop.datatypes.ColorSpace;
58
59 public class PDFColor extends PDFPathPaint {
60     protected static double blackFactor = 2.0; // could be 3.0 as well.
61
protected double red = -1.0;
62     protected double green = -1.0;
63     protected double blue = -1.0;
64
65     protected double cyan = -1.0;
66     protected double magenta = -1.0;
67     protected double yellow = -1.0;
68     protected double black = -1.0;
69
70     public PDFColor(org.apache.fop.datatypes.ColorType theColor) {
71         this.colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
72         // super(theNumber)
73
this.red = (double)theColor.red();
74         this.green = (double)theColor.green();
75         this.blue = (double)theColor.blue();
76
77     }
78
79     public PDFColor(double theRed, double theGreen, double theBlue) {
80         // super(theNumber);
81
this.colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB);
82
83         this.red = theRed;
84         this.green = theGreen;
85         this.blue = theBlue;
86     }
87
88     // components from 0 to 255
89
public PDFColor(int theRed, int theGreen, int theBlue) {
90         this(((double)theRed) / 255d, ((double)theGreen) / 255d,
91              ((double)theBlue) / 255d);
92
93     }
94
95     public PDFColor(double theCyan, double theMagenta, double theYellow,
96                     double theBlack) {
97         // super(theNumber);//?
98

99         this.colorSpace = new ColorSpace(ColorSpace.DEVICE_CMYK);
100
101         this.cyan = theCyan;
102         this.magenta = theMagenta;
103         this.yellow = theYellow;
104         this.black = theBlack;
105     }
106
107
108     public ArrayList JavaDoc getVector() { // return a vector representation of the color
109
// in the appropriate colorspace.
110
ArrayList JavaDoc theColorVector = new ArrayList JavaDoc();
111         if (this.colorSpace.getColorSpace() == ColorSpace.DEVICE_RGB) { // RGB
112
theColorVector.add(new Double JavaDoc(this.red));
113             theColorVector.add(new Double JavaDoc(this.green));
114             theColorVector.add(new Double JavaDoc(this.blue));
115         } else if (this.colorSpace.getColorSpace()
116                    == ColorSpace.DEVICE_CMYK) { // CMYK
117
theColorVector.add(new Double JavaDoc(this.cyan));
118             theColorVector.add(new Double JavaDoc(this.magenta));
119             theColorVector.add(new Double JavaDoc(this.yellow));
120             theColorVector.add(new Double JavaDoc(this.black));
121         } else { // GRAY
122
theColorVector.add(new Double JavaDoc(this.black));
123         }
124         return (theColorVector);
125     }
126
127     public double red() {
128         return (this.red);
129     }
130
131     public double green() {
132         return (this.green);
133     }
134
135     public double blue() {
136         return (this.blue);
137     }
138
139     public int red255() {
140         return (int)(this.red * 255d);
141     }
142
143     public int green255() {
144         return (int)(this.green * 255d);
145     }
146
147     public int blue255() {
148         return (int)(this.blue * 255d);
149     }
150
151     public double cyan() {
152         return (this.cyan);
153     }
154
155     public double magenta() {
156         return (this.magenta);
157     }
158
159     public double yellow() {
160         return (this.yellow);
161     }
162
163     public double black() {
164         return (this.black);
165     }
166
167     public void setColorSpace(int theColorSpace) {
168         int theOldColorSpace = this.colorSpace.getColorSpace();
169         if (theOldColorSpace != theColorSpace) {
170             if (theOldColorSpace == ColorSpace.DEVICE_RGB) {
171                 if (theColorSpace == ColorSpace.DEVICE_CMYK) {
172                     this.convertRGBtoCMYK();
173                 } else // convert to Gray?
174
{
175                     this.convertRGBtoGRAY();
176                 }
177
178             } else if (theOldColorSpace == ColorSpace.DEVICE_CMYK) {
179                 if (theColorSpace == ColorSpace.DEVICE_RGB) {
180                     this.convertCMYKtoRGB();
181                 } else // convert to Gray?
182
{
183                     this.convertCMYKtoGRAY();
184                 }
185             } else // used to be Gray
186
{
187                 if (theColorSpace == ColorSpace.DEVICE_RGB) {
188                     this.convertGRAYtoRGB();
189                 } else // convert to CMYK?
190
{
191                     this.convertGRAYtoCMYK();
192                 }
193             }
194             this.colorSpace.setColorSpace(theColorSpace);
195         }
196     }
197
198     public String JavaDoc getColorSpaceOut(boolean fillNotStroke) {
199         StringBuffer JavaDoc p = new StringBuffer JavaDoc("");
200
201         double tempDouble;
202
203         if (this.colorSpace.getColorSpace()
204                 == ColorSpace.DEVICE_RGB) { // colorspace is RGB
205
// according to pdfspec 12.1 p.399
206
// if the colors are the same then just use the g or G operator
207
boolean same = false;
208             if (this.red == this.green && this.red == this.blue) {
209                 same = true;
210             }
211             // output RGB
212
if (fillNotStroke) { // fill
213
if (same) {
214                     p.append(PDFNumber.doubleOut(this.red) + " g\n");
215                 } else {
216                     p.append(PDFNumber.doubleOut(this.red) + " "
217                              + PDFNumber.doubleOut(this.green) + " "
218                              + PDFNumber.doubleOut(this.blue) + " "
219                              + " rg \n");
220                 }
221             } else { // stroke/border
222
if (same) {
223                     p.append(PDFNumber.doubleOut(this.red) + " G\n");
224                 } else {
225                     p.append(PDFNumber.doubleOut(this.red) + " "
226                              + PDFNumber.doubleOut(this.green) + " "
227                              + PDFNumber.doubleOut(this.blue) + " "
228                              + " RG \n");
229                 }
230             }
231         } // end of output RGB
232
else if (this.colorSpace.getColorSpace()
233                   == ColorSpace.DEVICE_CMYK) { // colorspace is CMYK
234

235             if (fillNotStroke) { // fill
236
p.append(PDFNumber.doubleOut(this.cyan) + " "
237                          + PDFNumber.doubleOut(this.magenta) + " "
238                          + PDFNumber.doubleOut(this.yellow) + " "
239                          + PDFNumber.doubleOut(this.black) + " k \n");
240             } else { // fill
241
p.append(PDFNumber.doubleOut(this.cyan) + " "
242                          + PDFNumber.doubleOut(this.magenta) + " "
243                          + PDFNumber.doubleOut(this.yellow) + " "
244                          + PDFNumber.doubleOut(this.black) + " K \n");
245             }
246
247         } // end of if CMYK
248
else { // means we're in DeviceGray or Unknown.
249
// assume we're in DeviceGray, because otherwise we're screwed.
250

251             if (fillNotStroke) {
252                 p.append(PDFNumber.doubleOut(this.black) + " g \n");
253             } else {
254                 p.append(PDFNumber.doubleOut(this.black) + " G \n");
255             }
256
257         }
258         return (p.toString());
259     }
260
261
262
263
264     protected void convertCMYKtoRGB() {
265         // convert CMYK to RGB
266
this.red = 1.0 - this.cyan;
267         this.green = 1.0 - this.green;
268         this.blue = 1.0 - this.yellow;
269
270         this.red = (this.black / this.blackFactor) + this.red;
271         this.green = (this.black / this.blackFactor) + this.green;
272         this.blue = (this.black / this.blackFactor) + this.blue;
273
274     }
275
276     protected void convertRGBtoCMYK() {
277         // convert RGB to CMYK
278
this.cyan = 1.0 - this.red;
279         this.magenta = 1.0 - this.green;
280         this.yellow = 1.0 - this.blue;
281
282         this.black = 0.0;
283         /*
284          * If you want to calculate black, uncomment this
285          * //pick the lowest color
286          * tempDouble = this.red;
287          *
288          * if (this.green < tempDouble)
289          * tempDouble = this.green;
290          *
291          * if (this.blue < tempDouble)
292          * tempDouble = this.blue;
293          *
294          * this.black = tempDouble / this.blackFactor;
295          */

296     }
297
298     protected void convertGRAYtoRGB() {
299         this.red = 1.0 - this.black;
300         this.green = 1.0 - this.black;
301         this.blue = 1.0 - this.black;
302     }
303
304     protected void convertGRAYtoCMYK() {
305         this.cyan = this.black;
306         this.magenta = this.black;
307         this.yellow = this.black;
308         // this.black=0.0;//?
309
}
310
311     protected void convertCMYKtoGRAY() {
312         double tempDouble = 0.0;
313
314         // pick the lowest color
315
tempDouble = this.cyan;
316
317         if (this.magenta < tempDouble)
318             tempDouble = this.magenta;
319
320         if (this.yellow < tempDouble)
321             tempDouble = this.yellow;
322
323         this.black = (tempDouble / this.blackFactor);
324
325     }
326
327     protected void convertRGBtoGRAY() {
328         double tempDouble = 0.0;
329
330         // pick the lowest color
331
tempDouble = this.red;
332
333         if (this.green < tempDouble)
334             tempDouble = this.green;
335
336         if (this.blue < tempDouble)
337             tempDouble = this.blue;
338
339         this.black = 1.0 - (tempDouble / this.blackFactor);
340
341     }
342
343     byte[] toPDF() {
344         return (new byte[0]);
345
346     } // end of toPDF
347

348     public boolean equals(Object JavaDoc obj) {
349         if (!(obj instanceof PDFColor)) {
350             return false;
351         }
352         PDFColor color = (PDFColor)obj;
353
354         if (color.red == this.red && color.green == this.green
355                 && color.blue == this.blue) {
356             return true;
357         }
358         return false;
359     }
360
361 }
362
Popular Tags