Setting material.color, some colors won't take

I am creation a color selector for selecting the color you want an object to be by name, it is for kids so I want very specific color names and matching colors.

// —I created an enum full of colors—

public enum theColors
{
	black,
	blue,
	brown,
	green,
	red,
	purple,
	yellow,
	orange,
	grey,
	white
}

//---- And then I added a procedure for selecting the color ----

public Color GetColor(theColors aColor)
{
	switch (aColor) {
	case theColors.black:
		return new Color(0,0,0);
	case theColors.blue:
		return new Color(0,0,255);
	case theColors.brown:
		return new Color(165,42,42);	
	case theColors.green:
		return new Color(0,255,0);
	case theColors.red:
		return new Color(255,0,0);
	case theColors.purple:
		return new Color(160,32,240);
	case theColors.yellow:
		return new Color(255,255,0);
	case theColors.orange:
		return new Color(255,165,0);
	case theColors.grey:
		return new Color(165,165,165);
	case theColors.white:
		return new Color(255,255,255);
	default:
		return new Color(0,0,0);
	}
}

//---- Then when I wish to use the color, I simply apply it thus ----

renderer.material.color = GetColor(newColor);

The problem is that only some of the colors will apply, black, white, grey, blue, red, yellow, all work.

Brown and purple and orange do not. Why?

I don’t know why your grey color works, because new Color(165,165,165) is actually white. You see, the Color structure in Unity doesn’t take bytes, it takes floats between 0 and 1. See documentation:

http://unity3d.com/support/documentation/ScriptReference/Color.html

So if you write new Color(165,165,165), it should be capping the values to 1, producing 1,1,1 = white.

Try normalizing all those values to 0 - 1 and see if that doesn’t make it work.

FYI, the short answer to this question:

UnityEngine.Color uses 0-1 ranges for the 3 RGB values,

new Color(1,0,0); //Red

UnityEngine.Color32 uses 0-255 on all 4 channels (RGBA)

new Color32(255, 0, 0, 255); //Red
new Color32(255, 0, 0, 125); //Red, semi transparent (alpha channel reduced)

Hope that helps someone!

Not entirely sure if this will work but instead of using New Color(…), use New Color32(…)

Use This, (C#)

class Exemple : MonoBehaviour
    {
        public Color initialColor;

        public void Start()
        {
            initialColor = renderer.material.color;
        }
        public void Update()
        {

            renderer.material.color = initialColor;
            increment = 0;
        }

        public void OnEventX()
        {
            renderer.material.color = Color.red;
        }

    }

The variable initialCollor will make it be normalized