Class: ZIMG::PNG::Chunk::PLTE

Inherits:
ZIMG::PNG::Chunk show all
Defined in:
lib/zimg/png/chunks.rb

Constant Summary

Constants inherited from ZIMG::PNG::Chunk

KNOWN_TYPES, VALID_SIZE_RANGE

Instance Attribute Summary collapse

Attributes inherited from ZIMG::PNG::Chunk

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

Instance Method Summary collapse

Methods inherited from ZIMG::PNG::Chunk

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

Methods included from DeepCopyable

#deep_copy

Methods inherited from Chunk

#cli_color

Constructor Details

This class inherits a constructor from ZIMG::PNG::Chunk

Instance Attribute Details

#max_colorsObject

Returns the value of attribute max_colors.



219
220
221
# File 'lib/zimg/png/chunks.rb', line 219

def max_colors
  @max_colors
end

Instance Method Details

#[](idx) ⇒ Object



221
222
223
224
# File 'lib/zimg/png/chunks.rb', line 221

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

#[]=(idx, color) ⇒ Object



226
227
228
229
# File 'lib/zimg/png/chunks.rb', line 226

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

#add(color) ⇒ Object



262
263
264
265
266
267
268
# File 'lib/zimg/png/chunks.rb', line 262

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



236
237
238
239
240
# File 'lib/zimg/png/chunks.rb', line 236

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

#each_with_indexObject

colors enumerator with index



243
244
245
246
247
# File 'lib/zimg/png/chunks.rb', line 243

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

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



270
271
272
# File 'lib/zimg/png/chunks.rb', line 270

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

#index(color) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/zimg/png/chunks.rb', line 254

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



231
232
233
# File 'lib/zimg/png/chunks.rb', line 231

def ncolors
  @data.to_s.size / 3
end

#to_aObject

convert to array of colors



250
251
252
# File 'lib/zimg/png/chunks.rb', line 250

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