Colors and Color Spaces Using Colors in SetaPDF

Table of Contents

  1. Introduction
    1. Colors
      1. Color Spaces

        Introduction

        Colors could be defined and used in various ways in a PDF file. The SetaPDF-Core component offers several classes to handle a wide range of available color spaces and values. 

        Colors

        Color values are represented by color classes. These instances are mostly used internal by a component to use such color for drawing in or writing text to a canvas object.

        If a components class offers a method to define a color, it requires a color instance or it will forward the argument to a static method which will create a component by the given color components: 

        Description

        Create an instance by a PDF array object, PHP array or a hexadecimal string of an RGB value.

        Parameters
        $components : int|float|string|array|SetaPDF_Core_Type_Array
         
        Exceptions

        Throws InvalidArgumentException

        For sure it is possible to initiate a color object manually by passing the components to its constructor. Common color values could be created with the SetaPDF_Core_DataStructure_Color_Gray, SetaPDF_Core_DataStructure_Color_Rgb, SetaPDF_Core_DataStructure_Color_Cmyk classes:

        PHP
        $red = new \SetaPDF_Core_DataStructure_Color_Rgb(1, 0, 0);
        // the components could also be passed as array
        $green = new \SetaPDF_Core_DataStructure_Color_Rgb(array(0, 1, 0));
        $blue = new \SetaPDF_Core_DataStructure_Color_Rgb(0, 0, 1);
        
        $gray = new \SetaPDF_Core_DataStructure_Color_Gray(.3);
        
        $orange = new \SetaPDF_Core_DataStructure_Color_Cmyk(0, .35, 1, 0);
        $violet = new \SetaPDF_Core_DataStructure_Color_Cmyk(array(.07, .39, 0, .4));
        
        $limegreen = \SetaPDF_Core_DataStructure_Color::createByComponents('#32CD32');

        Color Spaces

        The SetaPDF-Core component offers classes for defining color spaces. The color classes mentioned earlier will define their own color space when they were invoked by an operator in a content stream.

        In the PDF format it is also possible to declare color components for a currently activated color space. This is needed for example for a Separation color space (see PDF 32000-1:2008 - 8.6.6.4 Separation Colour Spaces):

        PHP
        <?php
        require_once('library/SetaPDF/Autoload.php');
        
        // get a document instance
        $writer = new \SetaPDF_Core_Writer_Http('separation.pdf', true);
        $document = new \SetaPDF_Core_Document($writer);
        
        // create a blank page
        $page = $document->getCatalog()->getPages()->create(\SetaPDF_Core_PageFormats::A4);
        
        // get the canvas object
        $canvas = $page->getCanvas();
        
        // define some spot colors
        $yellow = \SetaPDF_Core_ColorSpace_Separation::createSpotColor($document, 'PANTONE 130', 0, 22/100, 90/100, 0);
        $blue = \SetaPDF_Core_ColorSpace_Separation::createSpotColor($document, 'PANTONE 3005', 100/100, 20/100, 0, 0);
        $orange = \SetaPDF_Core_ColorSpace_Separation::createSpotColor($document, 'PANTONE 144', 0, 50/100, 100/100, 0);
        
        // Set the non stroking color space and tint value
        $canvas->setNonStrokingColorSpace($yellow)
               ->setNonStrokingColor(new \SetaPDF_Core_DataStructure_Color_Special(1));
        
        // Set the stroking color space and tint value
        $canvas->setStrokingColorSpace($orange)
               ->setStrokingColor(new \SetaPDF_Core_DataStructure_Color_Special(1));
        
        // let's draw a rect
        $canvas->path()->setLineWidth(5);
        $canvas->draw()->rect(20, 500, 200, 200, \SetaPDF_Core_Canvas_Draw::STYLE_DRAW_AND_FILL);
        
        // set the non stroking color space and tint value
        $canvas->setNonStrokingColorSpace($blue)
               ->setNonStrokingColor(new \SetaPDF_Core_DataStructure_Color_Special(1));
        
        // change the tint value of the stroking color space
        $canvas->setStrokingColor(new \SetaPDF_Core_DataStructure_Color_Special(.5));
        
        // draw a circle
        $canvas->draw()->circle(190, 540, 60, \SetaPDF_Core_Canvas_Draw::STYLE_DRAW_AND_FILL);
        
        $document->save()->finish();