class IntelQA::Carrier::Grid(T)

Overview

A grid is 2D array of T pixels

To create a grid of size 400 x 200

grid = ImageCarrier::Grid.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 = ImageCarrier::Grid.new(400, 200, RGBA::WHITE)
canvas3 = ImageCarrier::Grid.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, Grids are limited to Int32::MAX = 2147483647 pixels in total, e.g. a maximal size of 46340x46340 for a square image.

Defined in:

grid.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(width : Int32 = 0, height : Int32 = 0, cell = T::NULL) #

[View source]
def self.new(width : Int32, height : Int32, &) #

[View source]

Instance Method Detail

def ==(other) #

Two canvases are considered equal if they are of equal size and all their pixels are equal


[View source]
def [](x, y) #

Short form for #get


[View source]
def []=(x, y, cell) #

Short form for #set


[View source]
def each(type = :grid, index = Int32::MIN, &) #

[View source]
def each_row(&) #

Iterate over each row of the grid (a Slice(RGBA) of size @width). The main usecase for this is writing code that encodes images in some file format.


[View source]
def fill(destination : GridRegion, source : Grid(T)) #

[View source]
def fill(destination : GridRegion, source : NamedTuple(grid: Grid(T), region: GridRegion)) #

[View source]
def fill(region : GridRegion, value : T) #

[View source]
def get(x, y) #

Get the value of pixel (x, y) without checking if (x, y) is a valid position.


[View source]
def height : Int32 #

[View source]
def includes_location?(x, y) #

Check if pixel (x, y) is part of this grid.


[View source]
def map(&) #

Same as #map!, but instead of mutating the current grid, a new one is created and returned


[View source]
def map!(&) #

Modify pixels by applying a function (color, x, y) -> new_color to each pixel of the current grid, e.g. to invert colors


[View source]
def map_with_index(&) #

Same as #map but passes along a fourth parameter with the index in the #pixels slice


[View source]
def map_with_index!(&block : T, Int32, Int32, Int32 -> T) #

Same as #map! but passes along a fourth parameter with the index in the #pixels slice


[View source]
def max #

[View source]
def min #

[View source]
def paste(grid : Grid(T), x, y) #

Paste the contents of a second Grid into this one, starting at position (x, y). The pixels are combined using the RGBA#over function.


[View source]
def pixel_type #

[View source]
def pixels : Slice(T) #

[View source]
def reduce(accumulator, &) #

[View source]
def safe_get(x : Int32, y : Int32) : T | Nil #

Same as #get, but returns nil if (x, y) are outside of the grid


[View source]
def safe_set(x : Int32, y : Int32, cell : T) : Bool #

Same as #set, but only sets the pixel, if it is part of the grid. Returns true if the pixel was set successfully, false if it was outside of the grid.


[View source]
def set(x, y, cell) #

Set the value of pixel (x, y) to color without checking if (x, y) is a valid position.


[View source]
def tile(destination : GridRegion, source : NamedTuple(grid: Grid(T), region: GridRegion)) #

[View source]
def width : Int32 #

[View source]
def wrapping_get(x : Int32, y : Int32) : T #

Same as #get, but if x ore y are outside of the grid, wrap them over at the edges. E.g. #wrapping_get(300, 250) on a 200x200 grid returns the pixel at (100, 50).


[View source]
def wrapping_set(x : Int32, y : Int32, cell : T) #

Same as #set, but wrapping along the grid edges. See #wrapping_get for an example.


[View source]