Module: ASE::Writer

Included in:
ASE
Defined in:
lib/ase/writer.rb

Instance Method Summary collapse

Instance Method Details

#to_file(file) ⇒ Object



3
4
5
6
# File 'lib/ase/writer.rb', line 3

def to_file(file)
  @file = file
  write!
end

#write!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ase/writer.rb', line 8

def write!
  raise "Must specify an output file" if @file.nil?
  @file = File.new(@file, 'wb')

  palette_count = @palettes.length
  color_count = @palettes.inject(0) { |sum, p| p.size }

  # Signature
  @file.write "ASEF"

  # Version
  @file.write_ushort 1, 0

  # Number of blocks
  @file.write_ulong(color_count + (palette_count * 2))

  @palettes.each do |palette_name, palette|
    # Block start
    @file.write_ushort 0xC001

    # Block length (title is UTF-16 encoded)
    @file.write_ulong 4 + (palette_name.length * 2)

    # Palette name
    @file.write_string palette_name

    palette.colors.each do |name, color|
      # Color start
      @file.write_ushort 1

      # Block length
      @file.write_ulong 22 + (name.length * 2)

      # Color name
      @file.write_string name

      # Color mode
      @file.write 'RGB '

      # Colors
      rgb = color.to_rgb.map { |c| c / 255 }
      rgb.each { |c| @file.write [c].pack('F').reverse }
      
      # End of colors
      @file.write_null_byte
    end

    @file.write_ushort 0xC002 # Group end
    @file.write_ulong 0 # Group end block
  end

  @file.close
end