Skip to content

Instantly share code, notes, and snippets.

@josephclaytonhansen
Last active August 21, 2022 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephclaytonhansen/46601bbc4839bc6d11b84dc02abf1a26 to your computer and use it in GitHub Desktop.
Save josephclaytonhansen/46601bbc4839bc6d11b84dc02abf1a26 to your computer and use it in GitHub Desktop.
import math
def HexFileToPixeloramaPalette(file, name):
name = '"' + name + '"'
first = """[gd_resource type="Resource" load_steps=15 format=2]
[ext_resource path="res://src/Palette/PaletteColor.gd" type="Script" id=1]
[ext_resource path="res://src/Palette/Palette.gd" type="Script" id=2]
"""
second_to_last = """
[resource]
resource_name = """
third_to_last = """
script = ExtResource( 2 )
name = """
fourth_to_last = """
comment = " "
"""
fifth_to_last = """
width = 4
height = """
al = []
with open(file, "r") as f:
lines = f.readlines()
n = len(lines)
i = 0
for line in lines:
try:
i += 1
line1 = '[sub_resource type="Resource" id='+str(i)+']'
line2 = 'script = ExtResource( 1 )'
h = line.lstrip('#')
line3 = 'color = Color {}'.format( tuple(round(int(h[i:i+2], 16) / 255.0, 6) for i in (0, 2, 4)) )
line3 = line3[:len(line3)-1] + ", 1 )"
line4 = 'index = '+str(i - 1)
itr = (line1,line2,line3,line4)
res = "\n".join(itr)
al.append(res)
if i != n:
al.append("\n\n")
except Exception as e:
print(e)
new_filename = file.rstrip(".hex") + ".tres"
with open(new_filename, "w") as f:
f.writelines(first)
f.writelines("\n")
f.writelines(al)
f.writelines(second_to_last)
f.writelines(name)
f.writelines(third_to_last)
f.writelines(name)
f.writelines(fourth_to_last)
height = math.ceil(n/4)
f.writelines(fifth_to_last)
f.writelines(str(height))
f.write("\n\n")
f.writelines("colors = {")
for x in range(0,n):
f.writelines(str(x)+": SubResource( "+ str(x+1)+"),\n")
f.writelines("}")
max = n
f.write("colors_max = "+str(max))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment