diff options
author | yenatch <yenatch@gmail.com> | 2014-11-01 21:11:49 -0700 |
---|---|---|
committer | yenatch <yenatch@gmail.com> | 2014-11-01 21:11:49 -0700 |
commit | ca42efaa0d6e0f53c1ce911ed9c206c646c8c7d9 (patch) | |
tree | c5a0b589f0e4507754b977f6068214c00ffcb3f2 /pokemontools/gfx.py | |
parent | 0dd6b20b2ff54e1b3bf15957fab77193c287fbea (diff) |
More lenient palette handling.
Truncate colors in a palette beyond the limit of 4.
Make palette sorting more accurate by taking the sum of the hues rather than by luminance. Ultimately, palette order is up to the whim of whoever was drawing in the tile editor at the time, but this is a reasonable estimation.
Diffstat (limited to 'pokemontools/gfx.py')
-rw-r--r-- | pokemontools/gfx.py | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/pokemontools/gfx.py b/pokemontools/gfx.py index 2bc39d6..e5cac7a 100644 --- a/pokemontools/gfx.py +++ b/pokemontools/gfx.py @@ -1394,33 +1394,34 @@ def png_to_2bpp(filein, **kwargs): 'g': line[px+1], 'b': line[px+2], 'a': line[px+3], } - newline += [color] if color not in palette: - palette += [color] + if len(palette) < 4: + palette += [color] + else: + # TODO Find the nearest match + print 'WARNING: %s: Color %s truncated to' % (filein, color), + color = sorted(palette, key=lambda x: sum(x.values()))[0] + print color + newline += [color] image += [newline] - assert len(palette) <= 4, 'Palette should be 4 colors, is really %d' % len(palette) + assert len(palette) <= 4, '%s: palette should be 4 colors, is really %d: %s' % (filein, len(palette), palette) # Pad out smaller palettes with greyscale colors hues = { - 'white': { 'r': 0xff, 'g': 0xff, 'b': 0xff, 'a': 0xff }, 'black': { 'r': 0x00, 'g': 0x00, 'b': 0x00, 'a': 0xff }, 'grey': { 'r': 0x55, 'g': 0x55, 'b': 0x55, 'a': 0xff }, 'gray': { 'r': 0xaa, 'g': 0xaa, 'b': 0xaa, 'a': 0xff }, + 'white': { 'r': 0xff, 'g': 0xff, 'b': 0xff, 'a': 0xff }, } - for hue in hues.values(): + preference = 'white', 'black', 'grey', 'gray' + for hue in map(hues.get, preference): if len(palette) >= 4: break if hue not in palette: palette += [hue] - # Sort palettes by luminance - def luminance(color): - rough = { 'r': 4.7, - 'g': 1.4, - 'b': 13.8, } - return sum(color[key] * rough[key] for key in rough.keys()) - palette.sort(key=luminance) + palette.sort(key=lambda x: sum(x.values())) # Game Boy palette order palette.reverse() |