lib/chunky_png/canvas.rb in chunky_png-1.3.11 vs lib/chunky_png/canvas.rb in chunky_png-1.3.12
- old
+ new
@@ -1,16 +1,16 @@
-require 'chunky_png/canvas/png_encoding'
-require 'chunky_png/canvas/png_decoding'
-require 'chunky_png/canvas/adam7_interlacing'
-require 'chunky_png/canvas/stream_exporting'
-require 'chunky_png/canvas/stream_importing'
-require 'chunky_png/canvas/data_url_exporting'
-require 'chunky_png/canvas/data_url_importing'
-require 'chunky_png/canvas/operations'
-require 'chunky_png/canvas/drawing'
-require 'chunky_png/canvas/resampling'
-require 'chunky_png/canvas/masking'
+require "chunky_png/canvas/png_encoding"
+require "chunky_png/canvas/png_decoding"
+require "chunky_png/canvas/adam7_interlacing"
+require "chunky_png/canvas/stream_exporting"
+require "chunky_png/canvas/stream_importing"
+require "chunky_png/canvas/data_url_exporting"
+require "chunky_png/canvas/data_url_importing"
+require "chunky_png/canvas/operations"
+require "chunky_png/canvas/drawing"
+require "chunky_png/canvas/resampling"
+require "chunky_png/canvas/masking"
module ChunkyPNG
# The ChunkyPNG::Canvas class represents a raster image as a matrix of
# pixels.
#
@@ -54,11 +54,10 @@
# @return [Array<ChunkyPNG::Color>] The list of pixels in this canvas.
# This array always should have +width * height+ elements.
attr_reader :pixels
-
#################################################################
# CONSTRUCTORS
#################################################################
# Initializes a new Canvas instance.
@@ -66,23 +65,24 @@
# @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.
+ # {ChunkyPNG::Color#parse} can handle.
#
# @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)
- unless initial.length == width * height
- raise ArgumentError, "The initial array should have #{width}x#{height} = #{width*height} elements!"
+ if initial.is_a?(Array)
+ pixel_count = width * height
+ unless initial.length == pixel_count
+ raise ArgumentError, "The initial array should have #{width}x#{height} = #{pixel_count} elements!"
end
@pixels = initial
else
@pixels = Array.new(width * height, ChunkyPNG::Color.parse(initial))
end
@@ -102,11 +102,10 @@
# @return [ChunkyPNG::Canvas] The newly constructed canvas instance.
def self.from_canvas(canvas)
new(canvas.width, canvas.height, canvas.pixels.dup)
end
-
#################################################################
# PROPERTIES
#################################################################
# Returns the dimension (width x height) for this canvas.
@@ -141,11 +140,11 @@
# 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 [Integer] pixel The new color for the provided coordinates.
+ # @param [Integer] color 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
@@ -153,11 +152,11 @@
# 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 [Integer] pixel The new color value for the provided coordinates.
+ # @param [Integer] color 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
@@ -231,11 +230,11 @@
# @see ChunkyPNG.Point
def include_point?(*point_like)
dimension.include?(ChunkyPNG::Point(*point_like))
end
- alias_method :include?, :include_point?
+ alias include? 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)
@@ -272,15 +271,17 @@
# Equality check to compare this canvas with other matrices.
# @param other The object to compare this Matrix to.
# @return [true, false] True if the size and pixel values of the other
# canvas are exactly the same as this canvas's size and pixel values.
def eql?(other)
- other.kind_of?(self.class) && other.pixels == self.pixels &&
- other.width == self.width && other.height == self.height
+ other.is_a?(self.class) &&
+ other.pixels == pixels &&
+ other.width == width &&
+ other.height == height
end
- alias :== :eql?
+ alias == eql?
#################################################################
# EXPORTING
#################################################################
@@ -294,11 +295,11 @@
# @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(' ') << ']'
+ inspected << "\n\t[" << row(y).map { |p| ChunkyPNG::Color.to_hex(p) }.join(" ") << "]"
end
inspected << "\n]>"
end
protected
@@ -356,16 +357,16 @@
true
end
# Throws an exception if the matrix width and height does not match this canvas' dimensions.
def assert_size!(matrix_width, matrix_height)
- if width != matrix_width
+ if width != matrix_width
raise ChunkyPNG::ExpectationFailed,
- 'The width of the matrix does not match the canvas width!'
+ "The width of the matrix does not match the canvas width!"
end
if height != matrix_height
raise ChunkyPNG::ExpectationFailed,
- 'The height of the matrix does not match the canvas height!'
+ "The height of the matrix does not match the canvas height!"
end
true
end
end
end