Class: Quake::Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/quake/palette.rb

Constant Summary collapse

SIZE =
256

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Palette

Returns a new instance of Palette.



7
8
9
10
11
12
13
14
# File 'lib/quake/palette.rb', line 7

def initialize(data)
  raise "Palette data must be 768 bytes, got #{data.bytesize}" unless data.bytesize == 768
  @rgb = Array.new(SIZE)
  SIZE.times do |i|
    r, g, b = data[i * 3, 3].unpack("C3")
    @rgb[i] = [r, g, b]
  end
end

Instance Method Details

#indexed_to_rgba(pixels, transparent_index: 255) ⇒ Object

Convert an array of 8-bit indexed pixels to RGBA bytes Last palette entry (255) is typically transparent



22
23
24
25
26
27
28
29
30
# File 'lib/quake/palette.rb', line 22

def indexed_to_rgba(pixels, transparent_index: 255)
  rgba = String.new(capacity: pixels.bytesize * 4)
  pixels.each_byte do |idx|
    r, g, b = @rgb[idx]
    a = (idx == transparent_index) ? 0 : 255
    rgba << r << g << b << a
  end
  rgba
end

#rgb(index) ⇒ Object



16
17
18
# File 'lib/quake/palette.rb', line 16

def rgb(index)
  @rgb[index]
end