Save This Page
Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.graphics.color » [javadoc | source]
    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   package org.apache.pdfbox.pdmodel.graphics.color;
   18   
   19   import java.awt.color.ColorSpace;
   20   
   21   /**
   22    * This class represents a CMYK color space.
   23    *
   24    * @author <a href="mailto:andreas@lehmi.de">Andreas Lehmkühler</a>
   25    * @version $Revision: 1.0 $
   26    */
   27   public class ColorSpaceCMYK extends ColorSpace
   28   {
   29       /**
   30        * IDfor serialization.
   31        */
   32       private static final long serialVersionUID = -6362864473145799405L;
   33       
   34       /**
   35        * Constructor.
   36        */
   37       public ColorSpaceCMYK()
   38       {
   39           super(ColorSpace.TYPE_CMYK,4);
   40       }
   41   
   42       /**
   43        *  Converts colorvalues from RGB-colorspace to CIEXYZ-colorspace.
   44        *  @param rgbvalue RGB colorvalues to be converted.
   45        *  @return Returns converted colorvalues.
   46        */
   47       private float[] fromRGBtoCIEXYZ(float[] rgbvalue) 
   48       {
   49           ColorSpace colorspaceRGB = ColorSpace.getInstance(CS_sRGB);
   50           return colorspaceRGB.toCIEXYZ(rgbvalue);
   51       }
   52       
   53       /**
   54        *  Converts colorvalues from CIEXYZ-colorspace to RGB-colorspace.
   55        *  @param rgbvalue CIEXYZ colorvalues to be converted.
   56        *  @return Returns converted colorvalues.
   57        */
   58       private float[] fromCIEXYZtoRGB(float[] xyzvalue) 
   59       {
   60           ColorSpace colorspaceXYZ = ColorSpace.getInstance(CS_CIEXYZ);
   61           return colorspaceXYZ.toRGB(xyzvalue);
   62       }
   63   
   64       /**
   65        * {@inheritDoc}
   66        */
   67       public float[] fromCIEXYZ(float[] colorvalue) 
   68       {
   69           if (colorvalue != null && colorvalue.length == 3)
   70           {
   71               // We have to convert from XYV to RGB to CMYK
   72               return fromRGB(fromCIEXYZtoRGB(colorvalue));
   73           }
   74           return null;
   75       }
   76   
   77       /**
   78        * {@inheritDoc}
   79        */
   80       public float[] fromRGB(float[] rgbvalue) 
   81       {
   82           if (rgbvalue != null && rgbvalue.length == 3) 
   83           {
   84               // First of all we have to convert from RGB to CMY
   85               float c = 1 - rgbvalue[0];
   86               float m = 1 - rgbvalue[1];
   87               float y = 1 - rgbvalue[2];
   88               // Now we have to convert from CMY to CMYK
   89               float varK = 1;
   90               float[] cmyk = new float[4];
   91               if ( c < varK )
   92               {
   93                   varK = c;
   94               }
   95               if ( m < varK )
   96               {
   97                   varK = m;
   98               }
   99               if ( y < varK ) 
  100               {
  101                   varK = y;
  102               }
  103               if ( varK == 1 ) 
  104               {
  105                   cmyk[0] = cmyk[1] = cmyk[2] = 0;
  106               }
  107               else 
  108               {
  109                   cmyk[0] = ( c - varK ) / ( 1 - varK );
  110                   cmyk[1] = ( m - varK ) / ( 1 - varK );
  111                   cmyk[2] = ( y - varK ) / ( 1 - varK );
  112               }
  113               cmyk[3] = varK;
  114               return cmyk;
  115           }
  116           return null;
  117       }
  118   
  119       /**
  120        * {@inheritDoc}
  121        */
  122       public float[] toCIEXYZ(float[] colorvalue) 
  123       {
  124           if (colorvalue != null && colorvalue.length == 4)
  125           {
  126               // We have to convert from CMYK to RGB to XYV
  127               return fromRGBtoCIEXYZ(toRGB(colorvalue));
  128           }
  129           return null;
  130       }
  131   
  132       /**
  133        * {@inheritDoc}
  134        */
  135       public float[] toRGB(float[] colorvalue) 
  136       {
  137           if (colorvalue != null && colorvalue.length == 4) 
  138           {
  139               // First of all we have to convert from CMYK to CMY
  140               float c = ( colorvalue[0] * ( 1 - colorvalue[3] ) + colorvalue[3] );
  141               float m = ( colorvalue[1] * ( 1 - colorvalue[3] ) + colorvalue[3] );
  142               float y = ( colorvalue[2] * ( 1 - colorvalue[3] ) + colorvalue[3] );
  143               // Now we have to convert from CMY to RGB
  144               float[] rgbvalues = new float[3];
  145               rgbvalues[0] = 1 - c;
  146               rgbvalues[1] = 1 - m;
  147               rgbvalues[2] = 1 - y;
  148               return rgbvalues;
  149           }
  150           return null;
  151       }
  152   
  153   }

Save This Page
Home » pdfbox-1.1.0-src » org.apache.pdfbox.pdmodel.graphics.color » [javadoc | source]