Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DB2014 · Feb 07, 2014 at 04:35 PM · javascriptcolorconvertcolor32

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);
 }    
Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Feb 07, 2014 at 05:07 PM 0
Share

I can't wrap my $$anonymous$$d around a human considering 0-255 to be easier than 0-1.

avatar image Jessy · Feb 07, 2014 at 05:09 PM 1
Share

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;
avatar image robertbu Jessy · Feb 07, 2014 at 05:40 PM 0
Share

@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.

avatar image Jessy Jessy · Feb 07, 2014 at 07:06 PM 0
Share

So, you can't cast in US? :-|

avatar image robertbu Jessy · Feb 07, 2014 at 08:10 PM 0
Share

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.

Show more comments
avatar image DB2014 · Feb 07, 2014 at 06:28 PM 0
Share

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?

avatar image Jessy · Feb 07, 2014 at 07:05 PM 0
Share

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?

avatar image DB2014 · Feb 08, 2014 at 01:43 PM 0
Share

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?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers

Turn seconds into minutes and seconds MM:SS 1 Answer

convert c# to JavaScript Problem 0 Answers

Assigning GUITexture through Javascript 1 Answer

Help converting C# to Javascript. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges