Module: Prawn::Graphics::Color

Included in:
Prawn::Graphics
Defined in:
lib/prawn/graphics/color.rb

Stable API collapse

Class Method Details

.hex2rgb(hex) ⇒ Object

Converts hex string into RGB value array:

>> Prawn::Graphics::Color.hex2rgb("ff7808")
=> [255, 120, 8]


78
79
80
81
82
83
# File 'lib/prawn/graphics/color.rb', line 78

def hex2rgb(hex)
  r = hex[0..1]
  g = hex[2..3]
  b = hex[4..5]
  [r, g, b].map { |e| e.to_i(16) }
end

.rgb2hex(rgb) ⇒ Object

Converts RGB value array to hex string suitable for use with fill_color and stroke_color

>> Prawn::Graphics::Color.rgb2hex([255,120,8])
=> "ff7808"


69
70
71
# File 'lib/prawn/graphics/color.rb', line 69

def rgb2hex(rgb)
  rgb.map { |e| format '%<value>02x', value: e }.join
end

Instance Method Details

#fill_color(*color) ⇒ Object Also known as: fill_color=

Sets or returns the fill color.

When called with no argument, it returns the current fill color.

If a single argument is provided, it should be a 6 digit HTML color code.

pdf.fill_color "f0ffc1"

If 4 arguments are provided, the color is assumed to be a CMYK value Values range from 0 - 100.

pdf.fill_color 0, 99, 95, 0


28
29
30
31
32
33
# File 'lib/prawn/graphics/color.rb', line 28

def fill_color(*color)
  return current_fill_color if color.empty?

  self.current_fill_color = process_color(*color)
  set_fill_color
end

#stroke_color(*color) ⇒ Object Also known as: stroke_color=

Sets or returns the line stroking color.

When called with no argument, it returns the current stroking color.

If a single argument is provided, it should be a 6 digit HTML color code.

pdf.stroke_color "f0ffc1"

If 4 arguments are provided, the color is assumed to be a CMYK value Values range from 0 - 100.

pdf.stroke_color 0, 99, 95, 0


51
52
53
54
55
56
57
# File 'lib/prawn/graphics/color.rb', line 51

def stroke_color(*color)
  return current_stroke_color if color.empty?

  color = process_color(*color)
  self.current_stroke_color = color
  set_stroke_color(color)
end