class

Image::Uniform(T)

Inherits Image::PixelBuffer < Reference < Object

Uniform represents an image where every pixel has the same color.

This is a memory-efficient way to represent solid color images. Uses O(1) memory regardless of dimensions - stores only one pixel value.

Use cases:

  • Solid backgrounds (white canvas, black bars)
  • Testing with predictable values
  • Compositing with solid colors

The key benefit is polymorphism: code accepting PixelBuffer(T) works with Uniform without special cases, while using minimal memory.

Memory usage: sizeof(T) bytes (typically 4 bytes for RGBA8)

Features:

  • Constant O(1) memory usage
  • Fast value access
  • Polymorphic with memory-backed buffers

Example:

# 4 bytes instead of 8MB for 1920x1080 white background
white = Image::Uniform.new(Image::Color::RGBA8.new(255, 255, 255, 255), 1920, 1080)

# Works anywhere PixelBuffer is expected
def composite(bg : PixelBuffer(Color::RGBA8), fg : PixelBuffer(Color::RGBA8))
  # bg can be Uniform or RGBA8 - no special handling needed
end

Constructors

new(pixel : T, width : Int32, height : Int32)
Source
new(pixel : T, rect : Rectangle)
Source

Instance methods

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

Returns the pixel at (x, y), or default value if out of bounds

Source
bounds

Returns the rectangular bounds where pixels are defined

Source
opaque?

Reports whether the image is fully opaque. For uniform images, check if the single pixel is opaque. Uses duck typing - if pixel responds to a, check its alpha value.

Source
set(x : Int32, y : Int32, pixel : T) : Nil

Sets the pixel at (x, y), no-op if out of bounds

Source
stride

Stride is 0 for uniform images (no memory buffer)

Source
sub_image(rect : Rectangle) : Uniform(T)

Returns a sub-image view (new Uniform with intersected bounds)

Source
unsafe_at(x : Int32, y : Int32) : T

Returns the pixel at (x, y) without bounds checking. undefined behavior if out of bounds.

Source