Converting a surface to another color

Hello,
I’m trying to write a simple 2D-program with SDL (I do not want to use OpenGL).
It is almost ready, but at the moment I paint the marbles of the players with
filledCircleColor. I have painted a circle with gimp with a gradient from red
(bottom) to white (top) so it looks better. I can load the image to the program,
but I need more colors and I do not want create an extra file for each color. I
need also blue, green etc. marbles. Does anyone know how to change the color in
a loaded surface? In the far future, it should also be possible to let the users
switch their colors, so it would not be possible to give a picture of each
possible color with the package.
Thanks
Viktor

What if you had a gradient function that created rectangles, then used a
circular alpha mask?On 12/31/06, Viktor Dick wrote:

Hello,
I’m trying to write a simple 2D-program with SDL (I do not want to use
OpenGL).
It is almost ready, but at the moment I paint the marbles of the players
with
filledCircleColor. I have painted a circle with gimp with a gradient from
red
(bottom) to white (top) so it looks better. I can load the image to the
program,
but I need more colors and I do not want create an extra file for each
color. I
need also blue, green etc. marbles. Does anyone know how to change the
color in
a loaded surface? In the far future, it should also be possible to let the
users
switch their colors, so it would not be possible to give a picture of each
possible color with the package.
Thanks
Viktor


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc

Thanks. If I understand rightly, the gradient function would put the
lines with lineColor or something like that. Is there someone that could
give me some code how to apply an alpa mask? I’m not familiar with
transparency under SDL and I did not found any tutorial explaining this
well enough.
Thanks.

Josh Matthews schrieb:> What if you had a gradient function that created rectangles, then used a

circular alpha mask?

On 12/31/06, Viktor Dick < @Viktor_Dick mailto:Viktor_Dick> wrote:

Hello,
I'm trying to write a simple 2D-program with SDL (I do not want to
use OpenGL).
It is almost ready, but at the moment I paint the marbles of the
players with
filledCircleColor. I have painted a circle with gimp with a gradient
from red
(bottom) to white (top) so it looks better. I can load the image to
the program,
but I need more colors and I do not want create an extra file for
each color. I
need also blue, green etc. marbles. Does anyone know how to change
the color in
a loaded surface? In the far future, it should also be possible to
let the users
switch their colors, so it would not be possible to give a picture
of each
possible color with the package.
Thanks
Viktor


_______________________________________________
SDL mailing list
SDL at libsdl.org <mailto:SDL at libsdl.org>
http://www.libsdl.org/mailman/listinfo/sdl


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc



SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

If you want a kind of cheezy solution, you can try my code for migrating the color of a surface. It would change the white too, but you could edit the code to prevent that, I guess. It’s a strictly pre-rendering function as well (slow). If you want the link, just ask. Otherwise, that alpha mask idea sounds good.

Hello,I’m trying to write a simple 2D-program with SDL (I do not want to use OpenGL). It is almost ready, but at the moment I paint the marbles of the players withfilledCircleColor. I have painted a circle with gimp with a gradient from red(bottom) to white (top) so it looks better. I can load the image to the program, but I need more colors and I do not want create an extra file for each color. Ineed also blue, green etc. marbles. Does anyone know how to change the color ina loaded surface? In the far future, it should also be possible to let the users switch their colors, so it would not be possible to give a picture of eachpossible color with the package.ThanksViktor_______________________________________________SDL mailing listSDL at libsdl.orghttp://www.libsdl.org/mailman/listinfo/sdl-- Cheers,JoshPGP: http://revvy.box43.net/Josh_Matthews.ascDate: Sun, 31 Dec 2006 16:24:22 -0500From: mrlachatte at gmail.comTo: sdl at libsdl.orgSubject: Re: [SDL] Converting a surface to another colorWhat if you had a gradient function that created rectangles, then used a circular alpha mask?
On 12/31/06, Viktor Dick < dickviktor at gmx.net> wrote:


Try amazing new 3D maps
http://maps.live.com/?wip=51

Your best bet would probably be creating a default image in the GIMP with an
alpha mask, then load that with SDL_Image and overwrite the pixel data.
Alternatively, you could avoid the alpha mask and just use a color key by
creating an image with an inverted circle filled with pink. In SDL, make a
surface, fill it with your gradient, then copy the pink pixels into your
surface from your image, set the surface’s color key and voila!On 12/31/06, Viktor Dick wrote:

Thanks. If I understand rightly, the gradient function would put the
lines with lineColor or something like that. Is there someone that could
give me some code how to apply an alpa mask? I’m not familiar with
transparency under SDL and I did not found any tutorial explaining this
well enough.
Thanks.

Josh Matthews schrieb:

What if you had a gradient function that created rectangles, then used a
circular alpha mask?

On 12/31/06, Viktor Dick < dickviktor at gmx.net <mailto:dickviktor at gmx.net>> wrote:

Hello,
I'm trying to write a simple 2D-program with SDL (I do not want to
use OpenGL).
It is almost ready, but at the moment I paint the marbles of the
players with
filledCircleColor. I have painted a circle with gimp with a gradient
from red
(bottom) to white (top) so it looks better. I can load the image to
the program,
but I need more colors and I do not want create an extra file for
each color. I
need also blue, green etc. marbles. Does anyone know how to change
the color in
a loaded surface? In the far future, it should also be possible to
let the users
switch their colors, so it would not be possible to give a picture
of each
possible color with the package.
Thanks
Viktor


_______________________________________________
SDL mailing list
SDL at libsdl.org <mailto:SDL at libsdl.org>
http://www.libsdl.org/mailman/listinfo/sdl


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc



SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc

[…]

Does anyone know how to change the color in a loaded surface?

The best way to properly “recolor” images is probably to convert from
RGB to HSV, “rotate” H (Hue) and then convert back to RGB.
Explanation and code:
http://en.wikipedia.org/wiki/HSV_color_space
http://www.cs.rit.edu/~ncs/color/t_convert.html

Or, just render the marbles from scratch…? Probably easier, and you
get high quality scaling and various other parameters as a more or
less free bonus. Oh, and you don’t need any image files at all. :wink:

Here’s some code from an old test program:On Sunday 31 December 2006 21:10, Viktor Dick wrote:

static SDL_Surface *create_sprite(int size, int aa, int trans)
{
int x, y;
SDL_Surface *s = SDL_CreateRGBSurface(SDL_SWSURFACE,
size, size, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff000000, 0x00ff0000, 0x0000ff00,
0x000000ff);
#else
0x000000ff, 0x0000ff00, 0x00ff0000,
0xff000000);
#endif
if(!s)
return NULL;

    for(y = 0; y < size; ++y)
            for(x = 0; x < size; ++x)
            {
                    unsigned char *p = s->pixels +
				y * s->pitch + x * 4;
                    float dx = x - size * 0.5;
                    float dy = y - size * 0.5;
                    float d = sqrt(dx*dx + dy*dy);
                    if(d > size * 0.5)
                    {
                            p[0] = 0;
                            p[1] = 0;
                            p[2] = 0;
                            p[3] = 0;
                    }
                    else
                    {
                            int v = (int)(255.0 *
					(1.0 - d / size * 2.0));
                            p[0] = v;
                            p[1] = v;
                            p[2] = v;
                            if(aa)
                            {
                                    if(v < 16)
                                            p[3] = v << 4;
                                    else
                                            p[3] = 255;
                            }
                            else
                            {
                                    if(v < 8)
                                            p[3] = 0;
                                    else
                                            p[3] = 255;
                            }
                            if(trans)
                                    p[3] /= 3;
                    }
            }
    return s;

}

Since this one renders a gray ball, you can modulate it by any RGB
color you want by scaling ‘v’ before it’s assigned to p[0], p[1] and
p[2].

Another version, that does some funky color gradients - although this
one’s in EEL rather than C, but I guess you can figure out the maths
anyway (same algorithm as the C code above):

import “SDL”, “math”;

export function render(size)
{
local s = Surface[0, size, size, 32]; // Default masks
local r = size / 2;
for local y = 0, size - 1
{
local dy = (y - size / 2) / r;
for local x = 0, size - 1
{
local dx = (x - size / 2) / r;
local d = sqrt(dxdx + dydy);
if ceil(d * r) == r
local a = 255 * (r - d * r);
else if d > 1
a = 0;
else
a = 255;
local c = MapColor(s, 255 - x * 255 / r,
255 - y * 255 / r,
d * 200,
a);
Plot(s, c, x, y);
}
}
return s;
}

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --’

Thanks @ all who helped! The following code works now:----------
SDL_Surface* create_surface(int size)
{
SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, size, size, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
#else
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
#endif
SDL_Surface* result = NULL;
if( tmp ) {
result = SDL_DisplayFormat( tmp );
SDL_FreeSurface( tmp );
};
if (!result) cerr << SDL_GetError();
return result;
};

SDL_Surface* create_marble(int d, Uint8 r, Uint8 g, Uint8 b, bool
top2bottom)
//diameter, red, green, blue. If top2bottom is true, the gradient will
go from top(full given color) to bottom (white).
{
SDL_Surface* gradient = create_surface(d);
if (top2bottom)
for (int i=0; i<d; i++) hlineRGBA(gradient, 0, d, i,
r+(0xff-r)*i/d,g+(0xff-g)*i/d, b+(0xff-b)*i/d, 0xff);
else
for (int i=0; i<d; i++) hlineRGBA(gradient, 0, d, d-i,
r+(0xff-r)*i/d,g+(0xff-g)*i/d, b+(0xff-b)i/d, 0xff);
SDL_Surface
tmp = create_surface(d);
filledCircleRGBA(tmp, d/2, d/2, d/2-1, 0, 1, 0, 0xff);
SDL_SetColorKey(tmp, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(tmp->format, 0, 1, 0));
drawsurface(tmp,gradient);
SDL_FreeSurface(tmp);
SDL_SetColorKey(gradient, SDL_SRCCOLORKEY | SDL_RLEACCEL,
SDL_MapRGB(gradient->format, 0,0,0));
return gradient;
};

Josh Matthews schrieb:

Your best bet would probably be creating a default image in the GIMP
with an alpha mask, then load that with SDL_Image and overwrite the
pixel data. Alternatively, you could avoid the alpha mask and just use
a color key by creating an image with an inverted circle filled with
pink. In SDL, make a surface, fill it with your gradient, then copy the
pink pixels into your surface from your image, set the surface’s color
key and voila!

On 12/31/06, Viktor Dick <@Viktor_Dick mailto:Viktor_Dick> wrote:

Thanks. If I understand rightly, the gradient function would put the
lines with lineColor or something like that. Is there someone that could
give me some code how to apply an alpa mask? I'm not familiar with
transparency under SDL and I did not found any tutorial explaining this
well enough.
Thanks.

Josh Matthews schrieb:
> What if you had a gradient function that created rectangles, then
used a
> circular alpha mask?
>
> On 12/31/06, *Viktor Dick* < @Viktor_Dick <mailto:@Viktor_Dick> <mailto:@Viktor_Dick <mailto:@Viktor_Dick>>> wrote:
>
>     Hello,
>     I'm trying to write a simple 2D-program with SDL (I do not want to
>     use OpenGL).
>     It is almost ready, but at the moment I paint the marbles of the
>     players with
>     filledCircleColor. I have painted a circle with gimp with a
gradient
>     from red
>     (bottom) to white (top) so it looks better. I can load the
image to
>     the program,
>     but I need more colors and I do not want create an extra file for
>     each color. I
>     need also blue, green etc. marbles. Does anyone know how to change
>     the color in
>     a loaded surface? In the far future, it should also be possible to
>     let the users
>     switch their colors, so it would not be possible to give a
picture
>     of each
>     possible color with the package.
>     Thanks
>     Viktor
>
>
>     _______________________________________________
>     SDL mailing list
>     SDL at libsdl.org <mailto:SDL at libsdl.org> <mailto:SDL at libsdl.org
<mailto:SDL at libsdl.org>>
>     http://www.libsdl.org/mailman/listinfo/sdl
<http://www.libsdl.org/mailman/listinfo/sdl>
>
>
>
>
> --
> Cheers,
> Josh
>
> PGP: http://revvy.box43.net/Josh_Matthews.asc
>
>
>
------------------------------------------------------------------------

>
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org <mailto:SDL at libsdl.org>
> http://www.libsdl.org/mailman/listinfo/sdl
<http://www.libsdl.org/mailman/listinfo/sdl>


_______________________________________________
SDL mailing list
SDL at libsdl.org <mailto:SDL at libsdl.org>
http://www.libsdl.org/mailman/listinfo/sdl
<http://www.libsdl.org/mailman/listinfo/sdl>


Cheers,
Josh

PGP: http://revvy.box43.net/Josh_Matthews.asc



SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl