GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-09-08 18:12:50

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Color to Hex problems [SOLVED]

Okay... maybe I've just been staring at this for too long but I think I'm to the point where even simple answers are eluding me.  Anyway, I need help.

Here's what I'm trying to accomplish.  I want to create a script that will accept a start color, an end color, and a number of steps.  Then, when the script is used, the color will change smoothly at the desired rate.  So if I put in Red (255,0,0 || FF0000) and I want it to fade to blue (0,0,255 || 0000FF)  each RGB value will transition towards it's end goal at a constant rate.  (so you'll have a purple color as they both hit 128 on their way in the opposite direction).

Anyway, so here's my main problem right now.  I'm using color_to_hex() to convert my start and end colors into hex values.  The problem is these values are not being padded properly.  So while Red is properly interpreted as FF0000, blue is being interpreted as FF (which is in turn interpreted as FF0000 by Game Maker);  So I need a way to properly pad each of the values... I think.  (I'm a little burned out.  Been working on this for hours.)

Anyway, let me throw some code up here...  I warn you though... it may make some of you more experienced guys absolutely nauseous.   It's really messy right now as I'm at the point where I'm just throwing stuff around to get things to work.  (It's currently set up as an object rather than a script as I experimented with core functionality.)

In the Create Event I have this:

ExpandstartColor  = c_blue;
endColor    = c_red;
newColor    = -1;
numSteps    = 128;
currentStep = 0;

Then, in the Draw Event I have this:

Expandif (newColor != endColor)
{
    //Debug Text Output
    draw_set_color(startColor);
    draw_text(10,5,color_to_hex(startColor));
    draw_set_color(endColor);
    draw_text(200,5,color_to_hex(endColor));

    //Convert the start and end colors into Hex Strings
    startColor = color_to_hex(startColor);
    endColor   = color_to_hex(endColor);

    //Ripping each of the colors for the StartColor into seperate dec values.
    startColorRed  = real(hex_to_dec(string_copy(startColor,1,2)));
    startColorGreen = real(hex_to_dec(string_copy(startColor,3,2)));
    startColorBlue   = real(hex_to_dec(string_copy(startColor,5,2)));
    
    //Ripping each of the colors for the End Color into seperate dec values.
    endColorRed  = real(hex_to_dec(string_copy(endColor,1,2)));
    endColorGreen = real(hex_to_dec(string_copy(endColor,3,2)));
    endColorBlue   = real(hex_to_dec(string_copy(endColor,5,2)));

    //Find the difference in the Blue Values
    blueDiff = endColorBlue - startColorBlue;
    //Find the difference in the Green Values
    greenDiff = endColorGreen - startColorGreen;
    //Find the difference in the Red Values
    redDiff = endColorRed - startColorRed;    

    //Modify each value by the difference divided by the number of steps the transition should take.    
    newColorBlue  = startColorBlue + (currentStep*(blueDiff/numSteps));
    newColorGreen = startColorGreen + (currentStep*(greenDiff/numSteps));
    newColorRed   = startColorRed + (currentStep*(redDiff/numSteps));    
        
    //Taking the new color values and converting them back to hex.
    newColorBlue  = dec_to_hex(newColorBlue);
    newColorGreen = dec_to_hex(newColorGreen);
    newColorRed   = dec_to_hex(newColorRed);

    //Building the string to represent the new color;
    newColor = "RRGGBB";
    newColor = string_replace(newColor,"BB",newColorBlue)
    newColor = string_replace(newColor,"GG",newColorGreen)
    newColor = string_replace(newColor,"RR",newColorRed)
    
    draw_set_color(hex_to_color(newColor));
    draw_text(10,175,newColor);

    newColor = hex_to_color(newColor);


    
    //Set the start and end colors back from hex for the next loop.
    startColor = hex_to_color(startColor);
    endColor   = hex_to_color(endColor);

    if (currentStep = numSteps)
    {
        newColor = endColor;
    }


    currentStep+=1;
}

draw_set_color(c_black);
draw_text(10,200,currentStep);

//    draw_set_color(hex_to_color(newColor));
//    draw_text(10,175,hex_to_color(newColor));    

        //Even More Debug Text
    draw_set_color(c_blue);
    draw_text(200,25,"Dec Blue:  "+string(endColorBlue));
    draw_set_color(c_green);
    draw_text(200,50,"Dec Green: "+string(endColorGreen));
    draw_set_color(c_red);
    draw_text(200,75,"Dec Red:   "+string(endColorRed));
        //More Debug Text
    draw_set_color(c_blue);
    draw_text(10,25,"Dec Blue:  "+string(startColorBlue));
    draw_set_color(c_green);
    draw_text(10,50,"Dec Green: "+string(startColorGreen));
    draw_set_color(c_red);
    draw_text(10,75,"Dec Red:   "+string(startColorRed));
        //Even More Debug Text
    draw_set_color(c_blue);
    draw_text(100,100,"Hex Blue:  "+string(newColorBlue));
    draw_set_color(c_green);
    draw_text(100,125,"Hex Green: "+string(newColorGreen));
    draw_set_color(c_red);
    draw_text(100,150,"Hex Red:   "+string(newColorRed));

Again, I'm terribly sorry about the mess...

Anyway, I know there's probably a better way to do this but I have been searching feverishly for a script made by someone more experienced and have yet to find one. 

So, if you look at this and think "My God... this is such a mess, I don't even know where to begin helping him..." rather than click away, you could post a suggestion for how I could start over using a different (hopefully more simple) method.  I'm very open to starting over so I can learn more and get something cleaner.

...or, if you happen to know of a script that does just what I'm trying to do out there somewhere on the internets, pointing me in the right direction via a link would also be appreciated.

Thanks for your time.

Last edited by Pixelated_Pope (2009-09-09 13:59:21)

Offline

#2 2009-09-09 12:58:55

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Color to Hex problems [SOLVED]

I think you can do this quite a bit more simply, although I'm not 100% sure I know what this is supposed to do. You say:

Here's what I'm trying to accomplish.  I want to create a script that will accept a start color, an end color, and a number of steps.  Then, when the script is used, the color will change smoothly at the desired rate.  So if I put in Red (255,0,0 || FF0000) and I want it to fade to blue (0,0,255 || 0000FF)  each RGB value will transition towards it's end goal at a constant rate.  (so you'll have a purple color as they both hit 128 on their way in the opposite direction).

But the code you've posted is so complex, I wonder if there is something I'm missing.

It's not clear what the return is supposed to be. If it's just a new color then built-in GM function merge_color is all you really need.

merge_color(col1,col2,amount) Returns a merged color of col1 and col2. The merging is determined by amount. A value of 0 corresponds to col1, a value of 1 to col2, and values in between to merged values.

ExpandnewColor = merge_color(startColor, endColor, currentStep/numSteps));

If it's supposed to return the color as an RRGGBB string, pass newColor through color_to_hex.

http://www.gmlscripts.com/script/color_to_hex

ExpandnewHexColor = color_to_hex(newColor);

And if you ever do need string padding scripts:

http://www.gmlscripts.com/script/string_lpad
http://www.gmlscripts.com/script/string_rpad


Abusing forum power since 1986.

Offline

#3 2009-09-09 13:36:30

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Color to Hex problems [SOLVED]

Okay... I apologize, I probably am not being very clear.

Here's a graphical description of what I'm trying to accomplish.
Color%20Slide.jpg

So you give the script two colors, a total number of steps, and I suppose you would need to give the script the current step you are on.
The script looks at the start and end colors, and based on the position between the two colors on the timeline (that would be the current step) returns a color value... preferably in dec rather than hex.

Merge Color would consistently give me the color in the middle... but with no animation; no way to transition smoothly from red to purple to blue with all the hues in between.

Does that clear anything up?

Last edited by Pixelated_Pope (2009-09-09 13:41:44)

Offline

#4 2009-09-09 13:51:51

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Color to Hex problems [SOLVED]

Pixelated_Pope wrote:

Merge Color would consistently give me the color in the middle... but with no animation; no way to transition smoothly from red to purple to blue with all the hues in between.

Not at all. That's only true if (argument2 == 0.5).

So you give the script two colors, a total number of steps, and I suppose you would need to give the script the current step you are on.

That's exactly what the code I posted does. Rewritten as a script:

Expand// color_transition(startColor, endColor, currentStep, numSteps)
return merge_color(argument0, argument1, argument2 / argument3);

Abusing forum power since 1986.

Offline

#5 2009-09-09 13:53:44

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Color to Hex problems [SOLVED]

Ha.  Wow.  Okay, then.  I can't believe I wasted so much time trying to do it myself.

Thanks, xot.  Very helpful as always!

Offline

#6 2009-09-09 14:49:01

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: Color to Hex problems [SOLVED]

No problem. Glad to help.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB