lib/chunky_png/canvas.rb in chunky_png-1.0.0 vs lib/chunky_png/canvas.rb in chunky_png-1.0.1

- old
+ new

@@ -72,11 +72,11 @@ @width, @height = width, height if initial.kind_of?(Array) && initial.length == width * height @pixels = initial else - @pixels = Array.new(width * height, ChunkyPNG::Color(initial)) + @pixels = Array.new(width * height, ChunkyPNG::Color.parse(initial)) end end # Initializes a new Canvas instances when being cloned. # @param [ChunkyPNG::Canvas] other The canvas to duplicate @@ -126,14 +126,13 @@ # @param [Integer] color The new color for the provided coordinates. # @return [Integer] The new color value for this pixel, i.e. <tt>color</tt>. # # @raise [ChunkyPNG::OutOfBounds] when the coordinates are outside of the image's dimensions. # @see #set_pixel - def []=(*args) - point = args.length == 2 ? ChunkyPNG::Point(args.first) : ChunkyPNG::Point(args[0], args[1]) - assert_xy!(point.x, point.y) - @pixels[point.y * width + point.x] = args.last + def []=(x, y, color) + assert_xy!(x, y) + @pixels[y * width + x] = ChunkyPNG::Color.parse(color) end # Replaces a single pixel in this canvas, without bounds checking. # # This method return value and effects are undefined for coordinates @@ -173,14 +172,13 @@ # @param [Integer] y The y-coordinate of the pixel (row) # @return [Integer] The current color value at the provided coordinates. # # @raise [ChunkyPNG::OutOfBounds] when the coordinates are outside of the image's dimensions. # @see #get_pixel - def [](*args) - point = ChunkyPNG::Point(*args) - assert_xy!(point.x, point.y) - @pixels[point.y * width + point.x] + def [](x, y) + assert_xy!(x, y) + @pixels[y * width + x] 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 #[]) @@ -233,10 +231,17 @@ def include_point?(*point_like) dimension.include?(ChunkyPNG::Point(*point_like)) end alias_method :include?, :include_point? - alias_method :include_xy?, :include_point? + + # Checks whether the given x- and y-coordinate are in the range of the canvas + # @param [Integer] x The x-coordinate of the pixel (column) + # @param [Integer] y The y-coordinate of the pixel (row) + # @return [true, false] True if the x- and y-coordinate is in the range of this canvas. + def include_xy?(x, y) + y >= 0 && y < height && x >= 0 && x < width + end # Checks whether the given y-coordinate is in the range of the canvas # @param [Integer] y The y-coordinate of the pixel (row) # @return [true, false] True if the y-coordinate is in the range of this canvas. def include_y?(y)