Colors and Color Spaces Using Colors in SetaPDF

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|\setasign\SetaPDF2\Core\Type\PdfArray
 
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 \setasign\SetaPDF2\Core\DataStructure\Color\Gray, \setasign\SetaPDF2\Core\DataStructure\Color\Rgb, \setasign\SetaPDF2\Core\DataStructure\Color\Cmyk classes:

PHP
<?php

use \setasign\SetaPDF2\Core\DataStructure\Color;

$red = new Color\Rgb(1, 0, 0);
// the components could also be passed as array
$green = new Color\Rgb([0, 1, 0]);
$blue = new Color\Rgb([0, 0, 1]);

$gray = new Color\Gray(.3);

$orange = new Color\Cmyk(0, .35, 1, 0);
$violet = new Color\Cmyk([.07, .39, 0, .4]);

$limegreen = Color\AbstractColor::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

use setasign\SetaPDF2\Core\Canvas\Draw;
use setasign\SetaPDF2\Core\ColorSpace\Separation;
use setasign\SetaPDF2\Core\Document;
use setasign\SetaPDF2\Core\PageFormats;
use setasign\SetaPDF2\Core\Writer\HttpWriter;
use setasign\SetaPDF2\Core\DataStructure\Color;

require_once('library/SetaPDF/Autoload.php');

// get a document instance
$writer = new HttpWriter('separation.pdf', true);
$document = new Document($writer);

// create a blank page
$page = $document->getCatalog()->getPages()->create(PageFormats::A4);

// get the canvas object
$canvas = $page->getCanvas();

// define some spot colors
$yellow = Separation::createSpotColor($document, 'PANTONE 130', 0, 22/100, 90/100, 0);
$blue = Separation::createSpotColor($document, 'PANTONE 3005', 100/100, 20/100, 0, 0);
$orange = 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 Color\Special(1));

// Set the stroking color space and tint value
$canvas->setStrokingColorSpace($orange)
       ->setStrokingColor(new Color\Special(1));

// let's draw a rect
$canvas->path()->setLineWidth(5);
$canvas->draw()->rect(20, 500, 200, 200, Draw::STYLE_DRAW_AND_FILL);

// set the non stroking color space and tint value
$canvas->setNonStrokingColorSpace($blue)
       ->setNonStrokingColor(new Color\Special(1));

// change the tint value of the stroking color space
$canvas->setStrokingColor(new Color\Special(.5));

// draw a circle
$canvas->draw()->circle(190, 540, 60, Draw::STYLE_DRAW_AND_FILL);

$document->save()->finish();