Web Colors in Scripting

We can do this:

renderer.material.SetColor("_Emission", Color.yellow);

But how can we use Web Safe Colors such as CC9933 in scripting? The choices with names, yellow, black, etc, are rather limited.
Thanks

You can create your own colours as shown on this page. For example:

renderer.material.SetColor("_Emission", Color(1.0, 0.8, 0.2));

The numbers should range from 0.0 to 1.0, rather than 0x00 to 0xFF.

If you’re more familiar with web-style hex colours, you might be able to do something like this instead:

Color(0xCC / 256.0, 0x99 / 256.0, 0x33 / 256.0);

Obviously, this isn’t very pretty, but you could write a function to make it easier to work with.

Thanks, Neil. I’ll work on that.

I know that you can specify hexadecimal numbers preceding with 0x, let’s say…

int a = 0xF

is equal to

int a = 15

But it throw me an error… I don’t know if the javascript for Unity doesn’t support it, had another syntax, or i’m wrong.

Creating a function to change an hexadecimal to an RGB value will be easy though

.ORG

My mistake :roll: syntax mistake :roll: …

Here is your function:

function Awake () 
{
	var white = HexToRGB (0xFFFFFF);
	Debug.Log ("white " + white);
	var gray = HexToRGB (0x888888);
	Debug.Log ("gray " + gray);
	var red = HexToRGB (0xFF0000);
	Debug.Log ("red " + red);
	var cian = HexToRGB (0x00FFFF);
	Debug.Log ("cian " + cian);
	var midpink = HexToRGB (0xFFBBCC);
	Debug.Log ("midpink " + midpink);
}

function HexToRGB (pColor : int) 
{
	var color : Color;
	
	color.r = ((pColor  0xFF0000) >> 16) / 255.0;
	color.g = ((pColor  0x00FF00) >> 8) / 255.0;
	color.b = (pColor  0x0000FF) / 255.0;
	color.a = 1.0;
	
	return color;
}

PD: You can add alpha as a second parameter if you wish…

.ORG

While we’re on the topic… “web safe colors” is a relic from an era when every computer in the world didn’t have 32 million colors. (ESPECIALLY in any computer capable of running Unity games)

Your web page is gonna look different on different computers, but it has nothing to do with web safe colors.

What you used above is C# syntax. In JavaScript it’s:

var a = 0xF

Edit: Woops! I saw you found out yourself… I better read the complete thread before posting next time…