# Blending colors on Surfaces in PyGame?

So I'm making a game in Python and PyGame. I have aliens and blocks that each adopt one of several colors; rather than make several copies of them in different colors, I plan on making the sprite once in white, and recoloring individual aliens and blocks.

Problem is, with PyGame, I only know how to use PixelArrays to replace specific colors with other colors (e.g. pure white with pure red, or (255, 255, 255) with (25, 34, 255)). The sprites aren't going to be super complex, but they will be shaded.

I could use the Python Imaging Library, but I'd kind of like to minimize dependencies. If this is the best way then so be it.

What I'd like to know is this; how can I use color-blending in PyGame? More than anything I'm concerned about color-blending algorithms (that ignore certain desired colors, e.g. black for outlines). Alpha blending is not of concern.

• You're going to have to be more specific: what's wrong with the colour replacement method you mention? Is it that you need to be able to change a range of colours to some other range? Will you know the exact range you need to change? (IF so, why not do it offline?) Does it have to be done at runtime (and if so, why?) And why do you call it blending when there doesn't appear to be 2 colours blended between? – Kylotan Jul 23 '12 at 12:20
• The mention I describe only turns one color to another. 0-255 is fine. It doesn't HAVE to be at runtime, but I don't want a spritesheet with five copies of the same sprite. As for calling it blending? Well, I've actually come across an answer, which I'm about to post. – JesseTG Jul 23 '12 at 16:52
• If all you want to do is change some values to other values then you don't need to know about Photoshop blend modes, so it's a bit confusing. Normally people do this sort of thing via hue shifts. – Kylotan Jul 23 '12 at 18:12
• I don't care about Photoshop. These are blending modes/formulas that are useful all across computer graphics and image processing. I don't think we're quite on the same page. – JesseTG Jul 23 '12 at 19:13
• They are indeed useful, but the problem you stated doesn't require any of them and it's not clear how they apply. I'm not trying to be awkward, just trying to make your question clearer, because many people have solved similar problems without talking about blending. – Kylotan Jul 23 '12 at 20:26

As for PyGame? First create a PixelArray of a Surface so you can manipulate individual pixels. Then use Surface.map_rgb to get the integer that represents the colors to change or blend, apply the math, and throw it back into the PixelArray. Remember to delete the PixelArray when done, or else you can't blit your image.