Class: GD2::Palette

Class | GD2::Palette
In: | lib/gd2/palette.rb
Parent: | Object

# Description

Palette objects are associated with an Image and hold the selection of pixel colors available to the Image. This is primarily a concern for Image::IndexedColor images, but their use with Image::TrueColor images is supported for consistency.

## Obtaining

Obtain a Palette object from the associated Image:

      palette = image.palette

### Methods

<< [] []= allocate available closest closest_hwb deallocate deallocate_unused exact exact! length resolve size used

### Classes and Modules

Class GD2::Palette::ColorNotFoundError
Class GD2::Palette::IndexedColor
Class GD2::Palette::PaletteFullError
Class GD2::Palette::TrueColor

### Attributes

image | [R] | The Image associated with this Palette

### Public Instance methods

<<(color)

Ensure the given `color` is present in this palette, allocating it if necessary. Returns the palette so calls may be stacked.

[](index)

Return the color allocated to the specified `index`, or nil if the entry is unallocated.

[]=(index, color)

Assign (allocate) the given `color` to the palette entry identified by `index`. If the entry was previously allocated to a different color, every pixel in the image having the given color `index` will effectively be changed to the new color. This method cannot be used with Image::TrueColor palettes.

allocate(color)

Assign the given `color` to an unoccupied entry in this palette and return it. Does not check whether the color is already allocated, and raises an error for Image::IndexedColor palettes if the palette is full.

available()

Return the number of palette entries available to be allocated for new colors.

closest(color)

Return the color in this palette that is closest to the given `color` according to Euclidian distance.

closest_hwb(color)

Return the color in this palette that is closest to the given `color` according to hue, whiteness, and blackness.

deallocate(color)

Remove the given `color` from this palette.

deallocate_unused()

Remove all colors from this palette that are not currently in use by the associated Image. This is an expensive operation. Returns the number of palette entries deallocated.

exact(color)

Locate the given `color` in this palette and return it. Returns nil if the color is not presently in the palette.

exact!(color)

Like Palette#exact except an error is raised if the color is not presently in the palette.

length()

Alias for size

resolve(color)

Locate the given `color` in this palette and return it if found; otherwise try to allocate the `color`, or if the palette is full, return a color from the palette that is closest to it.

size()

Return the maximum number of colors this palette can hold.

used()

Return the number of colors presently allocated in this palette.

[Validate]