class

CrImage::Paletted

Inherits CrImage::PalettedImage / CrImage::Image / Reference / Object

Paletted is an in-memory indexed color image.

Each pixel is stored as a single byte index (0-255) into a color palette. This format is memory-efficient for images with limited colors (e.g., GIF).

Memory layout: Contiguous byte array with 1 byte per pixel (palette index) Color model: Palette (up to 256 colors)

Use Paletted for:

  • GIF images (256 colors max)
  • Images with limited color palettes
  • Reducing file size through color quantization

Example:

palette = CrImage::Color::Palette.new([
  CrImage::Color::RED,
  CrImage::Color::GREEN,
  CrImage::Color::BLUE,
])
img = CrImage::Paletted.new(CrImage.rect(0, 0, 640, 480), palette)
img.set_color_index(100, 100, 0_u8) # Set to first palette color

Constructors

new(pix : Slice(UInt8), stride : Int32, rect : CrImage::Rectangle, palette : CrImage::Color::Palette)
Source
new(rect : CrImage::Rectangle = Rectangle.zero, palette : CrImage::Color::Palette = Color::Palette.new)
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 of the pixel at (x,y).

If coordinates are outside bounds, returns a default color (typically transparent black) rather than raising an exception. This simplifies image processing algorithms.

  • at(bounds().min.x, bounds().min.y) returns the upper-left pixel of the grid.
  • at(bounds().max.x-1, bounds().max.y-1) returns the lower-right one.

For explicit nil on out-of-bounds, concrete types provide at? method.

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
color_index_at(x : Int32, y : Int32) : UInt8

color_index_at returns the pallete index of the pixel at (x,y).

Source
color_model

color_model returns the Image's color model

Source
hash(hasher)

See Object#hash(hasher)

opaque?

opaque scans the entire image and reports whether it is fully opaque

Source
palette

Color palette containing up to 256 colors.

Source
pix

Returns a read-only view of the pixel buffer Modifying the returned Bytes directly is unsafe and may break image invariants Use at()/set() methods for safe pixel manipulation :nodoc:

Source
pixel_offset(x : Int32, y : Int32) : Int32
Source
rect

Rectangle defining the image's bounds.

Source
set(x : Int32, y : Int32, c : Color::Color)

Sets the color of the pixel at (x,y).

If the provided coordinates are not within bounds, this method does nothing (no-op). This simplifies drawing operations that may extend beyond image boundaries.

Source
set_color_index(x : Int32, y : Int32, index : UInt8)
Source
stride

Stride is the pixel buffer stride (in bytes) between vertically adjacent pixels.

Source
sub_image(r : Rectangle) : Image

sub_image returns an image representing the portion of the image visible though r. the returned value shares pixes with the original image.

Source