Skip to content

Instantly share code, notes, and snippets.

@aarqon
Created January 14, 2017 04:43
Show Gist options
  • Select an option

  • Save aarqon/a7fef244b0dd44a8b951efd1a5cbc0cc to your computer and use it in GitHub Desktop.

Select an option

Save aarqon/a7fef244b0dd44a8b951efd1a5cbc0cc to your computer and use it in GitHub Desktop.
A Python script used to generate the prettier BGR332 palette
import math
import colorsys
def scale(mult,step):
value = (mult)*(255/step)
if value % 1 > 0.5:
return math.ceil(value)
else:
return int(value)
rows = 24
columns = 0
colors = []
color_bins = [[] for _ in range(rows)]
for r in range(8):
for g in range(8):
for b in range(4):
#print(scale(r,7), scale(g,7), scale(b,3))
colors.append((scale(r,7),scale(g,7),scale(b,3)))
for each in colors:
(h, s, v) = colorsys.rgb_to_hsv((each[0]/255),(each[1]/255),(each[2]/255))
h_bin = int(h*rows)
color_bins[h_bin].append((h,s,v))
#print((h,s,v), "put in Bin", h_bin+1)
for bin in color_bins:
if len(bin) > columns:
columns = len(bin)
sorted(bin, key=lambda color: color[2]) #Sorts each bin by v
# Time to try to make the gpl file!
file = open("8bit-binned.gpl",'w')
file.write("""GIMP Palette
Name: 8-bit Binned
""")
file.write("Columns: "+str(columns)+'\n')
print("columns:", columns)
for bin in color_bins:
for each in bin:
(r,g,b) = colorsys.hsv_to_rgb(each[0],each[1],each[2])
r = str(int(r*255))
g = str(int(g*255))
b = str(int(b*255))
file.write(' '.join((r,g,b,'-\n')))
file.write("0 0 0 -\n"*(columns-len(bin)))
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment