Class: GD2::Palette::IndexedColor

Inherits:
GD2::Palette show all
Includes:
Enumerable
Defined in:
lib/gd2/palette.rb

Instance Attribute Summary

Attributes inherited from GD2::Palette

#image

Instance Method Summary collapse

Methods inherited from GD2::Palette

#<<, #[], #[]=, #allocate, #available, #closest, #closest_hwb, #deallocate, #exact, #exact!, #initialize, #resolve, #size, #used

Constructor Details

This class inherits a constructor from GD2::Palette

Instance Method Details

#deallocate_unusedObject

:nodoc:


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/gd2/palette.rb', line 188

def deallocate_unused   #:nodoc:
  used = @image.collect.flatten.uniq.inject(Array.new(MAX_COLORS)) do
      |ary, c|
    ary[c] = true
    ary
  end
  count = 0
  each do |color|
    unless used.at(color.index)
      self[color.index] = nil
      count += 1
    end
  end
  count.zero? ? nil : count
end

#eachObject

Iterate through every color allocated in this palette.


180
181
182
183
184
185
186
# File 'lib/gd2/palette.rb', line 180

def each  #:yields: color
  (0...MAX_COLORS).each do |i|
    color = self[i]
    yield color unless color.nil?
  end
  self
end

#inspectObject

:nodoc:


164
165
166
# File 'lib/gd2/palette.rb', line 164

def inspect   #:nodoc:
  "#<#{self.class} [#{used}]>"
end