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, rect : Rectangle)
SourceInstance methods
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.
sub_image(rect : Rectangle) : Uniform(T)
Returns a sub-image view (new Uniform with intersected bounds)