No-bull base16 builder
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
969B

  1. #!/usr/bin/env python3
  2. import pystache, sys, yaml
  3. def DieIf(cond, *message, **kwargs):
  4. if cond:
  5. print(sys.argv[0] + ":", *message, **kwargs)
  6. sys.exit(1)
  7. inData = yaml.safe_load(sys.stdin.read())
  8. DieIf(len(sys.argv)==1, "no template specified")
  9. template = open(sys.argv[1], 'r').read()
  10. data = {'scheme-name': inData.get('scheme', 'Untitled'),
  11. 'scheme-author': inData.get('author', 'Anonymous'),
  12. 'scheme-slug': '<stdin>'}
  13. for i in ["base%02X" % x for x in range(16)]:
  14. DieIf(i not in inData, "missing color %s" % i)
  15. colorHex = [inData[i][x*2:x*2+2] for x in range(3)]
  16. color = [int(x, 16) for x in colorHex]
  17. data[i + "-hex"] = inData[i]
  18. data[i + "-hex-bgr"] = "".join(colorHex[::-1])
  19. for c in range(3):
  20. comp = "rgb"[c]
  21. data[i + "-hex-" + comp] = colorHex[c]
  22. data[i + "-rgb-" + comp] = color[c]
  23. data[i + "-dec-" + comp] = color[c] / 255.0
  24. print(pystache.render(template, data))