Module: Prawn::Graphics::Color

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

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object

Provides the following shortcuts:

stroke_some_method(*args) #=> some_method(*args); stroke
fill_some_method(*args) #=> some_method(*args); fill
fill_and_stroke_some_method(*args) #=> some_method(*args); fill_and_stroke

59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/prawn/graphics/color.rb', line 59

def method_missing(id,*args,&block)
  case(id.to_s) 
  when /^fill_and_stroke_(.*)/
    send($1,*args,&block); fill_and_stroke
  when /^stroke_(.*)/
    send($1,*args,&block); stroke 
  when /^fill_(.*)/
    send($1,*args,&block); fill
  else
    super
  end
end

Class Method Details

.hex2rgb(hex) ⇒ Object

Converts hex string into RGB value array:

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

89
90
91
92
# File 'lib/prawn/graphics/color.rb', line 89

def hex2rgb(hex)
  r,g,b = hex[0..1], hex[2..3], 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"

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

def rgb2hex(rgb)
  rgb.map { |e| "%02x" % e }.join
end

Instance Method Details

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

Sets the 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

25
26
27
28
29
# File 'lib/prawn/graphics/color.rb', line 25

def fill_color(*color)  
  return @fill_color if color.empty? 
  @fill_color = process_color(*color) 
  set_fill_color
end

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

Sets the line stroking color. 6 digit HTML color codes are used.

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

45
46
47
48
49
# File 'lib/prawn/graphics/color.rb', line 45

def stroke_color(*color)   
  return @stroke_color if color.empty?
  @stroke_color = process_color(*color)
  set_stroke_color
end