lib/chunky_png/palette.rb in chunky_png-1.3.14 vs lib/chunky_png/palette.rb in chunky_png-1.3.15

- old
+ new

@@ -10,21 +10,23 @@ # This palette supports decoding colors from a palette if an explicit palette # is provided in a PNG datastream, and it supports encoding colors to an # explicit palette (stores as PLTE & tRNS chunks in a PNG file). # # @see ChunkyPNG::Color - class Palette < SortedSet + class Palette < Set # Builds a new palette given a set (Enumerable instance) of colors. # # @param enum [Enumerable<Integer>] The set of colors to include in this # palette.This Enumerable can contain duplicates. # @param decoding_map [Array] An array of colors in the exact order at # which they appeared in the palette chunk, so that this array can be # used for decoding. def initialize(enum, decoding_map = nil) - super(enum) + super(enum.sort.freeze) @decoding_map = decoding_map if decoding_map + @encoding_map = {} + freeze end # Builds a palette instance from a PLTE chunk and optionally a tRNS chunk # from a PNG datastream. # @@ -133,11 +135,11 @@ # variable. # # @return [true, false] True if a encoding map was built when this palette # was loaded. def can_encode? - !@encoding_map.nil? + !@encoding_map.empty? end # Returns a color, given the position in the original palette chunk. # @param index [Integer] The 0-based position of the color in the palette. # @return [ChunkyPNG::Color] The color that is stored in the palette under @@ -174,11 +176,11 @@ # suitable for encoding an image. # # @return [ChunkyPNG::Chunk::Palette] The PLTE chunk. # @see ChunkyPNG::Palette#can_encode? def to_plte_chunk - @encoding_map = {} - colors = [] + @encoding_map.clear + colors = [] each_with_index do |color, index| @encoding_map[color] = index colors += ChunkyPNG::Color.to_truecolor_bytes(color) end