Class: ChunkyPNG::Palette
- Inherits:
-
SortedSet
- Object
- SortedSet
- ChunkyPNG::Palette
- Defined in:
- lib/chunky_png/palette.rb
Overview
A palette describes the set of colors that is being used for an image.
A PNG image can contain an explicit palette which defines the colors of that image, but can also use an implicit palette, e.g. all truecolor colors or all grayscale colors.
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).
Class Method Summary collapse
-
.from_canvas(canvas) ⇒ ChunkyPNG::Palette
Builds a palette instance from a given canvas.
-
.from_chunks(palette_chunk, transparency_chunk = nil) ⇒ ChunkyPNG::Palette
Builds a palette instance from a PLTE chunk and optionally a tRNS chunk from a PNG datastream.
-
.from_pixels(pixels) ⇒ ChunkyPNG::Palette
Builds a palette instance from a given set of pixels.
Instance Method Summary collapse
-
#[](index) ⇒ ChunkyPNG::Color
Returns a color, given the position in the original palette chunk.
-
#best_colormode ⇒ Fixnum
Determines the most suitable colormode for this palette.
-
#can_decode? ⇒ true, false
Checks whether this palette is suitable for decoding an image from a datastream.
-
#can_encode? ⇒ true, false
Checks whether this palette is suitable for encoding an image from to datastream.
-
#grayscale? ⇒ true, false
Check whether this pelette only contains grayscale colors.
-
#index(color) ⇒ Fixnum
Returns the position of a color in the palette.
-
#indexable? ⇒ true, false
Checks whether the size of this palette is suitable for indexed storage.
-
#initialize(enum, decoding_map = nil) ⇒ Palette
constructor
Builds a new palette given a set (Enumerable instance) of colors.
-
#opaque? ⇒ true, false
Check whether this pelette only contains opaque colors.
-
#to_plte_chunk ⇒ ChunkyPNG::Chunk::Palette
Creates a PLTE chunk that corresponds with this palette to store the r, g and b channels of all colors.
-
#to_trns_chunk ⇒ ChunkyPNG::Chunk::Transparency
Creates a tRNS chunk that corresponds with this palette to store the alpha channel of all colors.
Constructor Details
#initialize(enum, decoding_map = nil) ⇒ Palette
Builds a new palette given a set (Enumerable instance) of colors.
22 23 24 25 |
# File 'lib/chunky_png/palette.rb', line 22 def initialize(enum, decoding_map = nil) super(enum) @decoding_map = decoding_map if decoding_map end |
Class Method Details
.from_canvas(canvas) ⇒ ChunkyPNG::Palette
Builds a palette instance from a given canvas.
62 63 64 |
# File 'lib/chunky_png/palette.rb', line 62 def self.from_canvas(canvas) self.new(canvas.pixels) end |
.from_chunks(palette_chunk, transparency_chunk = nil) ⇒ ChunkyPNG::Palette
Builds a palette instance from a PLTE chunk and optionally a tRNS chunk from a PNG datastream.
This method will cerate a palette that is suitable for decoding an image.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/chunky_png/palette.rb', line 36 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 |
.from_pixels(pixels) ⇒ ChunkyPNG::Palette
Builds a palette instance from a given set of pixels.
69 70 71 |
# File 'lib/chunky_png/palette.rb', line 69 def self.from_pixels(pixels) self.new(pixels) end |
Instance Method Details
#[](index) ⇒ ChunkyPNG::Color
Returns a color, given the position in the original palette chunk.
117 118 119 |
# File 'lib/chunky_png/palette.rb', line 117 def [](index) @decoding_map[index] end |
#best_colormode ⇒ Fixnum
Determines the most suitable colormode for this palette.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/chunky_png/palette.rb', line 164 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 |
#can_decode? ⇒ true, false
Checks whether this palette is suitable for decoding an image from a datastream.
This requires that the positions of the colors in the original palette chunk is known, which is stored as an array in the @decoding_map instance variable.
99 100 101 |
# File 'lib/chunky_png/palette.rb', line 99 def can_decode? !@decoding_map.nil? end |
#can_encode? ⇒ true, false
Checks whether this palette is suitable for encoding an image from to datastream.
This requires that the position of the color in the future palette chunk is known, which is stored as a hash in the @encoding_map instance variable.
109 110 111 |
# File 'lib/chunky_png/palette.rb', line 109 def can_encode? !@encoding_map.nil? end |
#grayscale? ⇒ true, false
Check whether this pelette only contains grayscale colors.
89 90 91 |
# File 'lib/chunky_png/palette.rb', line 89 def grayscale? all? { |color| Color.grayscale?(color) } end |
#index(color) ⇒ Fixnum
Returns the position of a color in the palette
125 126 127 |
# File 'lib/chunky_png/palette.rb', line 125 def index(color) @encoding_map[color] end |
#indexable? ⇒ true, false
Checks whether the size of this palette is suitable for indexed storage.
75 76 77 |
# File 'lib/chunky_png/palette.rb', line 75 def indexable? size < 256 end |
#opaque? ⇒ true, false
Check whether this pelette only contains opaque colors.
82 83 84 |
# File 'lib/chunky_png/palette.rb', line 82 def opaque? all? { |color| Color.opaque?(color) } end |
#to_plte_chunk ⇒ ChunkyPNG::Chunk::Palette
Creates a PLTE chunk that corresponds with this palette to store the r, g and b channels of all colors.
Note that a PLTE chunk should only be included if the image is encoded using index colors. After this chunk has been built, the palette becomes suitable for encoding an image.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/chunky_png/palette.rb', line 149 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 |
#to_trns_chunk ⇒ ChunkyPNG::Chunk::Transparency
Creates a tRNS chunk that corresponds with this palette to store the alpha channel of all colors.
Note that this chunk can be left out of every color in the palette is opaque, and the image is encoded using indexed colors.
136 137 138 |
# File 'lib/chunky_png/palette.rb', line 136 def to_trns_chunk ChunkyPNG::Chunk::Transparency.new('tRNS', map { |c| ChunkyPNG::Color.a(c) }.pack('C*')) end |