Author Topic: Simple RGBA to hex conversion?  (Read 3571 times)

I tried searching for this but couldn't find anything on it, does anyone have a readable RGBA to HEX script I can use on a string? ( %c = "r g b a"; )

try this lovely untested piece of psuedocode crap
*torquescript not included because my brain is in python land

Code: [Select]
function convertstuff(%c)
{
for i < getwordcount()
{
%tempcolor = getword(%i) // get color number thing in 0-1 range
%hexnum = %tempcolor * 256 // convert from 0-1 number range to 0-256 number range (may be faulty)
%hexacharacter = mFloor(%hexnum / 16)
%hexbcharacter = (%hexnum % 16)
// do fancy stuff to convert numbers
// 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// into hex. for loops and a string maybe?
// 0 1 2 3 4 5 6 7 8 9 a b c d e f
%outputhex = %hexacharacter @ %hexbcharacter;
%output = %output @ %outputhex;
}
return %output;
}

Code: [Select]
function convertToHex(%color)
{
%b = firstWord(%d);
%r = mFloor(%b / 16);
%b -= %r * 16;

if(%r < 0 || %b < 0)
{
%result[1] = "00";
}

else
{
%result[1] = getSubStr("0123456789ABCDEF", %r, 1) @ getSubStr("0123456789ABCDEF", %b, 1);
}

%b = getWord(%d, 1);
%r = mFloor(%b / 16);
%b -= %r * 16;

if(%r < 0 || %b < 0)
{
%result[2] = "00";
}

else
{
%result[2] = getSubStr("0123456789ABCDEF", %r, 1) @ getSubStr("0123456789ABCDEF", %b, 1);
}

%b = getWord(%d, 2);
%r = mFloor(%b / 16);
%b -= %r * 16;

if(%r < 0 || %b < 0)
{
%result[3] = "00";
}

else
{
%result[3] = getSubStr("0123456789ABCDEF", %r, 1) @ getSubStr("0123456789ABCDEF", %b, 1);
}

return %result[1] @ %result[2] @ %result[3];
}
Converts 0-255 to hex. Example: convertToHex("233 100 255");

Or you can use this to convert each word into base 16. That is what I did when I needed this.

Code: [Select]
-codeblobsnip-

You do know that there's no limit on functions, right? You also never defined %d - it's clearly supposed to be %color, but it's not.

Code: [Select]
function RBGToHex(%col)   //Converts a 0-255 RGB color to its hex equivalent
{
    for(%i=0;%i<3;%i++)
        %sub[%i] = ByteToHex(getWord(%col, %i));
    return %sub0 @ %sub1 @ %sub2;
}

function ByteToHex(%byte)   //Converts a byte into its hex equivalent
{
    %low = %byte % 16;
    %high = %byte - %low;
    return NybbleToChar(%high)@NybbleToChar(%low);
}

function NybbleToChar(%nybble)   //Converts a nybble into its hex equivalent
{ return getSubStr("0123456789ABCDEF", %nybble, 1); }

If your function is doing more than one thing, it's probably a sign that it needs to be broken into more than one function.

Credit to Port
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;
}

%hex = rgbToHex(getWords(%rgba,0,3));
« Last Edit: November 19, 2013, 03:23:34 PM by swollow »

By the way, to convert it back you can do something like this:

Code: [Select]
function hexToRGB( %hex )
{
   for(%i = 0; %i < 3; %i++)
      eval("%color[%i] = 0x" @ getSubStr(%hex, %i*2, 2) @ ";");
   return %color0 SPC %color1 SPC %color2;
}
« Last Edit: November 19, 2013, 03:56:33 PM by $trinick »

Why is everyone forgetting that he wants RGBA

Why is everyone forgetting that he wants RGBA
It's literally the same process with one more byte tacked on. The reason we're forgetting is because hex doesn't support an alpha channel in Blockland.


You could also use the getColorF or getColorI functions to convert a hex color code to RGB. They are both default :)

Quote
==> getColorI("FFFFFF");
"255 255 255 255";
==> getColorF("FFFFFF");
"1.000000 1.000000 1.000000 1.000000";

==> getColorI("1.000000 1.000000 1.000000 1.000000");
"255 255 255 255";
==> getColorF("255 255 255 255");
"1.000000 1.000000 1.000000 1.000000";
« Last Edit: November 19, 2013, 05:27:20 PM by Zelothix »

Excellent, I got the results I wanted.