Class: ZPNG::Chunk::PLTE

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

Instance Attribute Summary collapse

Attributes inherited from ZPNG::Chunk

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

Instance Method Summary collapse

Methods inherited from ZPNG::Chunk

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

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.



197
198
199
# File 'lib/zpng/chunk.rb', line 197

def max_colors
  @max_colors
end

Instance Method Details

#[](idx) ⇒ Object



199
200
201
202
# File 'lib/zpng/chunk.rb', line 199

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

#[]=(idx, color) ⇒ Object



204
205
206
207
# File 'lib/zpng/chunk.rb', line 204

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

#add(color) ⇒ Object



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

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

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



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

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

#index(color) ⇒ Object



213
214
215
216
217
218
219
# File 'lib/zpng/chunk.rb', line 213

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



209
210
211
# File 'lib/zpng/chunk.rb', line 209

def ncolors
  @data.to_s.size/3
end