If you care only about what color has the lowest luminescence, without caring whether or not its saturation is close to zero (that is, "is it grayscale?"), then the best way to find the color with the lowest visible brightness (that is, the darkest to the human eye) is with this function:
function GetDarkestColor()
{
if($Darkest !$= "")
return $Darkest;
%lowestV = 999999;
for(%i=0;%i<64;%i++)
{
%c = getColorIDTable(%i);
if(getWord(%c, 3) != 1)
continue;
%r = 0.2126 * getWord(%c, 0);
%g = 0.7152 * getWord(%c, 1);
%b = 0.0722 * getWord(%c, 2);
%v = %r + %g + %b;
if(%v < %lowestV)
{
%lowestV = %v;
%darkest = %i;
}
}
return $Darkest = %darkest;
}