1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: PDFDeviceColorSpace.java 1069439 2011-02-10 15:58:57Z jeremias $ */
19 
20 package org.apache.fop.pdf;
21 
22 import java.awt.color.ColorSpace;
23 
24 /**
25  * Represents a device-specific color space. Used for mapping DeviceRGB, DeviceCMYK and DeviceGray.
26  */
27 public class PDFDeviceColorSpace implements PDFColorSpace {
28 
29     private int numComponents;
30 
31     /**
32      * Unknown colorspace
33      */
34     public static final int DEVICE_UNKNOWN = -1;
35 
36     /**
37      * Gray colorspace
38      */
39     public static final int DEVICE_GRAY = 1;
40 
41     /**
42      * RGB colorspace
43      */
44     public static final int DEVICE_RGB = 2;
45 
46     /**
47      * CMYK colorspace
48      */
49     public static final int DEVICE_CMYK = 3;
50 
51     // Are there any others?
52 
53     /**
54      * Current color space value.
55      */
56     protected int currentColorSpace = DEVICE_UNKNOWN;
57 
58     /**
59      * Create a PDF colorspace object.
60      *
61      * @param theColorSpace the current colorspace
62      */
PDFDeviceColorSpace(int theColorSpace)63     public PDFDeviceColorSpace(int theColorSpace) {
64         this.currentColorSpace = theColorSpace;
65         numComponents = calculateNumComponents();
66     }
67 
calculateNumComponents()68     private int calculateNumComponents() {
69         if (currentColorSpace == DEVICE_GRAY) {
70             return 1;
71         } else if (currentColorSpace == DEVICE_RGB) {
72             return 3;
73         } else if (currentColorSpace == DEVICE_CMYK) {
74             return 4;
75         } else {
76             return 0;
77         }
78     }
79 
80     /**
81      * Set the current colorspace.
82      *
83      * @param theColorSpace the new color space value
84      */
setColorSpace(int theColorSpace)85     public void setColorSpace(int theColorSpace) {
86         this.currentColorSpace = theColorSpace;
87         numComponents = calculateNumComponents();
88     }
89 
90     /**
91      * Get the colorspace value
92      *
93      * @return the colorspace value
94      */
getColorSpace()95     public int getColorSpace() {
96         return (this.currentColorSpace);
97     }
98 
99     /**
100      * Get the number of color components for this colorspace
101      *
102      * @return the number of components
103      */
getNumComponents()104     public int getNumComponents() {
105         return numComponents;
106     }
107 
108     /** @return the name of the color space */
getName()109     public String getName() {
110         switch (currentColorSpace) {
111         case DEVICE_CMYK:
112             return "DeviceCMYK";
113         case DEVICE_GRAY:
114             return "DeviceGray";
115         case DEVICE_RGB:
116             return "DeviceRGB";
117         default:
118             throw new IllegalStateException("Unsupported color space in use.");
119         }
120     }
121 
122     /** {@inheritDoc} */
isDeviceColorSpace()123     public boolean isDeviceColorSpace() {
124         return true;
125     }
126 
127     /** {@inheritDoc} */
isRGBColorSpace()128     public boolean isRGBColorSpace() {
129         return getColorSpace() == DEVICE_RGB;
130     }
131 
132     /** {@inheritDoc} */
isCMYKColorSpace()133     public boolean isCMYKColorSpace() {
134         return getColorSpace() == DEVICE_CMYK;
135     }
136 
137     /** {@inheritDoc} */
isGrayColorSpace()138     public boolean isGrayColorSpace() {
139         return getColorSpace() == DEVICE_GRAY;
140     }
141 
142     /**
143      * Returns a suitable {@link PDFDeviceColorSpace} object given a {@link ColorSpace} object.
144      * @param cs ColorSpace instance
145      * @return a PDF-based color space
146      */
toPDFColorSpace(ColorSpace cs)147     public static PDFDeviceColorSpace toPDFColorSpace(ColorSpace cs) {
148         if (cs == null) {
149             return null;
150         }
151 
152         PDFDeviceColorSpace pdfCS = new PDFDeviceColorSpace(0);
153         switch (cs.getType()) {
154             case ColorSpace.TYPE_CMYK:
155                 pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_CMYK);
156             break;
157             case ColorSpace.TYPE_GRAY:
158                 pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_GRAY);
159                 break;
160             default:
161                 pdfCS.setColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
162         }
163         return pdfCS;
164     }
165 
166 }
167