class StumpyCore::Canvas
- StumpyCore::Canvas
- Reference
- Object
Overview
A canvas is 2D array of RGBA
pixels
To create a canvas of size 400
x 200
canvas = StumpyCore::Canvas.new(400, 200)
The default background color is transparent,
but it can be passed in as a parameter or as a block
that returns the color value for each {x, y}
pair.
canvas2 = StumpyCore::Canvas.new(400, 200, RGBA::WHITE)
canvas3 = StumpyCore::Canvas.new(256, 256) do |x, y|
RGBA.from_rgb_n(x, y, 255, 8)
end
Because of the way pixels are stored in a Slice
,
Canvas
es are limited to Int32::MAX = 2147483647
pixels in total,
e.g. a maximal size of 46340x46340 for a square image.
Defined in:
stumpy_core/canvas.crConstructors
Instance Method Summary
-
#==(other)
Two canvases are considered equal if they are of equal size and all their pixels are equal
-
#[](x, y)
Short form for
#get
-
#[]=(x, y, color)
Short form for
#set
-
#each_row(&block)
Iterate over each row of the canvas (a
Slice(RGBA)
of size@width
). -
#get(x, y)
Get the value of pixel
(x, y)
without checking if(x, y)
is a valid position. - #height : Int32
-
#includes_pixel?(x, y)
Check if pixel
(x, y)
is part of this canvas. -
#map(&block)
Same as
#map!
, but instead of mutating the current canvas, a new one is created and returned -
#map!(&block)
Modify pixels by applying a function
(color, x, y) -> new_color
to each pixel of the current canvas, e.g. - #map_with_index(&block)
- #map_with_index!(&block)
-
#paste(canvas : Canvas, x, y)
Past the contents of a second
Canvas
into this one, starting at position(x, y)
. - #pixels : Slice(RGBA)
-
#safe_get(x : Int32, y : Int32) : RGBA?
Same as
#get
, but returnsnil
if(x, y)
are outside of the canvas -
#safe_set(x : Int32, y : Int32, color : RGBA) : Bool
Same as
#set
, but only sets the pixel, if it is part of the canvas. -
#set(x, y, color)
Set the value of pixel
(x, y)
tocolor
without checking if(x, y)
is a valid position. - #width : Int32
-
#wrapping_get(x : Int32, y : Int32) : RGBA
Same as
#get
, but ifx
orey
are outside of the canvas, wrap them over at the edges. -
#wrapping_set(x : Int32, y : Int32, color : RGBA)
Same as
#set
, but wrapping along the canvas edges.
Constructor Detail
Instance Method Detail
Two canvases are considered equal if they are of equal size and all their pixels are equal
Iterate over each row of the canvas
(a Slice(RGBA)
of size @width
).
The main usecase for this is
writing code that encodes images
in some file format.
Same as #map!
, but instead of mutating the current canvas,
a new one is created and returned
Modify pixels by
applying a function (color, x, y) -> new_color
to each pixel of the current canvas,
e.g. to invert colors
Past the contents of a second Canvas
into this one,
starting at position (x, y)
.
The pixels are combined using the RGBA#over
function.
Same as #get
,
but returns nil
if (x, y)
are outside of the canvas
Same as #set
, but only sets the pixel,
if it is part of the canvas.
Returns true
if the pixel was set successfully,
false
if it was outside of the canvas.
Set the value of pixel (x, y)
to color
without checking if (x, y)
is a valid position.
Same as #get
, but if x
ore y
are outside of the canvas,
wrap them over at the edges.
E.g. #wrapping_get(300, 250)
on a 200x200 canvas
returns the pixel at (100, 50)
.
Same as #set
, but wrapping along the canvas edges.
See #wrapping_get
for an example.