lib/chunky_png/canvas.rb in chunky_png-1.0.1 vs lib/chunky_png/canvas.rb in chunky_png-1.1.0
- old
+ new
@@ -8,11 +8,11 @@
require 'chunky_png/canvas/resampling'
require 'chunky_png/canvas/masking'
module ChunkyPNG
- # The ChunkPNG::Canvas class represents a raster image as a matrix of
+ # The ChunkyPNG::Canvas class represents a raster image as a matrix of
# pixels.
#
# This class supports loading a Canvas from a PNG datastream, and creating a
# {ChunkyPNG::Datastream PNG datastream} based on this matrix. ChunkyPNG
# only supports 8-bit color depth, otherwise all of the PNG format's
@@ -56,24 +56,29 @@
#################################################################
# CONSTRUCTORS
#################################################################
- # Initializes a new Canvas instance
- # @param [Integer] width The width in pixels of this canvas
- # @param [Integer] width The height in pixels of this canvas
- # @param [ChunkyPNG::Pixel, Array<ChunkyPNG::Color>] initial The initial value of te 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))
end
end
@@ -98,11 +103,11 @@
#################################################################
# PROPERTIES
#################################################################
# Returns the dimension (width x height) for this canvas.
- # @return [ChunkyPNG::Dimension] A dimension instante with the width and height set for this canvas.
+ # @return [ChunkyPNG::Dimension] A dimension instance with the width and height set for this canvas.
def dimension
ChunkyPNG::Dimension.new(width, height)
end
# Returns the area of this canvas in number of pixels.
@@ -110,24 +115,14 @@
def area
pixels.length
end
# Replaces a single pixel in this canvas.
- #
- # @overload []=(x, y, color)
- # Sets the color value of a pixel given a x- and y-coordinate
- # @param [Integer] x The x-coordinate of the pixel (column)
- # @param [Integer] y The y-coordinate of the pixel (row)
- # @param [Integer] color The new color for the provided coordinates.
- # @return [Integer] The new color value for this pixel, i.e. <tt>color</tt>.
- #
- # @overload []=(point, color)
- # Sets the color value of a pixel given point-like value.
- # @param [ChunkyPNG::Point, ...] point The point on the canvas to replace.
- # @param [Integer] color The new color for the provided coordinates.
- # @return [Integer] The new color value for this pixel, i.e. <tt>color</tt>.
- #
+ # @param [Integer] x The x-coordinate of the pixel (column)
+ # @param [Integer] y The y-coordinate of the pixel (row)
+ # @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 []=(x, y, color)
assert_xy!(x, y)
@pixels[y * width + x] = ChunkyPNG::Color.parse(color)
@@ -138,42 +133,33 @@
# This method return value and effects are undefined for coordinates
# out of bounds of the canvas.
#
# @param [Integer] x The x-coordinate of the pixel (column)
# @param [Integer] y The y-coordinate of the pixel (row)
- # @param [Inteer] pixel The new color for the provided coordinates.
+ # @param [Integer] pixel The new color for the provided coordinates.
# @return [Integer] The new color value for this pixel, i.e. <tt>color</tt>.
def set_pixel(x, y, color)
@pixels[y * width + x] = color
end
# Replaces a single pixel in this canvas, with bounds checking. It will do
# noting if the provided coordinates are out of bounds.
#
# @param [Integer] x The x-coordinate of the pixel (column)
# @param [Integer] y The y-coordinate of the pixel (row)
- # @param [CInteger] pixel The new color value for the provided coordinates.
+ # @param [Integer] pixel The new color value for the provided coordinates.
# @return [Integer] The new color value for this pixel, i.e. <tt>color</tt>, or
# <tt>nil</tt> if the coordinates are out of bounds.
def set_pixel_if_within_bounds(x, y, color)
return unless include_xy?(x, y)
@pixels[y * width + x] = color
end
# Returns a single pixel's color value from this canvas.
- #
- # @overload [](point)
- # Returns the color value given a point-like value.
- # @param [ChunkyPNG::Point, ...] point The coordinates of the pixel as point.
- # @return [Integer] The current color value at the provided coordinates.
- #
- # @overload [](x, y)
- # Returns the color value given a x- and y-coordinate.
- # @param [Integer] x The x-coordinate of the pixel (column)
- # @param [Integer] y The y-coordinate of the pixel (row)
- # @return [Integer] The current color value at the provided coordinates.
- #
+ # @param [Integer] x The x-coordinate of the pixel (column)
+ # @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 [](x, y)
assert_xy!(x, y)
@pixels[y * width + x]
@@ -255,10 +241,10 @@
def include_x?(x)
x >= 0 && x < width
end
# Returns the palette used for this canvas.
- # @return [ChunkyPNG::Palette] A pallete which contains all the colors that are
+ # @return [ChunkyPNG::Palette] A palette which contains all the colors that are
# being used for this image.
def palette
ChunkyPNG::Palette.from_canvas(self)
end