base16-noble-builder/noble.py

29 lines
969 B
Python
Raw Normal View History

2022-06-23 12:28:22 -04:00
#!/usr/bin/env python3
import pystache, sys, yaml
def DieIf(cond, *message, **kwargs):
if cond:
print(sys.argv[0] + ":", *message, **kwargs)
sys.exit(1)
2022-06-23 12:28:22 -04:00
inData = yaml.safe_load(sys.stdin.read())
DieIf(len(sys.argv)==1, "no template specified")
2022-06-23 12:28:22 -04:00
template = open(sys.argv[1], 'r').read()
data = {'scheme-name': inData.get('scheme', 'Untitled'),
'scheme-author': inData.get('author', 'Anonymous'),
'scheme-slug': '<stdin>'}
for i in ["base%02X" % x for x in range(16)]:
DieIf(i not in inData, "missing color %s" % i)
2022-06-23 12:28:22 -04:00
colorHex = [inData[i][x*2:x*2+2] for x in range(3)]
color = [int(x, 16) for x in colorHex]
data[i + "-hex"] = inData[i]
data[i + "-hex-bgr"] = "".join(colorHex[::-1])
for c in range(3):
comp = "rgb"[c]
data[i + "-hex-" + comp] = colorHex[c]
data[i + "-rgb-" + comp] = color[c]
data[i + "-dec-" + comp] = color[c] / 255.0
print(pystache.render(template, data))