Prawn::Graphics::Color

Constants

COLOR_SPACES

Public Instance Methods

fill_color(*color) click to toggle source

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
    # File lib/prawn/graphics/color.rb, line 27
27:       def fill_color(*color)
28:         return current_fill_color if color.empty?
29:         self.current_fill_color = process_color(*color)
30:         set_fill_color
31:       end
Also aliased as: fill_color=
fill_color=(*color) click to toggle source
Alias for: fill_color
hex2rgb(hex) click to toggle source

Converts hex string into RGB value array:

 >> Prawn::Graphics::Color.hex2rgb("ff7808")
 => [255, 120, 8]
    # File lib/prawn/graphics/color.rb, line 75
75:       def hex2rgb(hex)
76:         r,g,b = hex[0..1], hex[2..3], hex[4..5]
77:         [r,g,b].map { |e| e.to_i(16) }
78:       end
rgb2hex(rgb) click to toggle source

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

  >> Prawn::Graphics::Color.rgb2hex([255,120,8])
  => "ff7808"
    # File lib/prawn/graphics/color.rb, line 66
66:       def rgb2hex(rgb)
67:         rgb.map { |e| "%02x" % e }.join
68:       end
stroke_color(*color) click to toggle source

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
    # File lib/prawn/graphics/color.rb, line 49
49:       def stroke_color(*color)
50:         return current_stroke_color if color.empty?
51:         color = process_color(*color)
52:         self.current_stroke_color = color
53:         set_stroke_color(color)
54:       end
Also aliased as: stroke_color=
stroke_color=(*color) click to toggle source
Alias for: stroke_color

Private Instance Methods

color_space(color) click to toggle source
     # File lib/prawn/graphics/color.rb, line 117
117:       def color_space(color)
118:         case color_type(color)
119:         when :RGB
120:           :DeviceRGB
121:         when :CMYK
122:           :DeviceCMYK
123:         end
124:       end
color_to_s(color) click to toggle source
     # File lib/prawn/graphics/color.rb, line 113
113:       def color_to_s(color)
114:         normalize_color(color).map { |c| '%.3f' % c }.join(' ')
115:       end
color_type(color) click to toggle source
     # File lib/prawn/graphics/color.rb, line 93
 93:       def color_type(color)
 94:         case color
 95:         when String
 96:           :RGB
 97:         when Array
 98:           :CMYK
 99:         end
100:       end
current_color_space(type) click to toggle source
     # File lib/prawn/graphics/color.rb, line 184
184:       def current_color_space(type)
185:         graphic_state.color_space[type]
186:       end
current_fill_color() click to toggle source
     # File lib/prawn/graphics/color.rb, line 193
193:       def current_fill_color
194:         graphic_state.fill_color
195:       end
current_fill_color=(color) click to toggle source
     # File lib/prawn/graphics/color.rb, line 197
197:       def current_fill_color=(color)        
198:         graphic_state.fill_color = color
199:       end
current_stroke_color() click to toggle source
     # File lib/prawn/graphics/color.rb, line 201
201:       def current_stroke_color
202:         graphic_state.stroke_color
203:       end
current_stroke_color=(color) click to toggle source
     # File lib/prawn/graphics/color.rb, line 205
205:       def current_stroke_color=(color)
206:         graphic_state.stroke_color = color
207:       end
normalize_color(color) click to toggle source
     # File lib/prawn/graphics/color.rb, line 102
102:       def normalize_color(color)
103:         case color_type(color)
104:         when :RGB
105:           r,g,b = hex2rgb(color)
106:           [r / 255.0, g / 255.0, b / 255.0]
107:         when :CMYK
108:           c,m,y,k = *color
109:           [c / 100.0, m / 100.0, y / 100.0, k / 100.0]
110:         end
111:       end
process_color(*color) click to toggle source
    # File lib/prawn/graphics/color.rb, line 82
82:       def process_color(*color)
83:         case(color.size)
84:         when 1
85:           color[0]
86:         when 4
87:           color
88:         else
89:           raise ArgumentError, 'wrong number of arguments supplied'
90:         end
91:       end
set_color(type, color, options = {}) click to toggle source
     # File lib/prawn/graphics/color.rb, line 149
149:       def set_color(type, color, options = {})
150:         operator = case type
151:         when :fill
152:           'scn'
153:         when :stroke
154:           'SCN'
155:         else
156:           raise ArgumentError, "unknown type '#{type}'"
157:         end
158: 
159:         if options[:pattern]
160:           set_color_space type, :Pattern
161:           add_content "/#{color} #{operator}"
162:         else          
163:           set_color_space type, color_space(color)
164:           color = color_to_s(color)
165:           write_color(color, operator)
166:         end
167:       end
set_color_space(type, color_space) click to toggle source
     # File lib/prawn/graphics/color.rb, line 128
128:       def set_color_space(type, color_space)
129:         # don't set the same color space again
130:         return if current_color_space(type) == color_space && !state.page.in_stamp_stream?
131:         set_current_color_space(color_space, type)
132: 
133:         unless COLOR_SPACES.include?(color_space)
134:           raise ArgumentError, "unknown color space: '#{color_space}'"
135:         end
136: 
137:         operator = case type
138:         when :fill
139:           'cs'
140:         when :stroke
141:           'CS'
142:         else
143:           raise ArgumentError, "unknown type '#{type}'"
144:         end
145: 
146:         add_content "/#{color_space} #{operator}"
147:       end
set_current_color_space(color_space, type) click to toggle source
     # File lib/prawn/graphics/color.rb, line 188
188:       def set_current_color_space(color_space, type)                
189:         save_graphics_state if graphic_state.nil?
190:         graphic_state.color_space[type] = color_space
191:       end
set_fill_color(color = nil) click to toggle source
     # File lib/prawn/graphics/color.rb, line 169
169:       def set_fill_color(color = nil)
170:         set_color :fill, color || current_fill_color        
171:       end
set_stroke_color(color = nil) click to toggle source
     # File lib/prawn/graphics/color.rb, line 173
173:       def set_stroke_color(color = nil)
174:         set_color :stroke, color || current_stroke_color
175:       end
update_colors() click to toggle source
     # File lib/prawn/graphics/color.rb, line 177
177:       def update_colors
178:         set_fill_color
179:         set_stroke_color
180:       end
write_color(color, operator) click to toggle source
     # File lib/prawn/graphics/color.rb, line 217
217:       def write_color(color, operator)
218:         add_content "#{color} #{operator}"
219:       end
write_fill_color() click to toggle source
     # File lib/prawn/graphics/color.rb, line 209
209:       def write_fill_color
210:         write_color(current_fill_color, 'scn')
211:       end
write_stroke_color() click to toggle source
     # File lib/prawn/graphics/color.rb, line 213
213:       def write_stroke_color
214:         write_color(current_fill_color, 'SCN')
215:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.