lib/rubysketch/processing.rb in rubysketch-0.3.20 vs lib/rubysketch/processing.rb in rubysketch-0.3.21
- old
+ new
@@ -622,13 +622,38 @@
# @param dh [Numrtic] height of destination region
#
# @return [nil] nil
#
def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
+ blend img, sx, sy, sw, sh, dx, dy, dw, dh, :normal
+ end
+
+ # Blends image.
+ #
+ # @overload blend(sx, sy, sw, sh, dx, dy, dw, dh, mode)
+ # @overload blend(img, sx, sy, sw, sh, dx, dy, dw, dh, mode)
+ #
+ # @param img [Image] image for blend source
+ # @param sx [Numrtic] x position of source region
+ # @param sy [Numrtic] y position of source region
+ # @param sw [Numrtic] width of source region
+ # @param sh [Numrtic] height of source region
+ # @param dx [Numrtic] x position of destination region
+ # @param dy [Numrtic] y position of destination region
+ # @param dw [Numrtic] width of destination region
+ # @param dh [Numrtic] height of destination region
+ # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE] blend mode
+ #
+ # @return [nil] nil
+ #
+ def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
img ||= self
@image.paint do |painter|
+ current = painter.blend_mode
+ painter.blend_mode = mode
painter.image img.getInternal__, sx, sy, sw, sh, dx, dy, dw, dh
+ painter.blend_mode = current
end
end
# Saves image to file.
#
@@ -848,17 +873,21 @@
# PI * 2
#
TAU = PI * 2
- # RGB mode for colorMode().
+ # RGBA format for createImage().
#
- RGB = :rgb
+ RGBA = :rgba
+ # RGB format for createImage, or RGB mode for colorMode().
+ #
+ RGB = :rgb
+
# HSB mode for colorMode().
#
- HSB = :hsb
+ HSB = :hsb
# Radian mode for angleMode().
#
RADIANS = :radians
@@ -1005,10 +1034,11 @@
@colorMaxes__ = [1.0] * 4
@angleScale__ = 1.0
@rectMode__ = nil
@ellipseMode__ = nil
@imageMode__ = nil
+ @tint__ = nil
@textAlignH__ = nil
@textAlignV__ = nil
@matrixStack__ = []
@styleStack__ = []
@fontCache__ = {}
@@ -1019,15 +1049,18 @@
angleMode RADIANS
rectMode CORNER
ellipseMode CENTER
imageMode CORNER
blendMode BLEND
+ strokeCap ROUND
+ strokeJoin MITER
textAlign LEFT
fill 255
stroke 0
strokeWeight 1
+ noTint
end
# @private
def updateCanvas__(image, painter)
@image__, @painter__ = image, painter
@@ -1184,10 +1217,21 @@
when RADIUS then [a - c, b - d, c * 2, d * 2]
else raise ArgumentError # ToDo: refine error message
end
end
+ # Sets blend mode. Default is BLEND.
+ #
+ # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE]
+ #
+ # @return [nil] nil
+ #
+ def blendMode(mode)
+ @painter__.blend_mode = mode
+ nil
+ end
+
# Sets fill color.
#
# @overload fill(rgb)
# @overload fill(rgb, alpha)
# @overload fill(gray)
@@ -1281,10 +1325,41 @@
def strokeJoin(join)
@painter__.stroke_join join
nil
end
+ # Sets fill color for drawing images.
+ #
+ # @overload tint(rgb)
+ # @overload tint(rgb, alpha)
+ # @overload tint(gray)
+ # @overload tint(gray, alpha)
+ # @overload tint(r, g, b)
+ # @overload tint(r, g, b, alpha)
+ #
+ # @param rgb [String] color code like '#00AAFF'
+ # @param gray [Integer] gray value (0..255)
+ # @param r [Integer] red value (0..255)
+ # @param g [Integer] green value (0..255)
+ # @param b [Integer] blue value (0..255)
+ # @param alpha [Integer] alpha value (0..255)
+ #
+ # @return [nil] nil
+ #
+ def tint(*args)
+ @tint__ = args
+ nil
+ end
+
+ # Resets tint color.
+ #
+ # @return [nil] nil
+ #
+ def noTint()
+ @tint__ = nil
+ end
+
# Limits the drawable rectangle.
#
# The parameters a, b, c, and d are determined by rectMode().
#
# @param a [Numeric] horizontal position of the drawable area, by default
@@ -1307,21 +1382,10 @@
def noClip()
@painter__.no_clip
nil
end
- # Sets blend mode. Default is BLEND.
- #
- # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE]
- #
- # @return [nil] nil
- #
- def blendMode(mode)
- @painter__.blend_mode = mode
- nil
- end
-
# Sets font.
#
# @overload textFont(font)
# @overload textFont(name)
# @overload textFont(font, size)
@@ -1400,11 +1464,11 @@
assertDrawing__
rgba = toRGBA__(*args)
if rgba[3] == 1
@painter__.background(*rgba)
else
- @painter__.push fill: rgba, stroke: nil do |_|
+ @painter__.push fill: rgba, stroke: :none do |_|
@painter__.rect 0, 0, width, height
end
end
nil
end
@@ -1416,13 +1480,11 @@
#
# @return [nil] nil
#
def point(x, y)
assertDrawing__
- w = @painter__.stroke_width
- w = 1 if w == 0
- @painter__.ellipse x - (w / 2.0), y - (w / 2.0), w, w
+ @painter__.line x, y, x, y
nil
end
# Draws a line.
#
@@ -1496,13 +1558,11 @@
# @param extent [Numeric] width and height of the shape
#
# @return [nil] nil
#
def circle(x, y, extent)
- assertDrawing__
- @painter__.ellipse x, y, extent, extent
- nil
+ ellipse x, y, extent, extent
end
# Draws an arc.
#
# The parameters a, b, c, and d are determined by ellipseMode().
@@ -1665,13 +1725,16 @@
#
# @return [nil] nil
#
def image(img, a, b, c = nil, d = nil)
assertDrawing__
- i = img.getInternal__
+ i = img.getInternal__
x, y, w, h = toXYWH__ @imageMode__, a, b, c || i.width, d || i.height
- @painter__.image i, x, y, w, h
+ tint = @tint__ ? toRGBA__(*@tint__) : 1
+ @painter__.push fill: tint, stroke: :none do |_|
+ @painter__.image i, x, y, w, h
+ end
nil
end
# Copies image.
#
@@ -1689,15 +1752,49 @@
# @param dh [Numrtic] height of destination region
#
# @return [nil] nil
#
def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
+ blend img, sx, sy, sw, sh, dx, dy, dw, dh, BLEND
+ end
+
+ # Blends image.
+ #
+ # @overload blend(sx, sy, sw, sh, dx, dy, dw, dh, mode)
+ # @overload blend(img, sx, sy, sw, sh, dx, dy, dw, dh, mode)
+ #
+ # @param img [Image] image for blend source
+ # @param sx [Numrtic] x position of source region
+ # @param sy [Numrtic] y position of source region
+ # @param sw [Numrtic] width of source region
+ # @param sh [Numrtic] height of source region
+ # @param dx [Numrtic] x position of destination region
+ # @param dy [Numrtic] y position of destination region
+ # @param dw [Numrtic] width of destination region
+ # @param dh [Numrtic] height of destination region
+ # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE] blend mode
+ #
+ # @return [nil] nil
+ #
+ def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
assertDrawing__
- src = img&.getInternal__ || @window__.canvas_image
+ src = img&.getInternal__ || @window__.canvas_image
+ current = @painter__.blend_mode
+
+ @painter__.blend_mode = mode
@painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh
+ @painter__.blend_mode = current
end
+ # Saves screen image to file.
+ #
+ # @param filename [String] file name to save image
+ #
+ def save(filename)
+ @window__.canvas_image.save filename
+ end
+
# Applies translation matrix to current transformation matrix.
#
# @overload translate(x, y)
# @overload translate(x, y, z)
#
@@ -2183,10 +2280,21 @@
def createCanvas(width, height, pixelDensity: self.pixelDensity)
resizeCanvas__ :createCanvas, width, height, pixelDensity
nil
end
+ # Changes title of window.
+ #
+ # @param title [String] new title
+ #
+ # @return [nil] nil
+ #
+ def setTitle(title)
+ @window__.title = title
+ nil
+ end
+
# Changes and returns canvas pixel density.
#
# @param density [Numeric] new pixel density
#
# @return [Numeric] current pixel density
@@ -2712,9 +2820,26 @@
#
# @return [Vector] new vector
#
def createVector(*args)
Vector.new(*args, context: self)
+ end
+
+ # Creates a new image.
+ #
+ # @overload createImage(w, h)
+ # @overload createImage(w, h, format)
+ #
+ # @param w [Numeric] width of new image
+ # @param h [Numeric] height of new image
+ # @param format [RGB, RGBA] image format
+ #
+ # @return [Image] new image
+ #
+ def createImage(w, h, format = RGBA)
+ colorspace = {RGB => Rays::RGB, RGBA => Rays::RGBA}[format]
+ raise ArgumentError, "Unknown image format" unless colorspace
+ Image.new Rays::Image.new(w, h, colorspace).paint {background 0, 0}
end
# Creates a camera object as a video input device.
#
# @return [Capture] camera object