- Home /
Convert Color32 to Color
I can visualise in my head a colour in 0-255 ranges as in Color32. I find it difficult to understand 0.x range of colours as in Color. At moment I get this error Assets/GroundColour.js(12,9): BCE0043: Unexpected token: xxxColor32Color.
I would just like to be able to convert Color32 values to Color with out mess at begin of var. Is that even possiable?
var xxxColorColor32 : Color = Color32(198,255,154,255);
#pragma strict
private var varColor32Color;
function Start ()
{
//gameObject.renderer.material.color = Color(0.777, 08, 0.604);// Change ground colour to dark green.
//gameObject.renderer.material.color = Color32(198,255,154,255);// Change ground colour to dark green.
// This is how you convert Color and Color32 values between each other
var xxxColor32Color : Color32 = Color(0.777, 08, 0.604);// This would return Value of (198,255,154,255)
var xxxColorColor32 : Color = Color32(198,255,154,255);// This would return Value of (0.777, 08, 0.604)
xxxColor32Color : Color32 = Color(0.777, 08, 0.604);
}
I can't wrap my $$anonymous$$d around a human considering 0-255 to be easier than 0-1.
Color and Color32 implement implicit operators between each other. Use casting notation if you don't like the static typing colon.
var color32 = (Color32)color;
var color = (Color)color32;
Or better yet, switch to C#:
Color color = color32;
Color32 color32 = color;
@Jessy - you cannot cast this way in Javascript. That is this will not compile in Javascript:
var color32 = (Color32)Color.red;
Since there is an implicit conversion you can do:
var color32 : Color32 = Color.red;
...which is what he is already doing.
You can use the 'as' operator to cast objects referred by reference (which Color and Color32 are not). You cannot do a C#-style cast.
Alright so correct me if I am wrong for each of this values
Color32 (1,2,3) same as Color (0.004,0.008,0.012) Color32 (253,254,255) same as Color (0.992,0.996,1.000)
If you find that easyier, then all can say is "Well done". It just doesn't make sense to me doing the values by 0.004. Or am I just missing something here?
You're missing that a human being isn't supposed to have to think about the colors in 8 bits. We use 8 bits because it's fast for computers, not because it's useful conceptually. You can't even represent medium grey! Do you not use floating anywhere?
Thanks, I didn't even think about that issue. It just looks like I've got try and learn a new way to represent numbers in my head. I am thinking the easy way for me to look at it this as it is a percentage of 255 levels. Or do you have any better idea of how I might be able to think about this? How do you think about the numbers your self?
Answer by robertbu · Feb 07, 2014 at 05:41 PM
In your last statement, your problem is your ': Color32'. That is, you can do:
var xxxColor32Color : Color32 = Color(0.777, 08, 0.604);// This would return Value of (198,255,154,255)
var xxxColorColor32 : Color = Color32(198,255,154,255);// This would return Value of (0.777, 08, 0.604)
xxxColor32Color = Color(0.777, 08, 0.604);
This works because you've already declared xxxColor32Color as a Color32.