From 4a4facce347cb1402eba1281a5ce1cd800a4a149 Mon Sep 17 00:00:00 2001 From: Gregory Brown Date: Sat, 3 May 2008 12:21:10 -0400 Subject: [PATCH] Hadean prawn is ready. --- lib/prawn/document/graphics.rb | 20 +++++++++++++++++--- spec/graphics_spec.rb | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/prawn/document/graphics.rb b/lib/prawn/document/graphics.rb index 3b444a234..249e05bb4 100644 --- a/lib/prawn/document/graphics.rb +++ b/lib/prawn/document/graphics.rb @@ -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_(.*)/ @@ -183,4 +197,4 @@ def method_missing(id,*args,&block) end end -end \ No newline at end of file +end diff --git a/spec/graphics_spec.rb b/spec/graphics_spec.rb index 9c61d87c7..b904c33a2 100644 --- a/spec/graphics_spec.rb +++ b/spec/graphics_spec.rb @@ -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 } @@ -181,4 +213,4 @@ def append_curved_segment(*params) lambda { @pdf.i_have_a_pretty_girlfriend_named_jia }. should raise_error(NoMethodError) end -end \ No newline at end of file +end