Coloring an Image

I am building a particle system and I’m trying to use a small image (32x32 pixels) for a particle. I have an image in png format which is a white circle which tapers to black surrounded by transparent pixels filing up the image’s rectangular area. What I want to do is color this image with a color of my choosing at runtime.

I need a method which combines the image data with the new color as follows:

  1. If a pixel in the image has an alpha value of zero (it is transparent) don’t do anything
  2. If a pixel has a non zero alpha value, merge the pixel’s value with that of the color such that a pure white pixel in the image would
    be replaced with the color. Gray scale values between white and black would be mixed with the color.
  3. After the particle image has been processed, it will need to be blit to a screen and show up as a ball of my specified color (which fades to black) with a background that is transparent.

I hope this is clear

Is this possible to do in SDL somehow or do I need to write my own code to do this?

Thanks

Assuming you’re using the renderers (and thereby textures), it looks
like what you want is SDL_SetTextureColorMod, though this happens the
moment you draw the texture (i.e. SDL_RenderCopy or SDL_RenderCopyEx),
rather than modifying the texture itself. Probably more useful that
way anyway.

For surfaces there seems to be SDL_SetSurfaceColorMod to do something similar.

2013/6/29, craiglindley :> I am building a particle system and I’m trying to use a small image (32x32

pixels) for a particle. I have an image in png format which is a white
circle which tapers to black surrounded by transparent pixels filing up the
image’s rectangular area. What I want to do is color this image with a color
of my choosing at runtime.

I need a method which combines the image data with the new color as
follows:

  1. If a pixel in the image has an alpha value of zero (it is transparent)
    don’t do anything
  2. If a pixel has a non zero alpha value, merge the pixel’s value with that
    of the color such that a pure white pixel in the image would
    be replaced with the color. Gray scale values between white and black would
    be mixed with the color.
  3. After the particle image has been processed, it will need to be blit to a
    screen and show up as a ball of my specified color (which fades to black)
    with a background that is transparent.

I hope this is clear

Is this possible to do in SDL somehow or do I need to write my own code to
do this?

Thanks

I probably should have mentioned I’m using SDL 1.2. Though having this functionality in SDL 2.0 might be enough to make me upgrade.

In SDL 1.2 you’re pretty much expected to do everything on your own
(it was a very barebones wrapper around the OS APIs), there’s a reason
why the docs tell you how to do a putpixel function. There’s some
minimal support for blitting surfaces, but nothing else.

2013/6/29, craiglindley :> I probably should have mentioned I’m using SDL 1.2. Though having this

functionality in SDL 2.0 might be enough to make me upgrade.