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)
-
(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)
- 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)
-
(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)
- See: ChunkyPNG::Palette#can_decode? -
Returns:
-
(ChunkyPNG::Color)
- The color that is stored in the palette under the given index
Parameters:
-
index
(Fixnum
) -- The 0-based position of the color in the palette.
def [](index) @decoding_map[index] end
def best_colormode
-
(Fixnum)
- The colormode which would create the smalles possible
def best_colormode if grayscale? if opaque? ChunkyPNG::COLOR_GRAYSCALE else ChunkyPNG::COLOR_GRAYSCALE_ALPHA end elsif indexable? ChunkyPNG::COLOR_INDEXED elsif opaque? ChunkyPNG::COLOR_TRUECOLOR else ChunkyPNG::COLOR_TRUECOLOR_ALPHA end end
def can_decode?
-
(true, false)
- True if a decoding map was built when this palette was loaded.
def can_decode? !@decoding_map.nil? end
def can_encode?
-
(true, false)
- True if a encoding map was built when this palette was loaded.
def can_encode? !@encoding_map.nil? end
def grayscale?
- 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)
- See: ChunkyPNG::Palette#can_encode? -
Returns:
-
(Fixnum)
- 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) @encoding_map[color] end
def indexable?
-
(true, false)
- True if the number of colors in this palette is less than 256.
def indexable? size < 256 end
def initialize(enum, decoding_map = nil)
-
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?
- 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
- 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
- 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
-
(ChunkyPNG::Chunk::Transparency)
- The tRNS chunk.
def to_trns_chunk ChunkyPNG::Chunk::Transparency.new('tRNS', map { |c| ChunkyPNG::Color.a(c) }.pack('C*')) end