Implicit function Color Vector4

I’m having some difficulty figuring out the syntax for converting a Vector4 to a Color.

In the documentation it states you can go to and from Vector4 to Color and back, but I can’t seem to figure it out.

Thanks,
~t

Implicit means that it converts to a color without any extra effort:

var b : Color = Vector4(0,1,0,1);

This sets b to full green.

Thanks!

Hmm, still a bit of a problem here…In the following code the ‘oneDfield[ ]’ Contains a list of Vector4’s

var c : Color = oneDfield[i]*255 ;
colorAr[i] = c;

I still get this error…

InvalidCastException: Cannot cast from source type to destination type.
eularianFluids_02_01.Con1DfieldToBitmap (System.Object width, System.Object length, System.Object oneDfield, System.Object typ, System.Object mVal)   (at Assets\Scripts\eularianFluids_02_01.js:79)
eularianFluids_02_01.INIT ()   (at Assets\Scripts\eularianFluids_02_01.js:165)
eularianFluids_02_01.Start ()   (at Assets\Scripts\eularianFluids_02_01.js:173)

I’m using this temporarily but it seems like it is allot of extra work.

				colorAr[i] = Color(oneDfield[i].x* 255, oneDfield[i].y* 255, oneDfield[i].z* 255, oneDfield[i].w* 255);

Perhaps the JS magic type conversion goes wrong here. I’m not sure, as I’m not a JS expert. Does removing the *255 fix it? It’s unlikely you want to use that multiplication anyway: Unity colors are defined with floats in the 0-1 range.

Yea it is strange…

This works:

				var c : Color = Vector4 (1,1,1,1); 
				colorAr[i] = c;

and so does:

colorAr[i] = Color(oneDfield[i].x, oneDfield[i].y, oneDfield[i].z, oneDfield[i].w);

BUT NOT:

var c : Color = oneDfield[i];
colorAr[i] = c;

OR:

colorAr[i] = oneDfield[i];

The last one results in this:

InvalidCastException: Cannot cast from source type to destination type.
System.Array.System.Collections.IList.set_Item (Int32 index, System.Object value) 
Boo.Lang.Runtime.RuntimeServices.SetArraySlice (System.Object target, System.Object[] args) [0x00000]
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000]
Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DispatcherFactory factory) [0x00000]
Boo.Lang.Runtime.RuntimeServices.SetSlice (System.Object target, System.String name, System.Object[] args) [0x00000]
eularianFluids_02_01.Con1DfieldToBitmap (System.Object width, System.Object length, System.Object oneDfield, System.Object typ, System.Object mVal)   (at Assets\Scripts\eularianFluids_02_01.js:81)
eularianFluids_02_01.INIT ()   (at Assets\Scripts\eularianFluids_02_01.js:167)
eularianFluids_02_01.Start ()   (at Assets\Scripts\eularianFluids_02_01.js:175)

It appears that a Vector4 in an array is not being cast properly, or the way I’m passing it to the function prevents implicit conversion.

How are your arrays defined? It seems that at least one of them is not typed (perhaps you’re using Array?).

Damn, figured it out thats for that last comment.

I was creating my Vector4 Arrays with ‘new Array’ then trying to implicitly convert a Vector4 within an Array to a Color in a Builtin Array.

I’ll post again if I can provide a simple example. For anyone else interested.

Cheers,
Tharyn