Skip to main content
Skip table of contents

Hqn200 - Color Replacement

COLOR REPLACEMENT OPERATORS AND PROCSETS
Originally published December 2020

INTRODUCTION

This document describes the Harlequin Host Renderer functionality that is available from v12.0r1 onwards.

In some workflows, colors specified by the end-user may be ascribed particular semantics. For example:

  • A particular combination of C, M, Y, and K may be used as a stand-in for a white ink, where spot colors were not historically available in the user's application, or
  • A particular combination of C, M, Y, and K may be used to specify a non-marking separation, such as a cut line for labels.

The Harlequin RIP supports the replacement of colors during PDL interpretation through two complementary capabilities:

  1. A set of color replacement operators and
  2. Some procedure sets (ProcSets) that use the color replacement operators to implement particular color replacement strategies.

The color replacement operators implement a client-side strategy; they allow a user (usually mediated through a ProcSet) to:

  • Manage a database of color replacements, and
  • Look up replacements for colors, returning a replacement color space and array of color values.

The ProcSets use features of the Harlequin RIP that allow interception of graphic objects during interpretation to capture the color(s) of objects specified by the user and perform replacement of specified colors.

Implementing Color Replacement

Note: The ProcSets described in this document are not included as standard for most OEMs in the Harlequin Host Renderer SDK.

Additional GGS-internal documentation is held in Confluence.

Please contact Global Graphics if you are considering implementing Color Replacement


THE COLOR REPLACEMENT DATABASE

The color replacement operators manage a database of color replacements. Each entry in the color replacement database includes:

  • The color space of the color to be replaced. This must be a color space with identifiable named color channels (one of DeviceGray, DeviceCMYK, DeviceRGB, Separation or DeviceN). The color space may be specified as a name (for DeviceGray, DeviceCMYK, or DeviceRGB), or as a complete PostScript color space array.
  • The color values in that color space that should be replaced, supplied as an array with the same number of entries as the number of colorants in the original color space.
  • The color space of the replacement color. Most color spaces are permitted for use as replacements, apart from Pattern. There are some extra constraints on color spaces used as replacements when color replacements are merged.
  • The color values of the replacement color, supplied as an array with the same number of entries as the number of colorants in the replacement color space.

The same color space can be used as input and replacement; only one replacement is performed on each lookup, so it is possible to swap colors, or have chains of mutually replacing colors.


When matching replacement colors, the database lookup is performed by first looking for an exact colorant name match for the input color space. This is an exact match in both number of colorants, names of colorants, and order of colorants.


Note: Permutations of the colorants will not match this step.


If an exact matching color space is found in the database, the color values to be replaced are examined. Replacement color values are matched to a configurable accuracy.


If no exact colorant name match is found, then the input color is decomposed into CMYK and spot colorants, or to individual channels (spot and process), depending on the overprint settings supplied. If knocking out, all CMYK channels present are collected together, and a replacement match is made using the process just described against the entire CMYK color using DeviceCMYK as the color space; spot channels are checked for replacements individually.

If overprinting, all colorants are checked for replacements individually, with no special treatment for CMYK channels. After finding replacements of the decomposed color, a composite color space is constructed with all the channels from the replacements (or original channels if there was no replacement for a channel or CMYK subset). This color space is returned to the user and used by the replacement color operators to map channels from the original colors.


Note: Decomposition into CMYK and spots is performed regardless of the current process space. The color replacement operators never take the current process space into account.

THE COLOR REPLACEMENT OPERATORS

The color replacement operators are:

  • addcolorreplacement, to register colors for replacement;
  • removecolorreplacement, to remove replacement color registrations;
  • setcolorreplacementaccuracy, to configure the tolerance to which color replacements will be matched.


All of these operators are in internaldict. It may be useful to re-define these operators into a dictionary on the dictionary stack, for easier access, thus:

CODE
[
	/addcolorreplacement /removecolorreplacement /setcolorreplacementaccuracy
] {
	1183615869 internaldict 1 index get def
} forall


The example code in this document assumes that the operators have either been redefined in such a way, or a begin…end pair for internaldict is wrapped around each example.

ADDING AND REMOVING REPLACEMENT COLORS


The database of color replacements is maintained using the PostScript operators addcolorreplacement and removecolorreplacement. Color replacement entries are added using addcolorreplacement, and persist until removed, replaced by a different entry with the same input color space and color values, or the end of the save/restore level in which the color replacement was added. For many use cases, replacement colors can be installed during job configuration, and never explicitly removed. The server loop restore at the end of a job will remove color replacements registered during job configuration. An example of a color replacement is:

CODE
<<
	/ColorSpace [ /Separation /BrightRed /DeviceCMYK { 0 exch dup 0 } ]
	/ColorValue [ 1 ]
	/ReplacementColorSpace /DeviceCMYK
	/ReplacementColorValue [ 1 0 1 0 ]
>> addcolorreplacement


This example differs from using a NamedColor resource to redefine BrightRed because it is only applied to the solid color (color value 1). A NamedColor resource would be applied to all color values of that spot. A color replacement entry may be removed from the database explicitly, if it was present, using removecolorreplacement:

CODE
<<
	/ColorSpace [ /Separation /BrightRed /DeviceCMYK { 0 exch dup 0 } ]
	/ColorValue [ 1 ]
>> removecolorreplacement

An exact match on the color values is performed for replacement, so it is still possible that a lookup with the same color values will still yield a replacement after removing them (if another replacement is registered which matches the color values within the configured tolerance). If the name /All is specified for the ColorValue value instead of an array, then all replacement colors for the ColorSpace are removed. Normally, there is no need to remove color replacements individually, because the color database entries follow save/restore nesting.

COLOR REPLACEMENT ACCURACY

The setcolorreplacementaccuracy operator sets the color replacement accuracy. This operator consumes one value from the stack, either a number in the range 0 to 1, or a dictionary containing color space accuracy pairs. For example:

TEXT
0.00390625 setcolorreplacementaccuracy

or

CODE
<<
	/Default 0.001
	/DeviceRGB 1 256 div
	/DeviceCMYK 0.002
>> setcolorreplacementaccuracy

The dictionary operand type allows accuracies to be set for each color space. The dictionary keys must be valid color spaces that would be valid for color replacement (either a standard name such as /DeviceCMYK, or a color space array), or the name /Default. The /Default entry is optional and if present sets the accuracy for any color space that does not have a specific entry in the dictionary. Supplying a number operand is equivalent to supplying a dictionary operand containing a single entry with the /Default key and the same real value. The dictionary accuracy values must be numbers in the range 0 to 1.


The accuracy value can be understood as how close a color value must be to match a lookup in the color replacement database. If all colorant values for the color are matched to the configured accuracy, then the entire color is matched, and a replacement for it is returned by the database query. If more than one color match is possible then the nearest match is chosen by a least-squares algorithm.


The accuracy settings from setcolorreplacementaccuracy are used for all subsequent color matching, and the settings are subject to save and restore.


The initial accuracy value is 1/1000. Setting an accuracy value of 0 selects the initial accuracy again.


Color values written into PDF or PostScript by some applications is managed at a fairly coarse level of granularity (for example, 256 steps for RGB color channels and only 100 for CMYK). In the case of CMYK the values may not actually be exactly as expected, because at some stage in the calculation a transition through 256 steps was performed. There are also often rounding errors, or the number of decimal places used in the PDF or PostScript is truncated. To match colors as expected it may be necessary to increase the accuracy value as high as 0.002, or about 1/512.


Note: A color space array used as a key in the dictionary passed to setcolorreplacementaccuracy must be exactly the same compound PostScript object as the value of ColorSpace in a subsequent call to addcolorreplacement; it is not enough to use two arrays containing the same values. Thus the following example will not work:


CODE
<<
	/Default 0.0001
	[
		/DeviceN [ /Cyan /Magenta /Yellow /Black /Orange /Green ]
		/DeviceCMYK
		{	% c m y k o g
			6 -1 roll 1 index add dup 1 gt { pop 1 } if 6 1 roll % Cyan
			5 -1 roll 2 index 0.45 mul add
				dup 1 gt { pop 1 } if 5 1 roll % Magenta
			4 -1 roll add dup 1 gt { pop 1 } if exch % Yellow
		}
	] 0.00001
>> setcolorreplacementaccuracy

<<
	/ColorSpace [
		/DeviceN [ /Cyan /Magenta /Yellow /Black /Orange /Green ]
		/DeviceCMYK
		{	% c m y k o g
			6 -1 roll 1 index add dup 1 gt { pop 1 } if 6 1 roll % Cyan
			5 -1 roll 2 index 0.45 mul add
				dup 1 gt { pop 1 } if 5 1 roll % Magenta
			4 -1 roll add dup 1 gt { pop 1 } if exch % Yellow
		}
	]
	/ColorValue [ 0.0 0.0 0.0 0.0 0.999 0.0 ]
	/ReplacementColorSpace /DeviceCMYK
	/ReplacementColorValue [ 0 0.8 0.5 0 ]
>> addcolorreplacement

but the following example will work because exactly the same array object is used in both places:

CODE
/mydevicen [
	/DeviceN [ /Cyan /Magenta /Yellow /Black /Orange /Green ]
	/DeviceCMYK
	{	% c m y k o g
		6 -1 roll 1 index add dup 1 gt { pop 1 } if 6 1 roll % Cyan
		5 -1 roll 2 index 0.45 mul add
			dup 1 gt { pop 1 } if 5 1 roll % Magenta
		4 -1 roll add dup 1 gt { pop 1 } if exch % Yellow
	}
] def

<<
	/Default 0.0001
	/DeviceRGB 1 512 div
	/DeviceCMYK 0.001
	mydevicen 0.0001
>> setcolorreplacementaccuracy

<<
	/ColorSpace mydevicen
	/ColorValue [ 0.0 0.0 0.0 0.0 0.999 0.0 ]
	/ReplacementColorSpace /DeviceCMYK
	/ReplacementColorValue [ 0 0.8 0.5 0 ]
>> addcolorreplacement


COLOR REPLACEMENT PROCSETS

There are some procedure sets for color replacement that may be shipped in an OEM distribution. The ProcSets use different techniques to apply replacements registered in the color replacement database.


If you have contracted with Global Graphics to support color replacement, there may be OEM start-up code and/or Page Features in your distribution that will load and configure these ProcSets appropriately.


HDLTREPLACECOLORS
The HDLTReplaceColors ProcSet uses the licensable Harlequin Display List Technology feature to intercept objects as they are added to the display list, and to perform color lookup and replacement on those objects.


The ProcSet is installed and configured using the SetupReplaceColors procedure thus:


CODE
<<
	/ReplaceFillColors true
	/ReplaceStrokeColors true
	/ReplaceTextColors true
	/ReplaceMaskColors true
	/ReplaceGradientColors true
	/ReplacementAutoNamedColors true
>> /HDLTReplaceColors /ProcSet findresource
/SetupReplaceColors get exec



The dictionary argument to SetupReplaceColors can contain the following keys:


/ReplaceFillColors – If this Boolean option is true (the default), colors used for vector graphic fill objects will be subject to replacement.


/ReplaceStrokeColors – If this Boolean option is true (the default), colors used for vector graphic stroke objects will be subject to replacement.


/ReplaceTextColors – If this Boolean option is true (the default), colors used for text objects will be subject to replacement.


/ReplaceMaskColors – If this Boolean option is true (the default), colors used for image stencil masks will be subject to replacement.


/ReplaceGradientColors – If this Boolean option is true, colors used for gradient stops in Type 2 (linear) and Type 3 (radial) shaded fills will be subject to replacement. The default value for this option is false, unlike many of the other options.


/AllowColorManagement – If this Boolean option is true (the default), then further color management is allowed on the replacement colors for objects. If this option is false, then replacement colors are not subject to color management. If replacement colors use colorants that are supported by the output device, then the exact ink combinations for replacement colors can be specified. Graphic objects that are used in transparency groups or use a different replacement color space to the device color space will still undergo some transformations.


/RecordReplacedColors – This key takes a Boolean value (default false). If true, then colors replaced will be recorded, and can be reported through the ForallReplacedColors procedure in this ProcSet. There are significant limitations when recording replaced colors:

  • Only the color space name is recorded (so all DeviceN or Separation colors will be combined together).
  • If a multiple color replacement is made (e.g., for blend gradient stops), then all of the colors in the replacement will be recorded, even if only one of them was actually replaced.

Note: These limitations make RecordReplacedColors useful only for DeviceGray, DeviceRGB, and DeviceCMYK colors. Recording replaced colors may also take extra time and a little bit of memory, so should be avoided unless necessary.


/DontReplaceColors – This Boolean option controls whether colors will actually be replaced by the ProcSet for the configured graphic object types. This option is expected to be used in conjunction with RecordReplacedColors, for workflows where the OEM wants to capture and report on colors that would be replaced in a job. The default value is false, indicating color replacement will be done.


/InstallShims – This option (default false) installs two utility procedures, AddSpecialCMYK and AddSpecialRGB into the current dictionary at the point that the HDLTReplaceColors SetupReplaceColors procedure is run.


/ReplacementAutoNamedColors – Some color space replacements require generation of NamedColors to represent one or more channels in the replacement color space (see section "Complex replacement color spaces"). The ReplacementAutoNamedColors option configures the RIP to accept these named colors by:

  1. Setting the OverprintPreview color option to /SpotsOnly, if it is not already /SpotsOnly or true;
  2. Adding the /ReplacementAutoNamedColors named color database to the NamedColor lookup order.


The default value of this option is true. This value may be set to false if the OEM wishes to perform both configuration steps above themselves. If these configuration steps are not performed, complex replacement color spaces may generate errors.


Note: The OEM must perform a setpagedevice operation after using this option to configure the HDLTReplaceColors ProcSet.


The HDLTReplaceColors ProcSet contains a procedure to iterate the colors recorded, if the RecordReplacedColors option was true. The procedure ForallReplaceColors is called thus:


CODE
{exch == ( )print ==} /DeviceCMYK
	/HDLTReplaceColors /ProcSet findresource /ForallReplaceColors get exec


ForallReplacedColors consumes a procedure object and a color space name from the stack. The procedure object is called once for every registered replacement color that was used. At the time the procedure is called, the stack contains (top to bottom):

  • An integer indicating how many times the registered color was replaced
  • An array of color values for the color that was replaced.


COMPLEX REPLACEMENT COLOR SPACES

Composite replacement color spaces may be generated when a replacement is decomposed into CMYK and spot colors and replaced as parts:

  • When replacing the color space used for images or other objects where the number of components in object description cannot be change; and
  • When replacing multiple colors together (for example, all the gradient stops of a blend).

When constructing a composite replacement color space, all colorants have to be represented on the virtual page device. Automatic separation addition and named color interception methods are used to make this as seamless as possible.


Replacement color spaces can be any valid color space. If replacements are made using device independent color spaces for individual components of a single color, or if different color spaces are used as replacements for colors in a multiple color replacement, the color replacement operators may generate NamedColors to represent replacement components.


Note: The NamedColor resource /ReplacementAutoNamedColors must be added to the NamedColor lookup order for these colors to be correctly replaced.


In Harlequin RIPs before v12.1r1, the same replacement colorant could not be used by two different parts of the composite replacement color. This constraint was removed in Harlequin v12.1r1.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.