Author Topic: Ensure a brick's color is changed to darkest color, despite color set?  (Read 711 times)

With the dumping of a brick, I found out the possible value to make a brick black is a single integer. In the standard colorset's case, this integer is 16. But that number only has to do with placement. It has nothing to do value or darkness, just position.

So my question is, how do I make a brick the darkest color possible, regardless of any alternate colorset the add-on user might have? Is it even possible?
« Last Edit: May 26, 2015, 02:43:24 PM by Narkro555 »

Best thing I can think of is to loop through all the colors, and determine which is them has the lowest sum of R+G+B, or convert to HSV and determine which has lowest V

Edit: after some searching on Google I found this:  

brightness  =  sqrt( .299 R2 + .587 G2 + .114 B2 )
So loop through all colors, calculate brightness for each according to that formula, then choose color with lowest brightness

idk how you'd handle alpha though
« Last Edit: May 26, 2015, 03:27:42 PM by Headcrab Zombie »

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:

Code: [Select]
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;
}

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:

Code: [Select]
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;
}
This is fantastic, thank you.