class

CrImage::RGBA

Inherits CrImage::Image / Reference / Object

RGBA is an in-memory image with premultiplied alpha.

Each pixel is stored as 4 bytes (R, G, B, A) with color values premultiplied by alpha for efficient compositing. This is the most common image format for rendering and display.

Memory layout: Contiguous byte array with 4 bytes per pixel Color model: RGBA with premultiplied alpha

Example:

img = CrImage::RGBA.new(CrImage.rect(0, 0, 640, 480))
img.set_rgba(100, 100, CrImage::Color::RGBA.new(255, 0, 0, 255))

Constructors

from_buffer(buffer : Bytes, width : Int32, height : Int32) : self

Creates an image from an existing pixel buffer.

Convenience constructor that calculates stride automatically. The buffer is used directly (not copied), so modifications to the image will affect the original buffer.

Parameters:

  • buffer : Raw pixel data in row-major order
  • width : Image width in pixels
  • height : Image height in pixels

Raises: ArgumentError if buffer size doesn't match expected size

Example:

# Create 100x100 RGBA image from raw bytes
pixels = Bytes.new(100 * 100 * 4)
img = CrImage::RGBA.from_buffer(pixels, 100, 100)
Source
new(r : Rectangle)
Source
new(pix : Slice(UInt8) = Bytes.empty, stride : Int32 = 0, rect : CrImage::Rectangle = Rectangle.zero)
Source

Instance methods

==(other : self)

Returns true if this reference is the same as other. Invokes same?.

at(x : Int32, y : Int32) : Color::Color

Returns the color at the specified coordinates.

If the coordinates are outside the image bounds, returns a default color (typically transparent black) rather than raising an exception. This is intentional behavior to simplify image processing algorithms that may sample outside bounds.

For explicit bounds checking, use bounds.in?(Point.new(x, y)) before calling.

Parameters:

  • x : X coordinate
  • y : Y coordinate

Returns: The color at (x, y), or default color if out of bounds

Example:

img = CrImage.rgba(100, 100)
color = img.at(50, 50)      # Returns actual color
color = img.at(200, 200)    # Returns default color (out of bounds)

# Explicit bounds check:
if img.bounds.in?(CrImage.point(x, y))
  color = img.at(x, y)  # Guaranteed to be actual pixel
end
Source
at?(x : Int32, y : Int32) : Color::RGBA | Nil

Returns the color at the specified coordinates, or nil if out of bounds.

This is an alternative to at that explicitly returns nil for out-of-bounds coordinates, making bounds checking more explicit in the type system.

Parameters:

  • x : X coordinate
  • y : Y coordinate

Returns: The color at (x, y), or nil if out of bounds

Example:

img = CrImage.rgba(100, 100)
if color = img.at?(50, 50)
  # color is guaranteed to be from the image
end

img.at?(200, 200)  # => nil (out of bounds)
Source
bounds

bounds returns the domain for which at can return non-zero color. The bounds do not necessarily contain the point(0,0).

Source
clear

Optimized clear for RGBA images

Source
color_model

color_model returns the Image's color model

Source
fill(color : Color::Color)

Optimized fill for RGBA images using bulk operations

Source
flood_fill(x : Int32, y : Int32, fill_color : Color::Color, tolerance : Int32 = 10) : Int32

Performs flood fill starting from a point.

Source
hash(hasher)

See Object#hash(hasher)

opaque?
Source
pixel_offset(x : Int32, y : Int32) : Int32
Source
rect
rgba_at(x : Int32, y : Int32) : Color::RGBA
Source
set(x : Int32, y : Int32, c : Color::Color)

Sets the color at the specified coordinates.

If the coordinates are outside the image bounds, this method does nothing (no-op) rather than raising an exception. This is intentional behavior to simplify drawing operations that may extend beyond image boundaries.

For explicit bounds checking, use bounds.in?(Point.new(x, y)) before calling.

Parameters:

  • x : X coordinate
  • y : Y coordinate
  • c : Color to set

Example:

img = CrImage.rgba(100, 100)
img.set(50, 50, CrImage::Color::RED)    # Sets pixel
img.set(200, 200, CrImage::Color::RED)  # No-op (out of bounds)
Source
set_rgba(x : Int32, y : Int32, c : Color::RGBA)
Source
stride
sub_image(r : Rectangle) : Image
Source
swap_buffer(buffer : Bytes) : Nil

Swaps the underlying pixel buffer without allocation.

Useful for video/realtime processing where you want to reuse an image object with different frame data.

Parameters:

  • buffer : New pixel buffer (must match current dimensions)

Raises: ArgumentError if buffer size doesn't match

Example:

img = CrImage::RGBA.from_buffer(frame1, 1920, 1080)
process(img)
img.swap_buffer(frame2)  # Zero allocation
process(img)
Source
to_hsl

Convert entire image through HSL color space and back to RGBA This applies HSL transformation to every pixel

Source
to_hsv

Convert entire image through HSV color space and back to RGBA This applies HSV transformation to every pixel

Source
to_lab

Convert entire image through LAB color space and back to RGBA This applies LAB transformation to every pixel

Source