Table of Contents

Colours in PDF

PDF distinguishes between filling (paint the interior of a closed path) and stroking (paint the outline of a path) when painting. Each mode has its own color and is used with the appropriate paint operators.

For example, a filled rectangle with border can be painted with one operator (/re) using both the filling and stroking color (which can be different).

In order to set a color, first the colorspace and then the actual color has to be set with the right parameters for that colorspace. For example setting the stroking color to red in RGB:

/DeviceRGB CS   % set colorspace for stroking
1 0 0 SC        % set stroking color

For the device colorspaces, there exist short cut operators doing both at once. For the above example one can write:

1 0 0 RG        % set RGB colorspace and color for stroking

There are 11 distinct colorspaces available in PDF:

Device colorspaces

Device dependent, Not reproducable.

CIE-based colorspaces

Calibrated: device independent, reproducable.

Special colorspaces

Most commonly used and easiest are the device colorspaces. Setting black is done like this in the different colorspaces:

% DeviceGray
0 G        % stroking
0 g        % filling
% DeviceRGB
0 0 0 RG   % stroking
0 0 0 rg   % filling
% DeviceCMYK
0 0 0 1 K  % stroking
0 0 0 1 k  % filling

The renderer supports the device color spaces. If you need other color spaces, you need to work directly with the color operators.

renderer strokeColor: <ColorValue or CMYKColor>.
renderer fillColor: <ColorValue or CMYKColor>.

The renderer will insert /DeviceRGB operators for ColorValues and /DeviceCMYK for CMYKColors. If the color is gray, /DevideGray is used.

Example:

page := Page newInBounds: (0 @ 0 corner: 100 @ 100) colorspace: DeviceRGB new render: [:renderer |
  renderer fillColor: ColorValue red.
  renderer addRectangleLeft: 20 bottom: 20 width: 20 height: 40.
  renderer fill.
  renderer strokeColor: ColorValue blue.
  renderer linewidth: 5.
  renderer addRectangleLeft: 20 bottom: 20 width: 20 height: 40.
  renderer stroke].