Skip to content

Instantly share code, notes, and snippets.

@chaudum
Created May 31, 2021 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaudum/67aa2e5b25be363581bbbe0816e87f57 to your computer and use it in GitHub Desktop.
Save chaudum/67aa2e5b25be363581bbbe0816e87f57 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import json
import sys
import re
from typing import Dict, List, Tuple
def parse_args():
parser = argparse.ArgumentParser(description="")
parser.add_argument(
"--infile", default=sys.stdin, type=argparse.FileType("r")
)
parser.add_argument(
"--outfile", default=sys.stdout, type=argparse.FileType("w")
)
return parser.parse_args()
def convert(infile, outfile) -> None:
colormap: Dict[str, str] = {}
colors: List[Tuple[str, str]] = []
for line in infile:
line = re.sub(r"\s+", " ", line.strip())
if line.startswith("#define"):
# definition
_, name, color, *r = line.split(" ")
colormap[name] = color
if line.startswith("*.color"):
# assignment
color, ref = line.split(" ")
color = color[2:-1]
colors.append((color, colormap.get(ref, ref)))
# zipped = [a for b in zip(colors[:7], colors[8:]) for a in b]
palette = ":".join(c for _, c in colors)
print(" | ".join(f"{a}={b}" for a, b in colors))
outfile.write(f"palette = \"{palette}\"")
if __name__ == "__main__":
params = parse_args()
convert(params.infile, params.outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment