funk/tools/palstr2gpl

30 lines
905 B
Python
Executable File

#!/usr/bin/python
import argparse
TITLE = "GIMP Palette"
NAME_HEAD = "Name: "
COMMENT = "# {}"
HEADER = "{}\n{}{{}}\n{}\n".format(TITLE,NAME_HEAD,COMMENT.format("Generated by palstring2gpl"))
COLOR_STR = "{:>3} {:>3} {:>3} Color{}\n"
parser = argparse.ArgumentParser(description="Converts TIC-80 style pal strings to GIMP palettes.")
parser.add_argument("palstr",help="File with the palette string.")
parser.add_argument("gpl",help="File to put result in.")
parser.add_argument("name",help="Name of palette. Defaults to 'New palette'.",default="New palette")
args = parser.parse_args()
def split(s,n):
for i in range(0,len(s),n):
yield s[i:i+n]
t = []
with open(args.palstr) as f:
t = [[int(x,16) for x in split(y,2)] for y in split(f.read().strip(),6)]
#print(t)
with open(args.gpl,"w") as f:
f.write(HEADER.format(args.name))
for c in range(len(t)):
f.write(COLOR_STR.format(*(t[c]+[c])))