[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Surface palette



Lee Harr wrote:
What makes a Surface have a palette?

I was thinking that using set_palette might be an interesting way of
changing the color of a sprite (instead of creating multiple images)
any 8bit image has a palette. the best way to get an 8bit image is to load one from disk. if you have a predefined palette you want to apply to any image, you can get SDL to do it with the 'convert' command.

my24bitsurface.convert(surfacewithpalette)

i highly recommend using this technique, it is very easy to create alternate sprites. i use this quite a bit in solarwolf. for example, all the different colored boxes load the graphics like this...

for i in range(15):
img = gfx.load_raw('box%02d.gif'%i)
boximages.append(img.convert())
pal = img.get_palette()
newpal = [(g,g,b) for (r,g,b) in pal]
img.set_palette(newpal)
yboximages.append(img.convert())
newpal = [(g,b,b) for (r,g,b) in pal]
img.set_palette(newpal)
rboximages.append(img.convert())

the images on disk are green, but you can sort of see as solarwolf progressed different colored boxes appeared. this also creates a yellow and red set of animated boxes.

of course, this is only swapping color channels to create the different images, you could also easily multiply, shift, or do just about anything you want to the palette.