Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Hadean prawn is ready.
  • Loading branch information
Gregory Brown committed May 3, 2008
1 parent 78e15be commit 4a4facc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
20 changes: 17 additions & 3 deletions lib/prawn/document/graphics.rb
Expand Up @@ -152,24 +152,38 @@ def rectangle(point,width,height)
add_content("%.3f %.3f %.3f %.3f re" % [ x, y, width, height ])
end

# Sets the fill color. 6 digit HTML color codes are used.
#
# pdf.fill_color "f0ffc1"
#
def fill_color(color)
r,g,b = [color[0..1], color[2..3], color[4..5]].map { |e| e.to_i(16) }
add_content "%.3f %.3f %.3f rg" % [r / 255.0, g / 255.0, b / 255.0]
end

# Sets the line stroking color. 6 digit HTML color codes are used.
#
# pdf.stroke_color "cc2fde"
#
def stroke_color(color)
r,g,b = [color[0..1], color[2..3], color[4..5]].map { |e| e.to_i(16) }
add_content "%.3f %.3f %.3f RG" % [r / 255.0, g / 255.0, b / 255.0]
end

def stroke #:nodoc:
# Strokes and closes the current path.
def stroke
add_content "S"
end

def fill #:nodoc:
# Fills, strokes, and closes the current path.
def fill
add_content "b"
end

# Provides the following shortcuts:
#
# stroke_some_method(*args) #=> some_method(*args); stroke
# fill_some_method(*args) #=> some_method(*args); fill
def method_missing(id,*args,&block)
case(id.to_s)
when /^stroke_(.*)/
Expand All @@ -183,4 +197,4 @@ def method_missing(id,*args,&block)

end
end
end
end
34 changes: 33 additions & 1 deletion spec/graphics_spec.rb
Expand Up @@ -160,6 +160,38 @@ def append_curved_segment(*params)
end
end

class ColorObserver
attr_reader :stroke_color, :fill_color

def set_rgb_color_for_stroking(*params)
@stroke_color = params
end

def set_rgb_color_for_nonstroking(*params)
@fill_color = params
end
end

describe "When setting colors" do

before(:each) { create_pdf }

it "should set stroke colors" do
@pdf.stroke_color "ffcccc"
colors = observer(ColorObserver)
# 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 = observer(ColorObserver)
# 80% red, 100% green, 0% blue
colors.fill_color.should == [0.8,1.0,0]
end

end

describe "When using painting shortcuts" do
before(:each) { create_pdf }

Expand All @@ -181,4 +213,4 @@ def append_curved_segment(*params)
lambda { @pdf.i_have_a_pretty_girlfriend_named_jia }.
should raise_error(NoMethodError)
end
end
end

0 comments on commit 4a4facc

Please sign in to comment.