#!/usr/bin/env python3 """Turn CDE palette files (*.dp) into a C table for splinter. A CDE palette file holds eight colours, one per line, in the order the desktop numbers its colour sets. Two encodings occur in the CDE tree: #RRRRGGGGBBBB 16 bits per channel, 48 bits total Black / White a colour name (only four files use this) Both are handled. Anything that is neither is a hard error: a palette that silently turns grey hides a broken file, and the palette is what the user sees first. Usage: genpalettes.py > palettes.c """ import os import re import sys NAMED = { "black": (0, 0, 0), "white": (255, 255, 255), } HEX48 = re.compile(r"^#([0-9a-fA-F]{12})$") def parse_line(text: str, path: str, lineno: int): tok = text.strip() if not tok: return None m = HEX48.match(tok) if m: v = m.group(1) return tuple(int(v[i:i + 4], 16) >> 8 for i in (0, 4, 8)) low = tok.lower() if low in NAMED: return NAMED[low] raise SystemExit("%s:%d: neither #RRRRGGGGBBBB nor a known name: %r" % (path, lineno, tok)) def load(path: str): out = [] with open(path, "r", encoding="utf-8") as fh: for lineno, line in enumerate(fh, 1): rgb = parse_line(line, path, lineno) if rgb is not None: out.append(rgb) if len(out) != 8: raise SystemExit("%s: %d colours, expected 8" % (path, len(out))) return out def main() -> int: if len(sys.argv) != 2: raise SystemExit(__doc__) src = sys.argv[1] names = sorted(f[:-3] for f in os.listdir(src) if f.endswith(".dp")) if not names: raise SystemExit("%s: no .dp files" % src) w = sys.stdout.write w("/*\n" " * Generated by tools/genpalettes.py from the CDE palette files.\n" " * Do not edit by hand: change the .dp source or the generator.\n" " */\n" "#include \"palettes.h\"\n\n" "const struct sp_palette sp_palettes[] = {\n") for name in names: cols = load(os.path.join(src, name + ".dp")) body = ", ".join("{0x%02x,0x%02x,0x%02x}" % c for c in cols) w("\t{ \"%s\", { %s } },\n" % (name, body)) w("};\n\n" "const size_t sp_palette_count = " "sizeof(sp_palettes) / sizeof(sp_palettes[0]);\n") return 0 if __name__ == "__main__": sys.exit(main())