Hue and Coloring?

Hi All :slight_smile:

Does anyone know a good way to alter the hue and color balance of images? Sort
of like the way you can in The Gimp? I want to “palette swap” some of the bad
guys and items in my game.

Anyone got some code to do this?

Ta,

Steve :slight_smile:

Hi, I’m not sure if you know the theory or not, so
just in case, here it is in a nutshell. You want to
convert from RGB to HSV (Hue-Saturation-Value).
Then to change the color, you would change the hue
value which is represented as a 360 degree wheel.
Then you would convert back to RGB and feed it into
your SDL commands. Generally, each draw frame you
would move a little around the wheel to gradually
rotate the color (i.e. palette swap).

As for source code, I found some code from a Google
cache from a page called “Color Conversion
Algorithms”. I couldn’t access the actual page though.
I’m not sure what the license is. I’ve never used this
code myself so I don’t know if it actually is correct,
though it looks okay at a glance (the MIN,MAX macros
bother me from a namespace pollution stance though).

http://216.239.37.104/search?q=cache:Xpusnh-T7DgJ:www.cs.rit.edu/~ncs/color/t_convert.html+color+rgb+hsv+&hl=en&ie=UTF-8

Good luck,
Eric> From: Stephen Sweeney

Date: Thu, 14 Aug 2003 13:17:14 +0100
Hi All :slight_smile:

Does anyone know a good way to alter the hue and
color balance of images?=
Sort=20
of like the way you can in The Gimp? I want to
"palette swap" some of the=
bad=20
guys and items in my game.

Anyone got some code to do this?

Ta,

Steve :slight_smile:


Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software

Hi All :slight_smile:

That works really really well :slight_smile:

I can change the hue, saturation and lightness (or value) of 256 color
images… Effectively I now get a palette swap that looks really groovy!

I’ve got a screenshot of the Blob Wars sprites up on my page,

http://www.parallelrealities.co.uk/blobWars.php

(image is entitled Palette Swapped Sprites)

if anyone is interested and if you’d like the code I used (it’s really just a
modification of the code on the page Eric suggested) then give me a shout :slight_smile:

Cheers, Eric :slight_smile:

Steve :)On Monday 18 Aug 2003 15:14, Eric Wing wrote:

Hi, I’m not sure if you know the theory or not, so
just in case, here it is in a nutshell. You want to
convert from RGB to HSV (Hue-Saturation-Value).
Then to change the color, you would change the hue
value which is represented as a 360 degree wheel.
Then you would convert back to RGB and feed it into
your SDL commands. Generally, each draw frame you
would move a little around the wheel to gradually
rotate the color (i.e. palette swap).

As for source code, I found some code from a Google
cache from a page called “Color Conversion
Algorithms”. I couldn’t access the actual page though.
I’m not sure what the license is. I’ve never used this
code myself so I don’t know if it actually is correct,
though it looks okay at a glance (the MIN,MAX macros
bother me from a namespace pollution stance though).

http://216.239.37.104/search?q=cache:Xpusnh-T7DgJ:www.cs.rit.edu/~ncs/color
/t_convert.html+color+rgb+hsv+&hl=en&ie=UTF-8

Good luck,
Eric

I used some code from Trophy to do this once http://trophy.sourceforge.net/index.php3?body=developers_corner

void RGBtoHSV(const int* rgb, int* hsv)
{
    int max = rgb[0];        // maximum RGB component
    int whatmax = 0;    // rgb[0]=>0, rgb[1]=>1, rgb[2]=>2
    if( rgb[1] > max )
    {
        max = rgb[1];
        whatmax = 1;
    }
    if( rgb[2] > max )
    {
        max = rgb[2];
        whatmax = 2;
    }

    int min = rgb[0];        // find minimum value
    if( rgb[1] < min ) min = rgb[1];
    if( rgb[2] < min ) min = rgb[2];
    
    int delta = max-min;
    hsv[2] = max;
    if (max == 0)
        hsv[1] = 0;
    else
        hsv[1] = (510*delta+max)/(2*max);

    if( hsv[1] == 0 )
    {
        hsv[0] = -1;        // undefined hue
    }
    else
    {
        switch( whatmax )
        {
        case 0:        // red is max component
            if( rgb[1] >= rgb[2] )
                hsv[0] = (120*(rgb[1]-rgb[2])+delta)/(2*delta);
            else
                hsv[0] = (120*(rgb[1]-rgb[2]+delta)+delta)/(2*delta) + 300;
            break;
        case 1:        // green is max component
            if( rgb[2] > rgb[0] )
                hsv[0] = 120 + (120*(rgb[2]-rgb[0])+delta)/(2*delta);
            else
                hsv[0] = 60 + (120*(rgb[2]-rgb[0]+delta)+delta)/(2*delta);
            break;
        case 2:        // blue is max component
            if ( rgb[0] > rgb[1] )
                hsv[0] = 240 + (120*(rgb[0]-rgb[1])+delta)/(2*delta);
            else
                hsv[0] = 180 + (120*(rgb[0]-rgb[1]+delta)+delta)/(2*delta);
            break;
        }
    }
}

void HSVtoRGB(const int* hsv, int* rgb)
{
    rgb[0]=rgb[1]=rgb[2] = hsv[2];

    if( hsv[1] == 0 || hsv[0] == -1 )
    {
        ;
    }
    else
    {
        if( hsv[0] >= 360 )
        {
            hsv[0] %= 360;
        }
        if( hsv[1] >= 255 )
        {
            hsv[1] %= 255;
        }
        if( hsv[2] >= 255 )
        {
            hsv[2] %= 255;
        }
        int f = hsv[0]%60;
        hsv[0] /= 60;
        int p = (2*hsv[2]*(255-hsv[1])+255)/510;
        if( (hsv[0] & 1) != 0 )
        {
            int q = (2*hsv[2]*(15300-hsv[1]*f)+15300)/30600;
            switch( hsv[0] )
            {
            case 1:
                rgb[0]=(int)q;
                rgb[1]=(int)hsv[2];
                rgb[2]=(int)p;
                break;
            case 3:
                rgb[0]=(int)p;
                rgb[1]=(int)q;
                rgb[2]=(int)hsv[2];
                break;
            case 5:
                rgb[0]=(int)hsv[2];
                rgb[1]=(int)p;
                rgb[2]=(int)q;
                break;
            }
        }
        else
        {
            int t = (2*hsv[2]*(15300-(hsv[1]*(60-f)))+15300)/30600;
            switch( hsv[0] )
            {
            case 0:
                rgb[0]=(int)hsv[2];
                rgb[1]=(int)t;
                rgb[2]=(int)p;
                break;
            case 2:
                rgb[0]=(int)p;
                rgb[1]=(int)hsv[2];
                rgb[2]=(int)t;
                break;
            case 4:
                rgb[0]=(int)t;
                rgb[1]=(int)p;
                rgb[2]=(int)hsv[2];
                break;
            }
        }
    }
}

Stephen Sweeney wrote:> Hi All :slight_smile:

Does anyone know a good way to alter the hue and color balance of images? Sort
of like the way you can in The Gimp? I want to “palette swap” some of the bad
guys and items in my game.

Anyone got some code to do this?

Ta,

Steve :slight_smile: