Class: ZPNG::Chunk::PLTE

Inherits:
ZPNG::Chunk show all
Defined in:
lib/zpng/chunk.rb

Direct Known Subclasses

BMP::BmpPaletteChunk

Constant Summary

Constants inherited from ZPNG::Chunk

KNOWN_TYPES, VALID_SIZE_RANGE

Instance Attribute Summary collapse

Attributes inherited from ZPNG::Chunk

#crc, #data, #idx, #offset, #size, #type

Instance Method Summary collapse

Methods inherited from ZPNG::Chunk

#check, #crc_ok?, #export, #export_data, #fix_crc!, from_stream, #initialize, #inspect, #valid?

Methods included from DeepCopyable

#deep_copy

Constructor Details

This class inherits a constructor from ZPNG::Chunk

Instance Attribute Details

#max_colorsObject

Returns the value of attribute max_colors.



222
223
224
# File 'lib/zpng/chunk.rb', line 222

def max_colors
  @max_colors
end

Instance Method Details

#[](idx) ⇒ Object



224
225
226
227
# File 'lib/zpng/chunk.rb', line 224

def [] idx
  rgb = @data[idx*3,3]
  rgb && ZPNG::Color.new(*rgb.unpack('C3'))
end

#[]=(idx, color) ⇒ Object



229
230
231
232
# File 'lib/zpng/chunk.rb', line 229

def []= idx, color
  @data ||= ''
  @data[idx*3,3] = [color.r, color.g, color.b].pack('C3')
end

#add(color) ⇒ Object



265
266
267
268
269
270
# File 'lib/zpng/chunk.rb', line 265

def add color
  raise "palette full (#{ncolors}), cannot add #{color.inspect}" if ncolors >= max_colors
  idx = ncolors
  self[idx] = color
  idx
end

#eachObject

colors enumerator



239
240
241
242
243
# File 'lib/zpng/chunk.rb', line 239

def each
  ncolors.times do |i|
    yield self[i]
  end
end

#each_with_indexObject

colors enumerator with index



246
247
248
249
250
# File 'lib/zpng/chunk.rb', line 246

def each_with_index
  ncolors.times do |i|
    yield self[i], i
  end
end

#find_or_add(color) ⇒ Object Also known as: <<



272
273
274
# File 'lib/zpng/chunk.rb', line 272

def find_or_add color
  index(color) || add(color)
end

#index(color) ⇒ Object



257
258
259
260
261
262
263
# File 'lib/zpng/chunk.rb', line 257

def index color
  ncolors.times do |i|
    c = self[i]
    return i if c.r == color.r && c.g == color.g && c.b == color.b
  end
  nil
end

#ncolorsObject



234
235
236
# File 'lib/zpng/chunk.rb', line 234

def ncolors
  @data.to_s.size/3
end

#to_aObject

convert to array of colors



253
254
255
# File 'lib/zpng/chunk.rb', line 253

def to_a
  ncolors.times.map{ |i| self[i] }
end