conventionschedule-android/nightThemeGenerator/exportColors.py

54 lines
1.5 KiB
Python

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import json
import sys
from pathlib import Path
import re
from typing import Tuple
import colorsys
import xml.etree.ElementTree as ET
def parse_theme(tree: ET.ElementTree) -> dict[str, str]:
colors = {color.attrib['name']: (color.text or '').strip()
for color in tree.findall('color[@name]')}
changed_colors = True
while changed_colors:
changed_colors = False
for k, v in colors.copy().items():
if v.startswith('@color/'):
colors[k] = colors[v[7:]]
changed_colors = True
for k, v in colors.copy().items():
if len(v) == 9:
v = f'#{v[3:]}{v[1:3]}'
elif len(v) != 7:
raise Exception(v)
colors[k] = v.upper()
return colors
def export_themes(exported_path: Path, day_path: Path, night_path: Path):
day = parse_theme(ET.parse(day_path))
night = parse_theme(ET.parse(night_path))
day_final = {**night, **day}
nigth_final = {**day, **night}
exported_path.write_text(json.dumps(dict(
day=day_final,
night=nigth_final
), indent=4), encoding='utf-8')
def main():
if len(sys.argv) != 3:
print(
f'Usage:\n {sys.argv[0]} <path/to/day/colors.xml> <path/to/night/colors.xml>', file=sys.stderr)
else:
export_themes(Path(__file__).parent.joinpath(
'exported.json'), Path(sys.argv[1]), Path(sys.argv[2]))
if __name__ == '__main__':
main()