Skip to content

Commit

Permalink
RDoc fixes and spec additions to Canvas#initialize, now also raises A…
Browse files Browse the repository at this point in the history
…rgumentError exceptions when the initial value is not understood.
  • Loading branch information
wvanbergen committed Mar 12, 2011
1 parent ada00ea commit 915ed05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
21 changes: 13 additions & 8 deletions lib/chunky_png/canvas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,25 @@ class Canvas
# CONSTRUCTORS
#################################################################

# Initializes a new Canvas instance
# @param [Integer] width The width in pixels of this canvas
# @param [Integer] height The height in pixels of this canvas
# @param [ChunkyPNG::Pixel, Array<ChunkyPNG::Color>] initial The initial value of the pixels:
# Initializes a new Canvas instance.
#
# * If a color is passed to this parameter, this color will be used as background color.
# @overload initialize(width, height, background_color)
# @param [Integer] width The width in pixels of this canvas
# @param [Integer] height The height in pixels of this canvas
# @param [Integer, ...] background_color The initial background color of this canvas.
# This can be a color value or any value that {ChunkyPNG::Color.parse} can handle.
#
# * If an array of pixels is provided, these pixels will be used as initial value. Note
# that the amount of pixels in this array should equal +width * height+.
# @overload initialize(width, height, initial)
# @param [Integer] width The width in pixels of this canvas
# @param [Integer] height The height in pixels of this canvas
# @param [Array<Integer>] initial The initial pizel values. Must be an array with
# <tt>width * height</tt> elements.
def initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT)

@width, @height = width, height

if initial.kind_of?(Array) && initial.length == width * height
if initial.kind_of?(Array)
raise ArgumentError, "The initial array should have #{width}x#{height} = #{width*height} elements!" unless initial.length == width * height
@pixels = initial
else
@pixels = Array.new(width * height, ChunkyPNG::Color.parse(initial))
Expand Down
2 changes: 1 addition & 1 deletion lib/chunky_png/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def html_color(color_name, opacity = nil)
base_color_name = $1.gsub(/[^a-z]+/i, '').downcase.to_sym
return PREDEFINED_COLORS[base_color_name] | opacity if PREDEFINED_COLORS.has_key?(base_color_name)
end
raise ChunkyPNG::Exception, "Unknown color name #{color_name}!"
raise ArgumentError, "Unknown color name #{color_name}!"
end

# @return [Integer] Black pixel/color
Expand Down
29 changes: 29 additions & 0 deletions spec/chunky_png/canvas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@
it { should respond_to(:height) }
it { should respond_to(:pixels) }

describe '#initialize' do
it "should accept a single color value as background color" do
canvas = ChunkyPNG::Canvas.new(2, 2, 'red @ 0.8')
canvas[1, 0].should == ChunkyPNG::Color.parse('red @ 0.8')
end

it "should raise an error if the color value is not understood" do
lambda { ChunkyPNG::Canvas.new(2, 2, :nonsense) }.should raise_error(ArgumentError)
end

it "should accept an array as initial pixel values" do
canvas = ChunkyPNG::Canvas.new(2, 2, [1,2,3,4])
canvas[0, 0].should == 1
canvas[1, 0].should == 2
canvas[0, 1].should == 3
canvas[1, 1].should == 4
end

it "should raise an ArgumentError if the initial array does not have the correct number of elements" do
lambda { ChunkyPNG::Canvas.new(2, 2, [1,2,3]) }.should raise_error(ArgumentError)
lambda { ChunkyPNG::Canvas.new(2, 2, [1,2,3,4,5]) }.should raise_error(ArgumentError)
end

it "should use a transparent background by default" do
canvas = ChunkyPNG::Canvas.new(1, 1)
canvas[0,0].should == ChunkyPNG::Color::TRANSPARENT
end
end

describe '#dimension' do
it "should return the dimensions as a Dimension instance" do
subject.dimension.should == ChunkyPNG::Dimension('1x1')
Expand Down

0 comments on commit 915ed05

Please sign in to comment.