module HexaPDF::Content::ColorSpace

This module contains the color space implementations.

General Information

The PDF specification defines several color spaces. Probably the most used ones are the device color spaces DeviceRGB, DeviceCMYK and DeviceGray. However, there are several others. For example, patterns are also implemented via color spaces.

HexaPDF provides implementations for the most common color spaces. Additional ones can easily be added. After implementing one it just has to be registered on the global configuration object under the ‘’ key.

Color space implementations are currently used so that different colors can be distinguished and to provide better error handling.

Color Space Implementations

A color space implementation consists of two classes: one for the color space and one for its colors.

The class for the color space needs to respond to the following methods:


Creates the color space using the given array with the color space definition. The first item in the array is always the color space family, the other items are color space specific.


Returns the PDF name of the color space family this color space belongs to.


Returns the color space definition as array or symbol.


Returns the default color for this color space.


Returns the color corresponding to the given arguments which may be normalized to conform to the PDF spec. The number and types of the arguments differ from one color space to another.


Returns the color corresponding to the given arguments without applying value normalization. The number and types of the arguments differ from one color space to another.

This method should be used when the arguments are already normalized (e.g. when loaded from a content stream).

The class representing a color in the color space needs to respond to the following methods:


Returns the associated color space object.


Returns an array of components that uniquely identifies this color within the color space.

See: PDF2.0 s8.6



Mapping of color names (CSS Color Module Level 3 names - see - and HexaPDF design color names) to RGB and gray values.

Visual listing of all colors:

canvas.font("Helvetica", size: 7.5)
map = HexaPDF::Content::ColorSpace::COLOR_NAMES
map.each_slice(43).each_with_index do |slice, col|
  x = 10 + col * 100
  slice.each_with_index do |(name, rgb), row|
    canvas.fill_color(rgb).rectangle(x, 380 - row * 9, 9, 9).fill
    canvas.fill_color("black").text(name, at: [x + 15, 380 - row * 9 + 2])

Public Class Methods

device_color_from_specification(gray) → color
device_color_from_specification(r, g, b) → color
device_color_from_specification(c, m, y, k) → color
device_color_from_specification(string) → color
device_color_from_specification(array) → color

Creates and returns a device color object from the given color specification.

There are several ways to define the color that should be used:

  • A single numeric argument specifies a gray color (see DeviceGray::Color).

  • Three numeric arguments specify an RGB color (see DeviceRGB::Color).

  • A string in the format “RRGGBB” where “RR” is the hexadecimal number for the red, “GG” for the green and “BB” for the blue color value also specifies an RGB color.

  • As does a string in the format “RGB” where “RR”, “GG” and “BB” would be used as the hexadecimal numbers for the red, green and blue color values of an RGB color.

  • Any other string is treated as a color name (CSS Color Module Level 3 and HexaPDF design color names are supported - see COLOR_NAMES).

  • Four numeric arguments specify a CMYK color (see DeviceCMYK::Color).

  • An array is treated as if its items were specified separately as arguments.

Note that it makes a difference whether integer or float values are used because the given values are first normalized (expected range by the PDF specification is 0.0 - 1.0) - see DeviceGray#color, DeviceRGB#color and DeviceCMYK#color for details.


cs = HexaPDF::Content::ColorSpace

# Note that Canvas#stroke_color implicitly uses this method, so
# explicitly using it like in this example is not needed
canvas.line(10, 10, 10, 190).stroke
canvas.stroke_color(cs.device_color_from_specification(0, 128, 255))
canvas.line(35, 10, 35, 190).stroke
canvas.line(60, 10, 60, 190).stroke
canvas.line(85, 10, 85, 190).stroke
canvas.line(110, 10, 110, 190).stroke
canvas.line(135, 10, 135, 190).stroke
canvas.stroke_color(cs.device_color_from_specification(10, 50, 0, 60))
canvas.line(160, 10, 160, 190).stroke
canvas.stroke_color(cs.device_color_from_specification([0, 128, 255]))
canvas.line(185, 10, 185, 190).stroke


Returns the name of the device color space that should be used for creating a color object from the components array.


Returns a device color object for the given components array without applying value normalization.

serialize_device_color(color, type: :fill)

Serializes the given device color into the form expected by PDF content streams.

The type argument can either be :stroke to serialize as stroke color operator or :fill as fill color operator.