palettegen.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import sys
  2. # define palette(s) here
  3. palettes = {
  4. 'blues': [0x15234b, 0x29366f, 0x304ead, 0x3b5dc9, 0x4485c9, 0x41a6f6, 0x75c4f5, 0xbeeff7, 0x3c208a],
  5. }
  6. sweetie16 = [0x1a1c2c, 0x5d275d, 0xb13e53, 0xef7d57, 0xffcd75, 0xa7f070, 0x38b764, 0x257179, 0x29366f, 0x3b5dc9, 0x41a6f6, 0x73eff7, 0xf4f4f4, 0x94b0c2, 0x566c86, 0x333c57]
  7. # mostly for JASC output
  8. def intToR_G_B(intValue):
  9. r = (intValue >> 16) & 0xff
  10. g = (intValue >> 8) & 0xff
  11. b = intValue & 0xff
  12. return "%s %s %s" % (r, g, b)
  13. # for Aseprite or whatever
  14. def outputJasc():
  15. for paletteIndex in palettes:
  16. paletteString = "JASC-PAL\n0100\n%s\n" % len(palettes[paletteIndex])
  17. for color in palettes[paletteIndex]:
  18. paletteString += intToR_G_B(color) + "\n"
  19. filename = "%s.pal" % paletteIndex
  20. fileOut = open(filename, "w+")
  21. fileOut.write(paletteString)
  22. fileOut.close()
  23. # only replaces entries from 4 onward by default
  24. def outputLua(includeDecoder=False):
  25. paletteString = "palettes = {\n"
  26. for paletteIndex in palettes:
  27. paletteString += "\t%s = {" % paletteIndex
  28. for color in palettes[paletteIndex]:
  29. paletteString += hex(color) + ","
  30. paletteString += "},\n"
  31. paletteString += "}"
  32. if includeDecoder:
  33. paletteString += "\n"
  34. paletteString += "function palset(entry, val)\n\tpoke(0x3fc0+(entry*3), val//65536)\n"
  35. paletteString += "\tpoke(0x3fc1+(entry*3), (val//256)%256)\n\tpoke(0x3fc2+(entry*3), val%256)\n"
  36. paletteString += "end\n\n"
  37. paletteString += "function swap_palette(pal_name)\n\tfor i = 4,#palettes[pal_name]+3 do\n"
  38. paletteString += "\t\tpalset(i, palettes[pal_name][i-3])\n\tend\nend\n"
  39. filename = "palettes.lua"
  40. fileOut = open(filename, "w+")
  41. fileOut.write(paletteString)
  42. fileOut.close()
  43. # output can be pasted into the TIC-80 sprite editor
  44. # can only output a single palette, practically
  45. # also, i am replacing really specific entries here - be aware
  46. def outputTic(paletteName):
  47. paletteString = ""
  48. paletteCounter = 0
  49. for i in range(0, 16):
  50. if paletteCounter < 3 or paletteCounter > 11:
  51. paletteString += hex(sweetie16[paletteCounter]).replace('0x', '')
  52. else:
  53. color = palettes[paletteName][i - 3]
  54. paletteString += hex(color).replace('0x', '')
  55. paletteCounter += 1
  56. filename = "tic-%s.txt" % paletteName
  57. fileOut = open(filename, "w+")
  58. fileOut.write(paletteString)
  59. fileOut.close()
  60. # parse CLI arguments and take action
  61. if len(sys.argv) > 1:
  62. outFormat = sys.argv[1]
  63. print("Outputting %s" % outFormat)
  64. if outFormat == "jasc":
  65. outputJasc()
  66. if outFormat == "lua":
  67. outputLua(True)
  68. if outFormat == "tic":
  69. outputTic(sys.argv[2])
  70. else:
  71. print("Incorrect number of arguments.")
  72. print("Usage: palettegen.py jasc|lua|tic [palette]")