#!/usr/bin/env ruby
require "nkf"
def error(msg)
warn msg
exit(1)
end
magic = ARGF.read(4)
error("invalid format") unless magic == "ASEF"
major = ARGF.read(2).unpack("n").first
minor = ARGF.read(2).unpack("n").first
error("invalid version") unless major == 1 && minor == 0
colors = []
name = ""
number_of_blocks = ARGF.read(4).unpack("N").first
number_of_blocks.times do |i|
type = ARGF.read(2).unpack("n").first
length = ARGF.read(4).unpack("N").first
case type
# grounp start
when 0xC001
group_name_length = ARGF.read(2).unpack("n").first-1
name = NKF.nkf( "-W16 -w", ARGF.read(group_name_length*2) )
ARGF.read(2) # NULL ¤ò¼Î¤Æ¤ë
next
# color entry
when 0x0001
name_length = ARGF.read(2).unpack("n").first
name_utf16 = ARGF.read(name_length*2)
color_space = ARGF.read(4)
case color_space
when "RGB\040"
r = (ARGF.read(4).unpack("g").first * 255).round
g = (ARGF.read(4).unpack("g").first * 255).round
b = (ARGF.read(4).unpack("g").first * 255).round
color_type = ARGF.read(2).unpack("n").first
colors << "##{'%02x' % r}#{'%02x' % g}#{'%02x' % b}"
when "CMYK"
c = ARGF.read(4).unpack("g").first
m = ARGF.read(4).unpack("g").first
y = ARGF.read(4).unpack("g").first
k = ARGF.read(4).unpack("g").first
color_type = ARGF.read(2).unpack("n").first
r = ( ( 1.0 - [ 1.0, c*(1-k) + k ].min ) * 255).round
g = ( ( 1.0 - [ 1.0, m*(1-k) + k ].min ) * 255).round
b = ( ( 1.0 - [ 1.0, y*(1-k) + k ].min ) * 255).round
colors << "##{'%02x' % r}#{'%02x' % g}#{'%02x' % b}"
else
error("unsupported format. this converter can understand rgb only.")
end
# grounp end
when 0xC002
ARGF.read(length) if length > 0
next
end
end
DEFAULT_COLORS = <
END
require "stringio"
StringIO.open( "", "w" ) do |xml|
xml.puts %q[]
xml.puts %q[]
colors.each_with_index do |color,index|
xml.puts %Q[]
end
xml.puts DEFAULT_COLORS
xml.puts %q[]
File.open( "#{name.tr(" ","_")}.soc", "w" ) do |f|
f.puts NKF.nkf( "-w", xml.string )
end
end