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

- old
+ new

@@ -10,24 +10,27 @@ # of the canvas; these pixels will simply be ignored. # @see ChunkyPNG::Canvas module Drawing # Composes a pixel on the canvas by alpha blending a color with its background color. - # @overload compose_pixel(x, y, color) - # @param [Integer] x The x-coordinate of the pixel to blend. - # @param [Integer] y The y-coordinate of the pixel to blend. - # @param [Integer] color The foreground color to blend with - # @overload compose_pixel(point, color) - # @param [ChunkyPNG::Point, ...] point The point on the canvas to blend. - # @param [Integer] color The foreground color to blend with - def compose_pixel(*args) - point = args.length == 2 ? ChunkyPNG::Point(args.first) : ChunkyPNG::Point(args[0], args[1]) - return unless include?(point) - color = ChunkyPNG::Color(args.last) - set_pixel(point.x, point.y, ChunkyPNG::Color.compose(color, get_pixel(point.x, point.y))) + # @param [Integer] x The x-coordinate of the pixel to blend. + # @param [Integer] y The y-coordinate of the pixel to blend. + # @param [Integer] color The foreground color to blend with + # @return [Integer] The composed color. + def compose_pixel(x, y, color) + return unless include_xy?(x, y) + compose_pixel_unsafe(x, y, ChunkyPNG::Color.parse(color)) end + # Composes a pixel on the canvas by alpha blending a color with its background color, + # without bounds checking. + # @param (see #compose_pixel) + # @return [Integer] The composed color. + def compose_pixel_unsafe(x, y, color) + set_pixel(x, y, ChunkyPNG::Color.compose(color, get_pixel(x, y))) + end + # Draws an anti-aliased line using Xiaolin Wu's algorithm. # # @param [Integer] x0 The x-coordinate of the first control point. # @param [Integer] y0 The y-coordinate of the first control point. # @param [Integer] x1 The x-coordinate of the second control point. @@ -36,11 +39,11 @@ # @param [true, false] inclusive Whether to draw the last pixel. # Set to false when drawing multiplelines in a path. # @return [ChunkyPNG::Canvas] Itself, with the line drawn. def line_xiaolin_wu(x0, y0, x1, y1, stroke_color, inclusive = true) - stroke_color = ChunkyPNG::Color(stroke_color) + stroke_color = ChunkyPNG::Color.parse(stroke_color) dx = x1 - x0 sx = dx < 0 ? -1 : 1 dx *= sx dy = y1 - y0 @@ -107,12 +110,12 @@ def polygon(path, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) vector = ChunkyPNG::Vector(*path) raise ArgumentError, "A polygon requires at least 3 points" if path.length < 3 - stroke_color = ChunkyPNG::Color(stroke_color) - fill_color = ChunkyPNG::Color(fill_color) + stroke_color = ChunkyPNG::Color.parse(stroke_color) + fill_color = ChunkyPNG::Color.parse(fill_color) # Fill unless fill_color == ChunkyPNG::Color::TRANSPARENT vector.y_range.each do |y| intersections = [] @@ -148,12 +151,12 @@ # @param [Integer] stroke_color The line color to use for this rectangle. # @param [Integer] fill_color The fill color to use for this rectangle. # @return [ChunkyPNG::Canvas] Itself, with the rectangle drawn. def rect(x0, y0, x1, y1, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) - stroke_color = ChunkyPNG::Color(stroke_color) - fill_color = ChunkyPNG::Color(fill_color) + stroke_color = ChunkyPNG::Color.parse(stroke_color) + fill_color = ChunkyPNG::Color.parse(fill_color) # Fill unless fill_color == ChunkyPNG::Color::TRANSPARENT [x0, x1].min.upto([x0, x1].max) do |x| [y0, y1].min.upto([y0, y1].max) do |y| @@ -179,11 +182,11 @@ # @param [Integer] stroke_color The color to use for the line. # @param [Integer] fill_color The color to use that fills the circle. # @return [ChunkyPNG::Canvas] Itself, with the circle drawn. def circle(x0, y0, radius, stroke_color = ChunkyPNG::Color::BLACK, fill_color = ChunkyPNG::Color::TRANSPARENT) - stroke_color = ChunkyPNG::Color(stroke_color) - fill_color = ChunkyPNG::Color(fill_color) + stroke_color = ChunkyPNG::Color.parse(stroke_color) + fill_color = ChunkyPNG::Color.parse(fill_color) f = 1 - radius ddF_x = 1 ddF_y = -2 * radius x = 0