PDFlib Cookbook

cookbook

color/devicen_color

Define and use several DeviceN colors.

Download PHP Code  Switch to Java Code  Show Output 

<?php
/*
 * DeviceN color and shadings with spot colors:
 * - Define DeviceN colors with N=2/3/4 process colors and use them to construct a shading
 * 
 * The required PostScript tint transform functions for DeviceN color based
 * on a subset of the Cyan, Magenta, Yellow and Black process colors are
 * listed in the PDFlib Tutorial.
 * 
 * The PostScript tint transform functions required for DeviceN color spaces
 * are defined below as transformFunc<N> for N=2/3/4/5. They work only with
 * Lab as alternate color space and need the Lab values of the DeviceN
 * components at the start of the code. The PostScript code is written for
 * clarity, not efficiency.
 * 
 * The PostScript functions below are not used by the code, but are only
 * provided here for information.
 * The functions are required only for explicitly constructing a DeviceN
 * color space. PDFlib automatically creates a suitable DeviceN color
 * space for shadings based on spot colors (see spot_color_shading).
 *
 * Required software: PDFlib/PDFlib+PDI/PPS 9.1
 * Required data: none
 */

        /* FOR INFORMATION ONLY: PostScript tint transfer functions for use
         * in a DeviceN color with N=2,3,4,5.
         * These functions create a simple weighted blend of the Lab values of
         * the underlying colors. The Lab values of the colors in the DeviceN
         * color space must be defined at the start of the PostScript code.
         */
$transformFunc2 =
        "% DeviceN transform function for N=2 in CIE L*a*b* color space\n" .
        "% Copyright (c) 2016 PDFlib GmbH\n"				.
        "% Lab color values of input colors must be listed here:\n" 	.
        "80 28 75				% color 1: PANTONE 123 U\n".
        "31.76 0 -17			% color 2: PANTONE 289 U\n" 	.
        "% blend L values\n" 						.
        "7 index 6 index mul	% t1*L1\n" 			.
        "7 index 4 index mul	% t2*L2\n" 			.
        "add\n" 							.
        "9 1 roll				% bottom: L\n" 		.
        "% blend a values\n" 						.
        "7 index 5 index mul	% t1*a1\n" 			.
        "7 index 3 index mul	% t2*a2\n" 			.
        "add\n" 							.
        "9 1 roll				% bottom: a\n" 		.
        "% blend b values\n" 						.
        "7 index 4 index mul	% t1*b1\n" 			.
        "7 index 2 index mul	% t2*b2\n" 			.
        "add\n" 							.
        "9 1 roll				% bottom: b\n" 		.
        "pop pop pop pop pop pop pop pop\n";

$transformFunc3 =
        "% DeviceN transform function for N=3 in CIE L*a*b* color space\n".
        "% Copyright (c) 2016 PDFlib GmbH\n"				.	
        "% Lab color values of input colors must be listed here:\n" 	.
        "80 28 75			% color 1: PANTONE 123 U\n" 	.
        "31.76 0 -17		% color 2: PANTONE 289 U\n" 		.
        "53.73 61 3			% color 3: PANTONE Rubine Red U\n".
        "% blend L values\n" 						.
        "11 index 9 index mul		% t1*L1\n" 		.
        "11 index 7 index mul		% t2*L2\n" 		.
        "11 index 5 index mul		% t3*L3\n" 		.
        "add add\n" 							.
        "13 1 roll					% bottom: L\n" 	.
        "% blend a values\n" 						.
        "11 index 8 index mul		% t1*a1\n" 		.
        "11 index 6 index mul		% t2*a2\n" 		.
        "11 index 4 index mul		% t3*a3\n" 		.
        "add add\n" 						.
        "13 1 roll					% bottom: a\n" 	.
        "% blend b values\n" 						.
        "11 index 7 index mul		% t1*b1\n" 		.
        "11 index 5 index mul		% t2*b2\n" 		.
        "11 index 3 index mul		% t3*b3\n" 		.
        "add add\n" 						.
        "13 1 roll					% bottom: b\n" 	.
        "pop pop pop pop pop pop pop pop pop pop pop pop\n";

$transformFunc4 =
        "% DeviceN transform function for N=4 in CIE L*a*b* color space\n".
        "% Copyright (c) 2016 PDFlib GmbH\n"				.	
        "% Lab color values of input colors must be listed here:\n" 	.
        "80 28 75			% color 1: PANTONE 123 U\n" 	.
        "31.76 0 -17		% color 2: PANTONE 289 U\n" 		.
        "53.73 61 3			% color 3: PANTONE Rubine Red U\n".
        "90.59 4 47			% color 4: PANTONE 1215 U\n" 	.
        "% blend L values\n" 						.
        "15 index 12 index mul		% t1*L1\n" 		.
        "15 index 10 index mul		% t2*L2\n" 		.
        "15 index 8 index mul		% t3*L3\n" 		.
        "15 index 6 index mul		% t4*L4\n" 		.
        "add add add\n" 						.
        "17 1 roll					% bottom: L\n" 	.
        "% blend a values\n" 						.
        "15 index 11 index mul		% t1*a1\n" 		.
        "15 index 9 index mul		% t2*a2\n" 		.
        "15 index 7 index mul		% t3*a3\n" 		.
        "15 index 5 index mul		% t4*a4\n" 		.
        "add add add\n" 						.
        "17 1 roll					% bottom: a\n" 	.
        "% blend b values\n" 						.
        "15 index 10 index mul		% t1*b1\n" 		.
        "15 index 8 index mul		% t2*b2\n" 		.
        "15 index 6 index mul		% t3*b3\n" 		.
        "15 index 4 index mul		% t4*b4\n" 		.
        "add add add\n" 						.
        "17 1 roll					% bottom: b\n" 	.
        "pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop\n";

$transformFunc5 =
        "% DeviceN transform function for N=5 in CIE L*a*b* color space\n".
        "% Copyright (c) 2016 PDFlib GmbH\n"				.
        "% Lab color values of input colors must be listed here:\n" 	.
        "80 28 75			% color 1: PANTONE 123 U\n" 	.
        "31.76 0 -17		% color 2: PANTONE 289 U\n" 		.
        "53.73 61 3			% color 3: PANTONE Rubine Red U\n".
        "90.59 4 47			% color 4: PANTONE 1215 U\n" 	.
        "60 0 -2			% color 5: PANTONE Cool Gray 8 U\n".
        "% blend L values\n" 						.
        "19 index 15 index mul		% t1*L1\n" 		.
        "19 index 13 index mul		% t2*L2\n" 		.
        "19 index 11 index mul		% t3*L3\n" 		.
        "19 index 9 index mul		% t4*L4\n" 		.
        "19 index 7 index mul		% t5*L5\n" 		.
        "add add add add\n" 						.
        "21 1 roll					% bottom: L\n" 	.
        "% blend a values\n" 						.
        "19 index 14 index mul		% t1*a1\n" 		.
        "19 index 12 index mul		% t2*a2\n" 		.
        "19 index 10 index mul		% t3*a3\n" 		.
        "19 index 8 index mul		% t4*a4\n" 		.
        "19 index 6 index mul		% t5*a5\n" 		.
        "add add add add\n" 						.
        "21 1 roll					% bottom: a\n" 	.
        "% blend b values\n" 						.
        "19 index 13 index mul		% t1*b1\n" 		.
        "19 index 11 index mul		% t2*b2\n" 		.
        "19 index 9 index mul		% t3*b3\n" 		.
        "19 index 7 index mul		% t4*b4\n" 		.
        "19 index 5 index mul		% t5*b5\n" 		.
        "add add add add\n" 						.
        "21 1 roll					% bottom: b\n" 	.
        "pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop pop\n";

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$title = "DeviceN color";

$y = 600; $x = 30; $width=700; $height=50; $fontsize=12; $leading=21;
$exitcode = 0;

try {
    $p = new pdflib();

    $p->set_option("searchpath={" . $searchpath . "}");

    /*  This means that errors in load_font() etc. throw an exception  */
    $p->set_option("errorpolicy=exception");

    $p->begin_document("", "");

    $p->set_info("Creator", "PDFlib Cookbook");
    $p->set_info("Title", $title);
    
    /* Load the font */
    $font = $p->load_font("NotoSerif-Bold", "unicode", "");
   
    /* Start the page */
    $p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
    
    $p->setfont($font, $fontsize);

  
    /* *******************************************************
     * Define DeviceN color based on two process colors
     */
    $devicen = $p->create_devicen(
      "names={Magenta Yellow} alternate=devicecmyk transform={{0 0 4 1 roll}}");

    $p->fit_textline("DeviceN-based shading with N=2 (process colors Magenta/Yellow) and stop colors 1/0 and 0/1:",
        $x, $y -= 2*$leading, "fillcolor=black");

    $y -= $height + $fontsize;
    
    /* Create axial shading based on DeviceN color */ 
    $sh = $p->shading("axial", $x, $y, $x+$width, $y, 0, 0, 0, 0,
         "startcolor={devicen " . $devicen . " 1 0} " .
         "  endcolor={devicen " . $devicen . " 0 1}");

    $shp = $p->shading_pattern($sh, "");
    $p->set_graphics_option("fillcolor={pattern " . $shp . "}");

    /* Fill a rectangle with the shading */
    $p->rect($x, $y, $width, $height);
    $p->fill();


    /* *******************************************************
     * Define DeviceN color based on three process colors
     */
    $devicen = $p->create_devicen(
        "names={Cyan Magenta Black} alternate=devicecmyk transform={{0 exch}}");

    $p->fit_textline("DeviceN-based shading with N=3 (process colors Cyan/Magenta/Black) and stop colors 1/0/0, 0/1/0 and 0/0/1:",
                $x, $y -= 2*$leading, "fillcolor=black");

    $y -= $height + $fontsize;
    
    /* Create axial shading based on DeviceN color */ 
    $sh = $p->shading("axial", $x, $y, $x+$width, $y, 0, 0, 0, 0,
         "stopcolors={  0%  {devicen " . $devicen . " 1 0 0} " .
                     " 50%  {devicen " . $devicen . " 0 1 0} " .
                     "100%  {devicen " . $devicen . " 0 0 1} }");

    $shp = $p->shading_pattern($sh, "");
    $p->set_graphics_option("fillcolor={pattern " . $shp . "}");

    /* Fill a rectangle with the shading */
    $p->rect($x, $y, $width, $height);
    $p->fill();


    /* *******************************************************
     * Define DeviceN color based on four process colors;
     * Since all CMYK components are used an empty transfer function
     * can be provided.
     */
    $devicen = $p->create_devicen(
        "names={Cyan Magenta Yellow Black} alternate=devicecmyk transform={{}}");

    $p->fit_textline("DeviceN-based shading with N=4 (process colors Cyan/Magenta/Yellow/Black) and stop colors 1/0/0/0, 0/1/0/0, 0/0/1/0 and 0/0/0/1:",
                $x, $y -= 2*$leading, "fillcolor=black");

    $y -= $height + $fontsize;
    
    /* Create axial shading based on DeviceN color */ 
    $sh = $p->shading("axial", $x, $y, $x+$width, $y, 0, 0, 0, 0,
         "stopcolors={  0%  {devicen " . $devicen . " 1 0 0 0} " .
                     " 33%  {devicen " . $devicen . " 0 1 0 0} " .
                     " 66%  {devicen " . $devicen . " 0 0 1 0} " .
                     "100%  {devicen " . $devicen . " 0 0 0 1} }");

    $shp = $p->shading_pattern($sh, "");
    $p->set_graphics_option("fillcolor={pattern " . $shp . "}");

    /* Fill a rectangle with the shading */
    $p->rect($x, $y, $width, $height);
    $p->fill();

    $p->end_page_ext("");
    

    $p->end_document("");
    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=devicen_color.pdf");
    print $buf;

}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in devicen_color sample:" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname()
            . ": " . $e->get_errmsg());
    exit(1);
}
catch (Throwable $e) {
    echo($e);
    exit(1);
}
$p= 0;