class ChunkyPNG::Palette
@see ChunkyPNG::Color
explicit palette (stores as PLTE & tRNS chunks in a PNG file).
is provided in a PNG datastream, and it supports encoding colors to an
This palette supports decoding colors from a palette if an explicit palette
or all grayscale colors.
that image, but can also use an implicit palette, e.g. all truecolor colors
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) # Although we don't need to call .uniq.sort before initializing, because # Palette subclasses SortedSet, we get significantly better performance # by doing so. new(canvas.pixels.uniq.sort) end
def self.from_chunks(palette_chunk, transparency_chunk = nil)
- See: ChunkyPNG::Palette#can_decode? -
Returns:
-
(ChunkyPNG::Palette)- The loaded palette instance.
Parameters:
-
transparency_chunk(ChunkyPNG::Chunk::Transparency, nil) -- The -
palette_chunk(ChunkyPNG::Chunk::Palette) -- The palette chunk to
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*") alpha_channel = transparency_chunk ? transparency_chunk.content.unpack("C*") : [] 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 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
def self.from_pixels(pixels) new(pixels) end
def [](index)
- See: ChunkyPNG::Palette#can_decode? -
Returns:
-
(ChunkyPNG::Color)- The color that is stored in the palette under
Parameters:
-
index(Integer) -- The 0-based position of the color in the palette.
def [](index) @decoding_map[index] end
def best_color_settings
-
(Integer)- The colormode which would create the smallest 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?
- See: ChunkyPNG::Color#grayscale?? -
Returns:
-
(true, false)- True if all colors in this palette are grayscale
def black_and_white? entries == [ChunkyPNG::Color::BLACK, ChunkyPNG::Color::WHITE] end
def can_decode?
-
(true, false)- True if a decoding map was built when this palette
def can_decode? !@decoding_map.nil? end
def can_encode?
-
(true, false)- True if a encoding map was built when this palette
def can_encode? !@encoding_map.empty? end
def determine_bit_depth
-
(Integer)- Number of bits per pixel, i.e. 1, 2, 4 or 8, or nil if
def determine_bit_depth case size when 1..2 then 1 when 3..4 then 2 when 5..16 then 4 when 17..256 then 8 end end
def grayscale?
- See: ChunkyPNG::Color#grayscale?? -
Returns:
-
(true, false)- True if all colors in this palette are grayscale
def grayscale? all? { |color| Color.grayscale?(color) } end
def index(color)
- 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?
-
(true, false)- True if the number of colors in this palette is at
def indexable? size <= 256 end
def initialize(enum, decoding_map = nil)
-
decoding_map(Array) -- An array of colors in the exact order at -
enum(Enumerable) -- The set of colors to include in this
def initialize(enum, decoding_map = nil) super(enum.sort.freeze) @decoding_map = decoding_map if decoding_map @encoding_map = {} freeze 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
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.
Other tags:
- Note: - A PLTE chunk should only be included if the image is encoded using
def to_plte_chunk @encoding_map.clear 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