Class: ZPNG::Chunk::PLTE
- Inherits:
-
ZPNG::Chunk
- Object
- ZPNG::Chunk
- ZPNG::Chunk::PLTE
- Defined in:
- lib/zpng/chunk.rb
Direct Known Subclasses
Constant Summary
Constants inherited from ZPNG::Chunk
Instance Attribute Summary collapse
-
#max_colors ⇒ Object
Returns the value of attribute max_colors.
Attributes inherited from ZPNG::Chunk
#crc, #data, #idx, #offset, #size, #type
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #[]=(idx, color) ⇒ Object
- #add(color) ⇒ Object
-
#each ⇒ Object
colors enumerator.
-
#each_with_index ⇒ Object
colors enumerator with index.
- #find_or_add(color) ⇒ Object (also: #<<)
- #index(color) ⇒ Object
- #ncolors ⇒ Object
-
#to_a ⇒ Object
convert to array of colors.
Methods inherited from ZPNG::Chunk
#check, #crc_ok?, #export, #export_data, #fix_crc!, from_stream, #initialize, #inspect, #valid?
Methods included from DeepCopyable
Constructor Details
This class inherits a constructor from ZPNG::Chunk
Instance Attribute Details
#max_colors ⇒ Object
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 |
#each ⇒ Object
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_index ⇒ Object
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 |
#ncolors ⇒ Object
234 235 236 |
# File 'lib/zpng/chunk.rb', line 234 def ncolors @data.to_s.size/3 end |
#to_a ⇒ Object
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 |