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 @fill_color if color.empty?
29: @fill_color = process_color(*color)
30: set_fill_color
31: end
Converts hex string into RGB value array:
>> Prawn::Graphics::Color.hex2rgb("ff7808")
=> [255, 120, 8]
# File lib/prawn/graphics/color.rb, line 93
93: def hex2rgb(hex)
94: r,g,b = hex[0..1], hex[2..3], hex[4..5]
95: [r,g,b].map { |e| e.to_i(16) }
96: end
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
# File lib/prawn/graphics/color.rb, line 63
63: def method_missing(id,*args,&block)
64: case(id.to_s)
65: when /^fill_and_stroke_(.*)/
66: send($1,*args,&block); fill_and_stroke
67: when /^stroke_(.*)/
68: send($1,*args,&block); stroke
69: when /^fill_(.*)/
70: send($1,*args,&block); fill
71: else
72: super
73: end
74: end
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 84
84: def rgb2hex(rgb)
85: rgb.map { |e| "%02x" % e }.join
86: end
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 @stroke_color if color.empty?
51: @stroke_color = process_color(*color)
52: set_stroke_color
53: end
# File lib/prawn/graphics/color.rb, line 135
135: def color_space(color)
136: case color_type(color)
137: when :RGB
138: :DeviceRGB
139: when :CMYK
140: :DeviceCMYK
141: end
142: end
# File lib/prawn/graphics/color.rb, line 131
131: def color_to_s(color)
132: normalize_color(color).map { |c| '%.3f' % c }.join(' ')
133: end
# File lib/prawn/graphics/color.rb, line 111
111: def color_type(color)
112: case color
113: when String
114: :RGB
115: when Array
116: :CMYK
117: end
118: end
# File lib/prawn/graphics/color.rb, line 120
120: def normalize_color(color)
121: case color_type(color)
122: when :RGB
123: r,g,b = hex2rgb(color)
124: [r / 255.0, g / 255.0, b / 255.0]
125: when :CMYK
126: c,m,y,k = *color
127: [c / 100.0, m / 100.0, y / 100.0, k / 100.0]
128: end
129: end
# File lib/prawn/graphics/color.rb, line 100
100: def process_color(*color)
101: case(color.size)
102: when 1
103: color[0]
104: when 4
105: color
106: else
107: raise ArgumentError, 'wrong number of arguments supplied'
108: end
109: end
# File lib/prawn/graphics/color.rb, line 167
167: def set_color(type, color, options = {})
168: operator = case type
169: when :fill
170: 'scn'
171: when :stroke
172: 'SCN'
173: else
174: raise ArgumentError, "unknown type '#{type}'"
175: end
176:
177: if options[:pattern]
178: set_color_space type, :Pattern
179: add_content "/#{color} #{operator}"
180: else
181: set_color_space type, color_space(color)
182: color = color_to_s(color)
183: add_content "#{color} #{operator}"
184: end
185: end
# File lib/prawn/graphics/color.rb, line 146
146: def set_color_space(type, color_space)
147: # don't set the same color space again
148: return if @color_space[type] == color_space
149: @color_space[type] = color_space
150:
151: unless COLOR_SPACES.include?(color_space)
152: raise ArgumentError, "unknown color space: '#{color_space}'"
153: end
154:
155: operator = case type
156: when :fill
157: 'cs'
158: when :stroke
159: 'CS'
160: else
161: raise ArgumentError, "unknown type '#{type}'"
162: end
163:
164: add_content "/#{color_space} #{operator}"
165: end
# File lib/prawn/graphics/color.rb, line 187
187: def set_fill_color
188: set_color :fill, @fill_color
189: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.