Class: Pdfrb::Document::Colors

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/colors.rb

Overview

Facade for color space management. Embeds ICC profiles, registers named color spaces in page Resources, and provides lookup.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Colors

Returns a new instance of Colors.



10
11
12
13
# File 'lib/pdfrb/document/colors.rb', line 10

def initialize(document)
  @document = document
  @next_cs_number = 1
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/pdfrb/document/colors.rb', line 8

def document
  @document
end

Instance Method Details

#embed_icc_profile(icc_bytes, alternate: nil) ⇒ Pdfrb::Model::PdfArray

Embed an ICC profile as a stream and return the color space array [/ICCBased ] for use in Resources.

Parameters:

  • icc_bytes (String)

    raw ICC profile bytes.

  • alternate (Symbol, nil) (defaults to: nil)

    fallback color space (:DeviceRGB, :DeviceCMYK, :DeviceGray, :Lab). Derived from the profile header if nil.

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pdfrb/document/colors.rb', line 23

def embed_icc_profile(icc_bytes, alternate: nil)
  profile = Pdfrb::Color::ICCProfile.new(icc_bytes, alternate: alternate)
  stream = document.add(
    { **profile.stream_dictionary_fields,
      Length: profile.raw_data.bytesize },
    type: Pdfrb::Model::Cos::Stream
  )
  stream.stream = profile.raw_data
  Pdfrb::Model::PdfArray.new([
                               :ICCBased,
                               Pdfrb::Model::Reference.new(stream.oid, stream.gen),
                             ])
end

#register(page, color_space) ⇒ Symbol

Register a color space in a page's Resources /ColorSpace dict and return the name (e.g. :CS1) for use in content streams.

Parameters:

Returns:

  • (Symbol)

    the registered name.



43
44
45
46
47
48
49
50
51
# File 'lib/pdfrb/document/colors.rb', line 43

def register(page, color_space)
  resources = ensure_resources(page)
  cs_dict = resources.value[:ColorSpace]
  cs_dict = ensure_color_space_dict(resources, cs_dict)

  name = cs_name
  cs_dict[name] = color_space
  name
end