@@ -0,0 +1,38 @@
|
||||
"""The product palette shared by build-time rasterization and both devices."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import pathlib
|
||||
import re
|
||||
|
||||
PALETTE_PATH = pathlib.Path(__file__).with_name('palette.json')
|
||||
COLORS = json.loads(PALETTE_PATH.read_text(encoding='utf-8'))
|
||||
|
||||
def rgb(name: str) -> tuple[int, int, int]:
|
||||
value = COLORS[name]
|
||||
if not isinstance(value, str) or not re.fullmatch(r'#[0-9a-fA-F]{6}', value):
|
||||
raise ValueError(f'invalid palette color: {name}')
|
||||
return tuple(int(value[index:index + 2], 16) for index in (1, 3, 5))
|
||||
|
||||
def generate_header(destination: pathlib.Path) -> None:
|
||||
lines = ['#pragma once', '', 'namespace vamp_style {', '']
|
||||
for name in COLORS:
|
||||
if not re.fullmatch(r'[a-z][a-z_]*', name):
|
||||
raise ValueError(f'invalid palette name: {name}')
|
||||
r, g, b = rgb(name)
|
||||
lines.append(f'constexpr auto {name} = vamp_style::color{{{r}, {g}, {b}, 255}};')
|
||||
lines.extend(['', '}', ''])
|
||||
content = '\n'.join(lines)
|
||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||
if not destination.exists() or destination.read_text() != content:
|
||||
destination.write_text(content, encoding='utf-8')
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--header', required=True, type=pathlib.Path)
|
||||
generate_header(parser.parse_args().header)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user