- Home /
Question is off-topic or not relevant
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;
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.