Anki/qt/tools/extract_sass_vars.py
Matthias Metelka 6c48dbcc7f Implement color palette using Sass maps
I hand-picked the gray tones, the other colors are from the Tailwind CSS v3 palette.

Significant changes:
- light theme is brighter
- dark theme is darker
- borders are softer

I also deleted some platform- and night-mode-specific code.
2022-08-16 14:54:12 +02:00

89 lines
2.1 KiB
Python

#!/usr/bin/env python3
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import json
import re
import sys
# bazel genrule "srcs"
colors_scss = sys.argv[1]
vars_scss = sys.argv[2]
# bazel genrule "outs"
colors_py = sys.argv[3]
props_py = sys.argv[4]
palette = {}
colors = {}
props = {}
color = ""
for line in open(colors_scss):
line = line.strip()
if not line:
continue
if m := re.match(r"^([a-z]+): \($", line):
color = m.group(1)
palette[color] = {}
elif m := re.match(r"(\d): (.+),$", line):
palette[color][m.group(1)] = m.group(2)
# TODO: recursive extraction of arbitrarily nested Sass maps?
reached_colors = False
for line in open(vars_scss):
line = line.strip()
if not line:
continue
if line == "themes: (":
reached_colors = True
continue
if reached_colors:
line = re.sub(
r"get\(\$color, (.+), (\d)\)",
lambda m: palette[m.group(1)][m.group(2)],
line,
)
if m := re.match(r"^(.+): ([^\(]+),$", line):
var = m.group(1)
val = m.group(2)
if reached_colors:
colors.setdefault(var, []).append(val)
else:
props.setdefault(var, []).append(val)
copyright_notice = """\
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
"""
with open(colors_py, "w") as buf:
buf.write(copyright_notice)
buf.write("# this file is auto-generated from _vars.scss and _colors.scss\n")
for color, (day, night) in colors.items():
color = color.replace("-", "_").upper()
buf.write(f'{color} = ("{day}", "{night}")\n')
with open(props_py, "w") as buf:
buf.write(copyright_notice)
buf.write("# this file is auto-generated from _vars.scss\n")
for prop, val in props.items():
day = val[0]
night = val[1] if len(val) > 1 else day
prop = prop.replace("-", "_").upper()
buf.write(f'{prop} = ("{day}", "{night}")\n')