Alpha set by tag in GUI.Label / GUI text behaves strange way

Hello everyone,

I’m having a strange issue. Let’s have this simple script, that creates a string that contains 256 letters “a” with different alphas, which are set by tag <color=rrggbbaa>text. You can see in script below that I convert numbers from 0 to 255 to two-digit hexadecimal that represents alpha of each “a” letter.

You would expect long string like “aaaaaaaaaaaaaa… (and so on)” where each “a” is less transparent than the previous one. But instead of that, I’m getting this:

while testing, I tried to save this string into a txt file and alphas seem to be okay there. Then I tried to write this on screen:

<color=#ffffff69>1</color> <color=#ffffff5A>2</color> <color=#ffffff3f>3</color>

you can see alpha for “1” is 69, for “2” it’s 5a and for “3” it’s 3f. Since 5a is between 3f and 69, you would expect transparency for “2” to be between “1”'s and “3”'s, but “2” has the fewest transparency.

Any ideas what’s going on? Because I’ve spent this whole weekend trying to figure this out and am really desparate now.

(btw - behaviour is the same when using GUI text instead of GUI.Label)

Thank you

Here’s my script:

string a;

void Start ()
{
	a = "";
	for (int i = 0; i < 256; i++)
		a += "<color=#ffffff" + ToTwoDigitHexa(i) + ">a</color>";
}

void OnGUI()
{
	GUI.Label(new Rect(0, 0, 1000, 500), a);
}

string ToTwoDigitHexa(int i)
{
	string s = i.ToString ("X");
	if (s.Length == 1)
		s = "0" + s;
	
	return s;
}

It is possible you have found a bug as it appears the color parameter can take rgb as capital letters and still work however the alpha hexadecimal number must be lower case for it to be correctly read.

Fortunately you can fix this very easily by doing:

return s.ToLower();

on line 21. Or change line 17 to:

string s = i.ToString ("x");

Scribe,

P.S.
rather than your if statement to check the length of s you can actually do:

string s = i.ToString("x2");

and it will automatically return a 2 digit number.

The changes:

string ToTwoDigitHexa(int i){
	string s = i.ToString("x2");
	return s;
}