Module: Prawn::Graphics::Color

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

Overview

Implements color handling.

Stable API collapse

Class Method Details

.hex2rgb(hex) ⇒ Array(Integer, Integer, Integer)

Converts hex string into RGB value array.

Examples:

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

Parameters:

  • hex (String)

    must be 6-digits long.

Returns:

  • (Array(Integer, Integer, Integer))
Source Code
lib/prawn/graphics/color.rb, line 101
101
def hex2rgb(hex)
102
  r = hex[0..1]
103
  g = hex[2..3]
104
  b = hex[4..5]
105
  [r, g, b].map { |e| e.to_i(16) }
106
end

.rgb2hex(rgb) ⇒ String

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

Examples:

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

Parameters:

  • rgb (Array(Number, Number, Number))

    Each component has to be in the range from 0 to 255.

Returns:

  • (String)
Source Code
lib/prawn/graphics/color.rb, line 89
89
def rgb2hex(rgb)
90
  rgb.map { |e| format('%<value>02x', value: e) }.join
91
end

Instance Method Details

#fill_colorString, Array<Number> #fill_color(color) ⇒ void Also known as: fill_color=

Sets or returns the fill color.

Overloads:

  • #fill_colorString, Array<Number>

    Returns the current fill color.

    Returns:

    • (String, Array<Number>)
  • #fill_color(color) ⇒ void

    This method returns an undefined value.

    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 is 0–100.

    pdf.fill_color 0, 99, 95, 0
    

    Parameters:

    • color (String, Array<Number>)
Source Code
lib/prawn/graphics/color.rb, line 35
35
def fill_color(*color)
36
  return current_fill_color if color.empty?
37
38
  self.current_fill_color = process_color(*color)
39
  set_fill_color
40
end

#stroke_colorString, Array<Number> #stroke_color(color) ⇒ void Also known as: stroke_color=

Sets or returns the line stroking color.

Overloads:

  • #stroke_colorString, Array<Number>

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

    Returns:

    • (String, Array<Number>)
  • #stroke_color(color) ⇒ void

    This method returns an undefined value.

    Sets the stroking color.

    Parameters:

    • color (String, Array<Number>)

      new stroking color:

      • In String form 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 to 100.

        pdf.stroke_color 0, 99, 95, 0
        
Source Code
lib/prawn/graphics/color.rb, line 67
67
def stroke_color(*color)
68
  return current_stroke_color if color.empty?
69
70
  color = process_color(*color)
71
  self.current_stroke_color = color
72
  set_stroke_color(color)
73
end