class ChunkyPNG::Palette

@see ChunkyPNG::Color
to an explicit palette (stores as PLTE & tRNS chunks in a PNG file).
palette is provided in a PNG datastream, and it supports encoding colors
This palette supports decoding colors from a palette if an explicit
colors or all grayscale colors.
that image, but can also use an implicit palette, e.g. all truecolor
A PNG image can contain an explicit palette which defines the colors of
A palette describes the set of colors that is being used for an image.

def self.from_canvas(canvas)

Returns:
  • (ChunkyPNG::Palette) - The palette instance.

Parameters:
  • canvas (ChunkyPNG::Canvas) -- The canvas to create a palette for.
def self.from_canvas(canvas)
  self.new(canvas.pixels)
end

def self.from_chunks(palette_chunk, transparency_chunk = nil)

Other tags:
    See: ChunkyPNG::Palette#can_decode? -

Returns:
  • (ChunkyPNG::Palette) - The loaded palette instance.

Parameters:
  • The (ChunkyPNG::Chunk::Transparency, nil) -- optional transparency chunk.
  • The (ChunkyPNG::Chunk::Palette) -- palette chunk to load from
def self.from_chunks(palette_chunk, transparency_chunk = nil)
  return nil if palette_chunk.nil?
  decoding_map = []
  index = 0
  palatte_bytes = palette_chunk.content.unpack('C*')
  if transparency_chunk
    alpha_channel = transparency_chunk.content.unpack('C*')
  else
    alpha_channel = []
  end
  index = 0
  palatte_bytes.each_slice(3) do |bytes|
    bytes << alpha_channel.fetch(index, ChunkyPNG::Color::MAX)
    decoding_map << ChunkyPNG::Color.rgba(*bytes)
    index += 1
  end
  self.new(decoding_map, decoding_map)
end

def self.from_pixels(pixels)

Returns:
  • (ChunkyPNG::Palette) - The palette instance.

Parameters:
  • pixels (Enumerable) -- An enumeration of pixels to create a palette for
def self.from_pixels(pixels)
  self.new(pixels)
end

def [](index)

Other tags:
    See: ChunkyPNG::Palette#can_decode? -

Returns:
  • (ChunkyPNG::Color) - The color that is stored in the palette under the given index

Parameters:
  • index (Integer) -- The 0-based position of the color in the palette.
def [](index)
  @decoding_map[index]
end

def best_color_settings

Returns:
  • (Integer) - The colormode which would create the smalles possible
def best_color_settings
  if black_and_white?
    [ChunkyPNG::COLOR_GRAYSCALE, 1]
  elsif grayscale?
    if opaque?
      [ChunkyPNG::COLOR_GRAYSCALE, 8]
    else
      [ChunkyPNG::COLOR_GRAYSCALE_ALPHA, 8]
    end
  elsif indexable?
    [ChunkyPNG::COLOR_INDEXED, determine_bit_depth]
  elsif opaque?
    [ChunkyPNG::COLOR_TRUECOLOR, 8]
  else
    [ChunkyPNG::COLOR_TRUECOLOR_ALPHA, 8]
  end
end

def black_and_white?

Other tags:
    See: ChunkyPNG::Color#grayscale?? -

Returns:
  • (true, false) - True if all colors in this palette are grayscale teints.
def black_and_white?
  entries == [ChunkyPNG::Color::BLACK, ChunkyPNG::Color::WHITE]
end

def can_decode?

Returns:
  • (true, false) - True if a decoding map was built when this palette was loaded.
def can_decode?
  !@decoding_map.nil?
end

def can_encode?

Returns:
  • (true, false) - True if a encoding map was built when this palette was loaded.
def can_encode?
  !@encoding_map.nil?
end

def determine_bit_depth

Returns:
  • (Integer) - Number of bits per pixel, i.e. 1, 2, 4 or 8, or nil if this
def determine_bit_depth
  case size
    when 1..2; 1
    when 3..4; 2
    when 5..16; 4
    when 17..256; 8
    else nil
  end
end

def grayscale?

Other tags:
    See: ChunkyPNG::Color#grayscale?? -

Returns:
  • (true, false) - True if all colors in this palette are grayscale teints.
def grayscale?
  all? { |color| Color.grayscale?(color) }
end

def index(color)

Other tags:
    See: ChunkyPNG::Palette#can_encode? -

Returns:
  • (Integer) - The 0-based position of the color in the palette.

Parameters:
  • color (ChunkyPNG::Color) -- The color for which to look up the index.
def index(color)
  color.nil? ? 0 : @encoding_map[color]
end

def indexable?

Returns:
  • (true, false) - True if the number of colors in this palette is at most 256.
def indexable?
  size <= 256
end

def initialize(enum, decoding_map = nil)

Parameters:
  • decoding_map (Array) -- An array of colors in the exact order at which
  • enum (Enumerbale) -- The set of colors to include in this palette.
def initialize(enum, decoding_map = nil)
  super(enum)
  @decoding_map = decoding_map if decoding_map
end

def opaque?

Other tags:
    See: ChunkyPNG::Color#opaque? -

Returns:
  • (true, false) - True if all colors in this palette are opaque.
def opaque?
  all? { |color| Color.opaque?(color) }
end

def opaque_palette

Other tags:
    See: ChunkyPNG::Color#opaque! -

Returns:
  • (ChunkyPNG::Palette) - A new Palette instance with only opaque colors.
def opaque_palette
  self.class.new(map { |c| ChunkyPNG::Color.opaque!(c) })
end

def to_plte_chunk

Other tags:
    See: ChunkyPNG::Palette#can_encode? -

Returns:
  • (ChunkyPNG::Chunk::Palette) - The PLTE chunk.
def to_plte_chunk
  @encoding_map = {}
  colors        = []
  each_with_index do |color, index|
    @encoding_map[color] = index
    colors += ChunkyPNG::Color.to_truecolor_bytes(color)
  end
  ChunkyPNG::Chunk::Palette.new('PLTE', colors.pack('C*'))
end

def to_trns_chunk

Returns:
  • (ChunkyPNG::Chunk::Transparency) - The tRNS chunk.
def to_trns_chunk
  ChunkyPNG::Chunk::Transparency.new('tRNS', map { |c| ChunkyPNG::Color.a(c) }.pack('C*'))
end