Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add Ability to Use Color Class
Add the ability to set fill_color, stroke_color, etc. with something
other than a string. This class should have a method color_str that
returns a hex string representing the color.
  • Loading branch information
slabounty committed Oct 25, 2014
commit 2f125aeb25e499ffcdc70a288a11dd31331dfad6
7 changes: 5 additions & 2 deletions lib/prawn/graphics/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def rgb2hex(rgb)
# => [255, 120, 8]
#
def hex2rgb(hex)
hex = hex.respond_to?(:to_str) ? hex.to_str : hex.color_str
r,g,b = hex[0..1], hex[2..3], hex[4..5]
[r,g,b].map { |e| e.to_i(16) }
end
Expand All @@ -93,9 +94,11 @@ def process_color(*color)

def color_type(color)
case color
when String
when -> (color) { color.respond_to?(:to_str) }
:RGB
when Array
when -> (color) { color.respond_to?(:color_str) }
:RGB
when -> (color) { color.respond_to?(:to_ary) }
case color.length
when 3
:RGB
Expand Down
18 changes: 18 additions & 0 deletions spec/graphics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,31 @@
colors.stroke_color.should == [1.0, 0.8, 0.8]
end

it "should accept stroke colors from objects that support color_str" do
color = mock('color')
color.expects(:color_str).returns("ffcccc")
@pdf.stroke_color color #"ffcccc"
colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
# 100% red, 80% green, 80% blue
colors.stroke_color.should == [1.0, 0.8, 0.8]
end

it "should set fill colors" do
@pdf.fill_color "ccff00"
colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
# 80% red, 100% green, 0% blue
colors.fill_color.should == [0.8,1.0,0]
end

it "should accept fill colors from objects that support color_str" do
color = mock('color')
color.expects(:color_str).returns("ccffcc")
@pdf.fill_color color #"ffcccc"
colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
# 100% red, 80% green, 80% blue
colors.fill_color.should == [0.8, 1.0, 0.8]
end

it "should reset the colors on each new page if they have been defined" do
@pdf.fill_color "ccff00"
#colors = PDF::Inspector::Graphics::Color.analyze(@pdf.render)
Expand Down