Module: HexaPDF::Content::ColorSpace
- Defined in:
- lib/hexapdf/content/color_space.rb
Overview
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 ‘color_space.map’ 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:
- #initialize(definition)
-
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.
- #family
-
Returns the PDF name of the color space family this color space belongs to.
- #definition
-
Returns the color space definition as array or symbol.
- #default_color
-
Returns the default color for this color space.
- #color(*args)
-
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.
- #prenormalized_color(*args)
-
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.
The class representing a color in the color space needs to respond to the following methods:
- #color_space
-
Returns the associated color space object.
- #components
-
Returns an array of components that uniquely identifies this color within the color space.
See: PDF1.7 s8.6
Defined Under Namespace
Modules: ColorUtils Classes: DeviceCMYK, DeviceGray, DeviceRGB, Universal
Class Method Summary collapse
-
.device_color_from_specification(*spec) ⇒ Object
:call-seq: ColorSpace.device_color_from_specification(gray) => color ColorSpace.device_color_from_specification(r, g, b) => color ColorSpace.device_color_from_specification(c, m, y, k) => color ColorSpace.device_color_from_specification(string) => color ColorSpace.device_color_from_specification(array) => color.
-
.for_components(components) ⇒ Object
Returns the name of the device color space that should be used for creating a color object from the components array.
-
.prenormalized_device_color(components) ⇒ Object
Returns a device color object for the given components array without applying value normalization.
Class Method Details
.device_color_from_specification(*spec) ⇒ Object
:call-seq:
ColorSpace.device_color_from_specification(gray) => color
ColorSpace.device_color_from_specification(r, g, b) => color
ColorSpace.device_color_from_specification(c, m, y, k) => color
ColorSpace.device_color_from_specification(string) => color
ColorSpace.device_color_from_specification(array) => color
Creates 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.
-
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 - see DeviceGray#color, DeviceRGB#color and DeviceCMYK#color.
119 120 121 122 123 |
# File 'lib/hexapdf/content/color_space.rb', line 119 def self.device_color_from_specification(*spec) spec.flatten! spec = spec[0].scan(/../).map!(&:hex) if spec.length == 1 && spec[0].kind_of?(String) GlobalConfiguration.constantize('color_space.map', for_components(spec)).new.color(*spec) end |
.for_components(components) ⇒ Object
Returns the name of the device color space that should be used for creating a color object from the components array.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hexapdf/content/color_space.rb', line 134 def self.for_components(components) case components.length when 1 then :DeviceGray when 3 then :DeviceRGB when 4 then :DeviceCMYK else raise ArgumentError, "Invalid number of color components, 1|3|4 expected, " \ "#{components.length} given" end end |
.prenormalized_device_color(components) ⇒ Object
Returns a device color object for the given components array without applying value normalization.
127 128 129 130 |
# File 'lib/hexapdf/content/color_space.rb', line 127 def self.prenormalized_device_color(components) GlobalConfiguration.constantize('color_space.map', for_components(components)).new. prenormalized_color(*components) end |