KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > graphics > color > PDColorSpaceInstance


1 /**
2  * Copyright (c) 2004-2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.pdmodel.graphics.color;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.color.ColorSpace JavaDoc;
35 import java.io.IOException JavaDoc;
36
37 import org.pdfbox.cos.COSArray;
38 import org.pdfbox.cos.COSFloat;
39
40 /**
41  * This class represents a color space and the color value for that colorspace.
42  *
43  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
44  * @version $Revision: 1.6 $
45  */

46 public class PDColorSpaceInstance
47 {
48     private PDColorSpace colorSpace = new PDDeviceGray();
49     private COSArray colorSpaceValue = new COSArray();
50     
51     /**
52      * Default constructor.
53      *
54      */

55     public PDColorSpaceInstance()
56     {
57         colorSpaceValue.add( new COSFloat( 0 ) );
58     }
59     
60     /**
61      * Create the current color from the colorspace and values.
62      * @return The current awt color.
63      * @throws IOException If there is an error creating the color.
64      */

65     public Color JavaDoc createColor() throws IOException JavaDoc
66     {
67         Color JavaDoc retval = null;
68         float[] components = colorSpaceValue.toFloatArray();
69         if( components.length == 3 )
70         {
71             //for some reason, when using RGB and the RGB colorspace
72
//the new Color doesn't maintain exactly the same values
73
//I think some color conversion needs to take place first
74
//for now we will just make rgb a special case.
75
retval = new Color JavaDoc( components[0], components[1], components[2] );
76         }
77         else
78         {
79             ColorSpace JavaDoc cs = colorSpace.createColorSpace();
80             retval = new Color JavaDoc( cs, components, 1f );
81         }
82         return retval;
83     }
84     
85     /**
86      * Constructor with an existing color set. Default colorspace is PDDeviceGray.
87      *
88      * @param csValues The color space values.
89      */

90     public PDColorSpaceInstance( COSArray csValues )
91     {
92         colorSpaceValue = csValues;
93     }
94
95
96     /**
97      * This will get the current colorspace.
98      *
99      * @return The current colorspace.
100      */

101     public PDColorSpace getColorSpace()
102     {
103         return colorSpace;
104     }
105
106     /**
107      * This will set the current colorspace.
108      *
109      * @param value The new colorspace.
110      */

111     public void setColorSpace(PDColorSpace value)
112     {
113         colorSpace = value;
114     }
115
116     /**
117      * This will get the color space values. Either 1 for gray or 3 for RGB.
118      *
119      * @return The colorspace values.
120      */

121     public float[] getColorSpaceValue()
122     {
123         return colorSpaceValue.toFloatArray();
124     }
125     
126     /**
127      * This will get the color space values. Either 1 for gray or 3 for RGB.
128      *
129      * @return The colorspace values.
130      */

131     public COSArray getCOSColorSpaceValue()
132     {
133         return colorSpaceValue;
134     }
135
136     /**
137      * This will update the colorspace values.
138      *
139      * @param value The new colorspace values.
140      */

141     public void setColorSpaceValue(float[] value)
142     {
143         colorSpaceValue.setFloatArray( value );
144     }
145 }
146
Popular Tags