2
\$\begingroup\$

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.

\$\endgroup\$
8
  • \$\begingroup\$ 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? \$\endgroup\$
    – Kylotan
    Jul 23, 2012 at 12:20
  • \$\begingroup\$ 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. \$\endgroup\$
    – JesseTG
    Jul 23, 2012 at 16:52
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kylotan
    Jul 23, 2012 at 18:12
  • \$\begingroup\$ 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. \$\endgroup\$
    – JesseTG
    Jul 23, 2012 at 19:13
  • \$\begingroup\$ 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. \$\endgroup\$
    – Kylotan
    Jul 23, 2012 at 20:26

1 Answer 1

1
\$\begingroup\$

So apparently it's actually not that hard.

This site right here, though aimed at Web development, has some useful tips for software development in general. And I just so happened to find a series (five parts so far!) about image blending.

The tutorials are in Javascript, but that doesn't matter, the main feature here is the math (it's just algebra). You can ignore pretty much anything not highlighted in blue; these tips are good for blending images or colors. Just iterate over every pixel, and apply each blend pixel by pixel with the given formulas. Too many blending modes to describe here right now.

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.

There, now I don't have to rely on the PIL for one function.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .