lib/chunky_png/canvas.rb in chunky_png-1.0.0.rc1 vs lib/chunky_png/canvas.rb in chunky_png-1.0.0.rc2
- old
+ new
@@ -69,21 +69,21 @@
# that the amount of pixels in this array should equal +width * height+.
def initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT)
@width, @height = width, height
- if initial.kind_of?(Integer)
- @pixels = Array.new(width * height, initial)
- elsif initial.kind_of?(Array) && initial.length == width * height
+ if initial.kind_of?(Array) && initial.length == width * height
@pixels = initial
else
- raise ChunkyPNG::ExpectationFailed, "Cannot use this value as initial #{width}x#{height} canvas: #{initial.inspect}!"
+ @pixels = Array.new(width * height, ChunkyPNG::Color(initial))
end
end
# Initializes a new Canvas instances when being cloned.
# @param [ChunkyPNG::Canvas] other The canvas to duplicate
+ # @return [void]
+ # @private
def initialize_copy(other)
@width, @height = other.width, other.height
@pixels = other.pixels.dup
end
@@ -182,11 +182,11 @@
end
# Returns a single pixel from this canvas, without checking bounds. The return value for
# this method is undefined if the coordinates are out of bounds.
# @param (see #[])
- # @return [ChunkyPNG::Color] The current pixel at the provided coordinates.
+ # @return [Integer] The current pixel at the provided coordinates.
def get_pixel(x, y)
@pixels[y * width + x]
end
# Returns an extracted row as vector of pixels
@@ -206,18 +206,20 @@
end
# Replaces a row of pixels on this canvas.
# @param [Integer] y The 0-based row index.
# @param [Array<Integer>] vector The vector of pixels to replace the row with.
+ # @return [void]
def replace_row!(y, vector)
assert_y!(y) && assert_width!(vector.length)
pixels[y * width, width] = vector
end
# Replaces a column of pixels on this canvas.
# @param [Integer] x The 0-based column index.
# @param [Array<Integer>] vector The vector of pixels to replace the column with.
+ # @return [void]
def replace_column!(x, vector)
assert_x!(x) && assert_height!(vector.length)
for y in 0...height do
set_pixel(x, y, vector[y])
end
@@ -225,11 +227,11 @@
# Checks whether the given coordinates are in the range of the canvas
# @param [ChunkyPNG::Point, Array, Hash, String] point_like The point to check.
# @return [true, false] True if the x and y coordinates of the point are
# within the limits of this canvas.
- # @see ChunkyPNG::Point.single
+ # @see ChunkyPNG.Point
def include_point?(*point_like)
dimension.include?(ChunkyPNG::Point(*point_like))
end
alias_method :include?, :include_point?
@@ -277,10 +279,11 @@
ChunkyPNG::Image.from_canvas(self)
end
# Alternative implementation of the inspect method.
# @return [String] A nicely formatted string representation of this canvas.
+ # @private
def inspect
inspected = "<#{self.class.name} #{width}x#{height} ["
for y in 0...height
inspected << "\n\t[" << row(y).map { |p| ChunkyPNG::Color.to_hex(p) }.join(' ') << ']'
end
@@ -289,10 +292,10 @@
protected
# Replaces the image, given a new width, new height, and a new pixel array.
def replace_canvas!(new_width, new_height, new_pixels)
- raise ChunkyPNG::ExpectationFailed, "The provided pixel array should have #{new_width * new_height} items" unless new_pixels.length == new_width * new_height
+ raise ArgumentError, "The provided pixel array should have #{new_width * new_height} items" unless new_pixels.length == new_width * new_height
@width, @height, @pixels = new_width, new_height, new_pixels
return self
end
# Throws an exception if the x-coordinate is out of bounds.