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.



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

def max_colors
  @max_colors
end

Instance Method Details

#[](idx) ⇒ Object



226
227
228
229
# File 'lib/zpng/chunk.rb', line 226

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

#[]=(idx, color) ⇒ Object



231
232
233
234
# File 'lib/zpng/chunk.rb', line 231

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

#add(color) ⇒ Object



267
268
269
270
271
272
# File 'lib/zpng/chunk.rb', line 267

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



241
242
243
244
245
# File 'lib/zpng/chunk.rb', line 241

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

#each_with_indexObject

colors enumerator with index



248
249
250
251
252
# File 'lib/zpng/chunk.rb', line 248

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

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



274
275
276
# File 'lib/zpng/chunk.rb', line 274

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

#index(color) ⇒ Object



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

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



236
237
238
# File 'lib/zpng/chunk.rb', line 236

def ncolors
  @data.to_s.size/3
end

#to_aObject

convert to array of colors



255
256
257
# File 'lib/zpng/chunk.rb', line 255

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