001: /**
002: * Copyright (c) 2004-2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.graphics.color;
031:
032: import java.awt.Color;
033: import java.awt.color.ColorSpace;
034: import java.io.IOException;
035:
036: import org.pdfbox.cos.COSArray;
037: import org.pdfbox.cos.COSFloat;
038:
039: /**
040: * This class represents a color space and the color value for that colorspace.
041: *
042: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
043: * @version $Revision: 1.6 $
044: */
045: public class PDColorSpaceInstance {
046: private PDColorSpace colorSpace = new PDDeviceGray();
047: private COSArray colorSpaceValue = new COSArray();
048:
049: /**
050: * Default constructor.
051: *
052: */
053: public PDColorSpaceInstance() {
054: colorSpaceValue.add(new COSFloat(0));
055: }
056:
057: /**
058: * Create the current color from the colorspace and values.
059: * @return The current awt color.
060: * @throws IOException If there is an error creating the color.
061: */
062: public Color createColor() throws IOException {
063: Color retval = null;
064: float[] components = colorSpaceValue.toFloatArray();
065: if (components.length == 3) {
066: //for some reason, when using RGB and the RGB colorspace
067: //the new Color doesn't maintain exactly the same values
068: //I think some color conversion needs to take place first
069: //for now we will just make rgb a special case.
070: retval = new Color(components[0], components[1],
071: components[2]);
072: } else {
073: ColorSpace cs = colorSpace.createColorSpace();
074: retval = new Color(cs, components, 1f);
075: }
076: return retval;
077: }
078:
079: /**
080: * Constructor with an existing color set. Default colorspace is PDDeviceGray.
081: *
082: * @param csValues The color space values.
083: */
084: public PDColorSpaceInstance(COSArray csValues) {
085: colorSpaceValue = csValues;
086: }
087:
088: /**
089: * This will get the current colorspace.
090: *
091: * @return The current colorspace.
092: */
093: public PDColorSpace getColorSpace() {
094: return colorSpace;
095: }
096:
097: /**
098: * This will set the current colorspace.
099: *
100: * @param value The new colorspace.
101: */
102: public void setColorSpace(PDColorSpace value) {
103: colorSpace = value;
104: }
105:
106: /**
107: * This will get the color space values. Either 1 for gray or 3 for RGB.
108: *
109: * @return The colorspace values.
110: */
111: public float[] getColorSpaceValue() {
112: return colorSpaceValue.toFloatArray();
113: }
114:
115: /**
116: * This will get the color space values. Either 1 for gray or 3 for RGB.
117: *
118: * @return The colorspace values.
119: */
120: public COSArray getCOSColorSpaceValue() {
121: return colorSpaceValue;
122: }
123:
124: /**
125: * This will update the colorspace values.
126: *
127: * @param value The new colorspace values.
128: */
129: public void setColorSpaceValue(float[] value) {
130: colorSpaceValue.setFloatArray(value);
131: }
132: }
|