00001
00002
00003
00004
00005
00006
00007
00008 def quantize(component):
00009 """Converts a color component in [0;255] to [0.31] (5-bit)."""
00010 return int( round(component/255.0*31) )
00011
00012
00013
00014 def convert_to_uint16(r,g,b):
00015
00016 base = 32768
00017 base = (base | r) | (g<<5) | (b<<10)
00018 return base
00019
00020
00021 ds_gamma_correction_factor = 2.3
00022
00023
00024
00025 def gamma_correct(component):
00026
00027
00028
00029
00030 return int( round( ( (component/255.0)**(1/ds_gamma_correction_factor) ) * 255.0 ) )
00031
00032
00033 r_range = 6
00034 g_range = 8
00035 b_range = 5
00036
00037
00038
00039
00040 color_key = ( 106, 76, 48 )
00041
00042 base_count= r_range*g_range*b_range
00043
00044 grey_range=8
00045
00046
00047 print "\nGenerating a master palette of %s base colors (for index in [1..%s]), with %s levels of red, %s of green and %s of blue. Index #0 is reserved for the colorkey, which is %s. The %s remaining index are used to add %s true grays and %s specific recurrent colors in target images.\n" % ( base_count, base_count, r_range, g_range, b_range, color_key, 256 - base_count -1, grey_range, 256 - base_count -1 - grey_range )
00048
00049
00050 base_palette = [ (
00051 int( round(255*i/ (r_range-1) )),
00052 int( round(255*j/ (g_range-1) )),
00053 int( round(255*k/ (b_range-1) )) )
00054 for i in range(r_range)
00055 for j in range(g_range)
00056 for k in range(b_range) ]
00057
00058 base_palette = [color_key] + base_palette
00059
00060
00061
00062
00063
00064
00065
00066 for grey in [ int( round(255*(g+1)/(grey_range+1)) ) for g in range(grey_range) ]:
00067 base_palette += [ (grey,grey,grey) ]
00068
00069
00070
00071
00072 base_palette.append( ( 6, 177, 220 ) )
00073 base_palette.append( ( 4, 147, 183 ) )
00074 base_palette.append( ( 28, 95, 139 ) )
00075
00076
00077 base_palette.append( ( 70, 59, 42 ) )
00078
00079
00080 base_palette.append( ( 238, 190, 141 ) )
00081 base_palette.append( ( 250, 205, 155 ) )
00082
00083
00084 base_palette.append( ( 255, 255, 111 ) )
00085
00086
00087
00088
00089 count=0
00090 for c in base_palette:
00091 print " + color index #%s: %s" % (count,c)
00092 count +=1
00093
00094 print "Palette has %s color index." % (len(base_palette))
00095
00096
00097 def cmp_colors(x,y):
00098 if x[0] > y[0]:
00099 return 1
00100 elif x[0] == y[0]:
00101 if x[1] > y[1]:
00102 return 1
00103 elif x[1] == y[1]:
00104 if x[2] > y[2]:
00105 return 1
00106 elif x[2] == y[2]:
00107 return 0
00108 else:
00109 return -1
00110 else:
00111 return -1
00112 else:
00113 return -1
00114
00115
00116 base_palette.sort( cmp_colors )
00117
00118
00119 original_palette_filename = "master-palette-original.rgb"
00120
00121 print " * Writing original master palette to '%s'." % (original_palette_filename)
00122 palette_file = open( original_palette_filename, 'w' )
00123
00124
00125
00126 for c in base_palette:
00127 palette_file.write( "%c" % (c[0]) )
00128 palette_file.write( "%c" % (c[1]) )
00129 palette_file.write( "%c" % (c[2]) )
00130
00131 palette_file.close()
00132
00133
00134
00135 gamma_corrected_palette_filename = "master-palette-gamma-corrected.rgb"
00136
00137 print " * Writing gamma-corrected master palette to '%s' (just for documentary purpose)." % (gamma_corrected_palette_filename)
00138 palette_file = open( gamma_corrected_palette_filename, 'w' )
00139
00140
00141 for c in base_palette:
00142 palette_file.write( "%c" % ( gamma_correct( c[0]) ) )
00143 palette_file.write( "%c" % ( gamma_correct( c[1]) ) )
00144 palette_file.write( "%c" % ( gamma_correct( c[2]) ) )
00145
00146 palette_file.close()
00147
00148
00149
00150
00151 quantized_palette_filename = "master-palette-quantized.rgb"
00152
00153 print " * Writing quantized master palette (non gamma-corrected) to '%s'." % (quantized_palette_filename)
00154 palette_file = open( quantized_palette_filename, 'w' )
00155
00156
00157
00158
00159 count=0
00160 for c in base_palette[1:]:
00161
00162
00163 palette_file.write( "%c" % ( int( 255.0 /31 * quantize(c[0]) ) ) )
00164 palette_file.write( "%c" % ( int( 255.0 /31 * quantize(c[1]) ) ) )
00165 palette_file.write( "%c" % ( int( 255.0 /31 * quantize(c[2]) ) ) )
00166 count +=1
00167
00168 palette_file.close()
00169
00170
00171
00172 final_palette_filename = "master-palette.pal"
00173
00174 print " * Writing final master palette (gamma-corrected, then quantized, then encoded for the DS) to '%s'." % (final_palette_filename)
00175 palette_file = open( final_palette_filename, 'w' )
00176
00177
00178
00179 for c in base_palette:
00180 converted_color = convert_to_uint16(
00181 quantize( gamma_correct( c[0]) ),
00182 quantize( gamma_correct( c[1]) ),
00183 quantize( gamma_correct( c[2]) ) )
00184 palette_file.write( "%c" % ( ( converted_color & 0xFF00 ) >> 8 ) )
00185 palette_file.write( "%c" % ( converted_color & 0x00FF ) )
00186
00187 palette_file.close()
00188
00189
00190
00191 print "Master palette successfully generated."
00192
00193 print "Use '%s' for color-reduction of assets, and '%s' as the actual DS palette." % ( quantized_palette_filename, final_palette_filename)
00194
00195