Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 16, 2015 at 10:39 PM by Dave-Carlile for the following reason:

Question is off-topic or not relevant

avatar image
1
Question by SwiftLee · Oct 16, 2015 at 10:38 PM · colorissueconvertcolorsconverting

RGB Colors as Float Values Converter

Hello, all!

I've developed a really simple implementation to fix your color conversion issues between Unity's float point RGB values and the regular 0 - 255 RGB values. I did the math, and it was very simple to convert if you know how to utilize dimensional analysis. So, assuming you know 255 is the max RGB value, you can write an equation to convert it! Here's what I did:

RBG Value / 255 = Unity float value

So here's the method I wrote to make it easy for your to do this, you could also create a class with a constructor to do the same thing!

  Color RGBColor(float r, float g, float b)
     {
         if (r > 255)
             r = 255f;
 
         if (g > 255)
             g = 255f;
 
         if (b > 255)
             b = 255f;
 
         r /= 255f;
         g /= 255f;
         b /= 255f;
 
         return new Color(r, g, b);
     }

If I wanted to use this method to convert my values, it is quite simple! Just do:

 Color c = RGBColor(255, 255, 255);

This would return a value of: Color(1.0f, 1.0f, 1.0f); Which is plain white!

Implementing it using a class:

 public class RGBColor : MonoBehaviour
 {
 
     public Color getRGBColor { get; set; }
 
     public RGBColor(float r, float g, float b)
     {
         Color c;
 
         if (r > 255)
             r = 255f;
 
         if (g > 255)
             g = 255f;
 
         if (b > 255)
             b = 255f;
 
         r /= 255f;
         g /= 255f;
         b /= 255f;
 
         c = new Color(r, g, b);
 
         this.getRGBColor = c;
         
     }
 }

Then, if you wanted to use it, you would do:

 RGBColor col = new RGBColor(255, 255, 255);
 Color c = col.getRGBColor;
 yourGameObject.color = c;
Comment
Add comment · Show 1
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 Dave-Carlile · Oct 16, 2015 at 10:39 PM 0
Share

There's the Color32 struct for this. No need to roll your own. Also, Unity Answers is for questions about Unity. Discussions and code contributions are better done in the Unity forums.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Legacy animation system cannot animate colors? 3 Answers

Convert Color32 to Color 1 Answer

Decal Shader, Change The Decal Color Separately 0 Answers

How many different levels of r g b and a can unity create in a Color? 2 Answers

mesh.colors doesnt seem to work at the moment 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