Author Topic: Find the closest color in the server colorset given R G B  (Read 3007 times)

I have an RGB (decimal 0 -> 1) color that I want to find the closest match for on the server colorset.
How do?

There's actually such a function in NARG's Terragen:

Code: [Select]
function findclosestcolor(%x) //input four numbers between 0 and 1
{
for(%a=0; %a<64; %a++)
{
%match = mabs(getword(getcoloridtable(%a),0) - getword(%x,0)) + mabs(getword(getcoloridtable(%a),1) - getword(%x,1)) + mabs(getword(getcoloridtable(%a),2) - getword(%x,2)) + mabs(getword(getcoloridtable(%a),3) - getword(%x,3))*4;

if(%match < %bestmatch || %bestmatch $= "")
{
%bestmatch = %match;
%bestid = %a;
}
}
return %bestid; //trans may be messed up if you input a nearly-but-not-quite opaque color
}

^client-sided

getclosestpaintcolor(vectorscale(%rgb,255))

^server-sided
« Last Edit: January 19, 2013, 06:40:12 PM by Electrk »

for(%i=0;%i<4;%i++)
    %color = %color SPC getWord(%rgb, %i) * 255;
%colorID = getClosestPaintColor(trim(%color));

Woops I forgot that a color has 4 values
Fixed
« Last Edit: January 19, 2013, 06:41:57 PM by Zeblote »

I forget the original author.

Code: [Select]
function getClosestPaintColor(%rgba)
{
%prevdist = 100000;
%colorMatch = 0;
for(%i = 0; %i < 64; %i++)
{
%color = getColorIDTable(%i);
if(vectorDist(%rgba,getWords(%color,0,2)) < %prevdist && getWord(%rgba,3) - getWord(%color,3) < 0.3 && getWord(%rgba,3) - getWord(%color,3) > -0.3)
{
%prevdist = vectorDist(%rgba,%color);
%colormatch = %i;
}
}
return %colormatch;
}

I forget the original author.

Code: [Select]
function getClosestPaintColor(%rgba)
{
%prevdist = 100000;
%colorMatch = 0;
for(%i = 0; %i < 64; %i++)
{
%color = getColorIDTable(%i);
if(vectorDist(%rgba,getWords(%color,0,2)) < %prevdist && getWord(%rgba,3) - getWord(%color,3) < 0.3 && getWord(%rgba,3) - getWord(%color,3) > -0.3)
{
%prevdist = vectorDist(%rgba,%color);
%colormatch = %i;
}
}
return %colormatch;
}
What, I thought this was a ddefault function?

No, not as far as I know.

I forget the original author.

Code: [Select]
function getClosestPaintColor(%rgba)
{
%prevdist = 100000;
%colorMatch = 0;
for(%i = 0; %i < 64; %i++)
{
%color = getColorIDTable(%i);
if(vectorDist(%rgba,getWords(%color,0,2)) < %prevdist && getWord(%rgba,3) - getWord(%color,3) < 0.3 && getWord(%rgba,3) - getWord(%color,3) > -0.3)
{
%prevdist = vectorDist(%rgba,%color);
%colormatch = %i;
}
}
return %colormatch;
}
I believe Space Guy was the one who created that.

What, I thought this was a ddefault function?
Nope. A ton of mods use it though.

I believe Space Guy was the one who created that.

No, I asked him if I was free to use it a while ago and he said he got it from somewhere else. I think he said it was originally from the duplicator.

The duplicator does employ that function to find the closest match to white.