Author Topic: making a gradual change in colors with hexcodes [solved]  (Read 623 times)

how would I go about making a gradual change between two hex code colors
« Last Edit: June 28, 2013, 10:28:45 AM by swollow »

You could use RGB numbers (255 0 0 being red, 0 255 0 being green, for example) and convert the numbers into hexdecimal.

You could convert it to RGB, use the following function and convert it back.

function rgbGradient(%step, %c1, %c2)
{
   %r1 = getWord(%c1, 0);
   %g1 = getWord(%c1, 1);
   %b1 = getWord(%c1, 2);

   %r2 = getWord(%c2, 0);
   %g2 = getWord(%c2, 1);
   %b2 = getWord(%c2, 2);

   %r3 = %r1 + %step * (%r2 - %r1);
   %g3 = %g1 + %step * (%g2 - %g1);
   %b3 = %b1 + %step * (%b2 - %b1);

   return %r3 SPC %g3 SPC %b3;
}


function rgbGradient(%step, %c1, %c2)
{
   %r1 = getWord(%c1, 0);
   %g1 = getWord(%c1, 1);
   %b1 = getWord(%c1, 2);

   %r2 = getWord(%c2, 0);
   %g2 = getWord(%c2, 1);
   %b2 = getWord(%c2, 2);

   %r3 = %r1 + %step * (%r2 - %r1);
   %g3 = %g1 + %step * (%g2 - %g1);
   %b3 = %b1 + %step * (%b2 - %b1);

   return %r3 SPC %g3 SPC %b3;
}

thanks port but this only seems to work for black and white, what am I doing wrong?

Code: [Select]
function rgbToHex( %rgb )
{
        %r = _compToHex( 255 * getWord( %rgb, 0 ) );
        %g = _compToHex( 255 * getWord( %rgb, 1 ) );
        %b = _compToHex( 255 * getWord( %rgb, 2 ) );
 
        return %r @ %g @ %b;
}
function _compToHex( %comp )
{
        %left = mFloor( %comp / 16 );
        %comp = mFloor( %comp - %left * 16 );
 
        %left = getSubStr( "0123456789ABCDEF", %left, 1 );
        %comp = getSubStr( "0123456789ABCDEF", %comp, 1 );
 
        return %left @ %comp;
}
function rgbGradient(%step, %c1, %c2)
{
   %r1 = getWord(%c1, 0);
   %g1 = getWord(%c1, 1);
   %b1 = getWord(%c1, 2);

   %r2 = getWord(%c2, 0);
   %g2 = getWord(%c2, 1);
   %b2 = getWord(%c2, 2);

   %r3 = %r1 + %step * (%r2 - %r1);
   %g3 = %g1 + %step * (%g2 - %g1);
   %b3 = %b1 + %step * (%b2 - %b1);

   return %r3 SPC %g3 SPC %b3;
}
function changeColCenter(%cl,%step)
{
cancel(%cl.colChangeSched);
%col = rgbGradient(%step, "1 0 0", "0 0 1");

if(%step < 1)
%step += 0.1;
else
return;

%hexCo = rgbToHex(%col);
centerPrint(%cl,"<color:" @ %hexCo @ ">Color",1);
schedule(100,0,changeColCenter,%cl,%step);
}
« Last Edit: June 28, 2013, 10:29:29 AM by swollow »

Code: [Select]
%r = _compToHex( 255 * getWord( %rgb, 0 ) );
%g = _compToHex( 255 * getWord( %rgb, 0 ) );
%b = _compToHex( 255 * getWord( %rgb, 0 ) );