Class: FreeImage::Palette

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/free-image/palette.rb

Overview

Represents a bitmaps palette. If the bitmap doesn’t have a palette (i.e. when its pixel bit depth is greater than 8), this function returns nil.

Instance Method Summary collapse

Constructor Details

#initialize(bitmap) ⇒ Palette

Returns a new instance of Palette.



14
15
16
17
# File 'lib/free-image/palette.rb', line 14

def initialize(bitmap)
  # Keep reference to bitmap so its not gc'ed
  @bitmap = bitmap
end

Instance Method Details

#[](index) ⇒ Object

Returns the index color as an instance of FreeImage::RGBQuad



28
29
30
31
32
33
34
35
36
# File 'lib/free-image/palette.rb', line 28

def [](index)
  unless (0..self.size).include?(index)
    raise(RangeError, "Value is out of range 0..#{self.size}. Value: #{index}")
  end

  ptr = FreeImage.FreeImage_GetPalette(@bitmap)
  FreeImage.check_last_error
  RGBQuad.new(ptr[index])
end

#eachObject



38
39
40
41
42
# File 'lib/free-image/palette.rb', line 38

def each
  size.times do |i|
    yield (self[i])
  end
end

#sizeObject Also known as: colors_used

Returns the palette-size for palletised bitmaps, and 0 for high-colour bitmaps.



20
21
22
23
24
# File 'lib/free-image/palette.rb', line 20

def size
  result = FreeImage.FreeImage_GetColorsUsed(@bitmap)
  FreeImage.check_last_error
  result
end