- Home /
Convert RGB to decimal
I need to know the values of the RGB color in decimal mode. Anyone knows the equation?(no script) Thanks.
Thanks for the answer, macfanpro. I´m using setcolor to change a button color, using decimal values. I need to restore the original color of this button when the mouse exit, but I´ve only the rgb value and I need to put this values in decimal too.
Answer by equalsequals · Jun 10, 2011 at 01:27 PM
If by decimal mode you mean a 4 dimensional float ranging between 0 and 1 for R, G, B and A values then:
RGB(A) is a 256-bit color, so by that logic then any channel can be 0 to 255. Knowing this, we can say a color is 255(r),127(g),25(b),255(a) you can do this:
Color color = new Color(r/255.0F,g/255.0F,b/255.0F,a/255.0F);
The equation is very basic math. 0 is 0 and 255 is 1, you simply take the number of bits in that channel and divide it by the maximum (255) to find it's normalized (decimal) value.
EDIT - Note: As Mike mentioned, don't divide by an int and expect to get a float. Always divide by a float to get decimal values.
Hope that helps
==
Just a small point - you want to divide by 255.0, not 255, otherwise you'll end up with all zeroes
Right - I obviously have not had my coffee yet. Had I ran it I would have noticed the implicit float-to-int problem. Thanks!
I would never have guess this is what RenatoB was asking. You're like some kind of noob-whisperer!
I work with a lot of UI designers, 3D artists and programmers. One thing you learn in a diplomatic role like Tech Artist is that there is a different name for everything across all trades, and your job is to understand them all. Typically UI designers know RGB as 255, and programmers know it as normalized data, so knowing this it was the logical assumption.
Answer by diOriginal · Aug 07, 2012 at 05:16 PM
You can use the following function.
http://docs.unity3d.com/Documentation/ScriptReference/Color32-operator_Color.html
exampel for red: private Color32 ColorRed = new Color32(255, 0, 0, 255);
Answer by Sylker · Dec 07, 2013 at 06:03 AM
Or you can use this:
http://www.corecoding.com/utilities/rgb-or-hex-to-float.php
and totally skip equations and scripts.
Answer by ckfinite · Jun 10, 2011 at 01:20 PM
Well, there is really no way to do what you say without script. What is decimal mode exactly? Do you want to get one of the r g or b values?
Answer by lst · Feb 26, 2016 at 08:19 PM
Also there is a web converter http://www.corecoding.com/utilities/rgb-or-hex-to-float.php
after can use directly
Color color = new Color(0,5f,0.5f,0.5f, 1f); // values from that converter enter here.
http://answers.unity3d.com/questions/128407/convert-rgb-to-decimal.html#answer-591472
Looks familiar doesn't it?
You posted the same link as Sylker posted over two years ago. So your answer didn't add anything.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102526im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
High precision numbers 2 Answers
C# public decimal variable not showing up in inspector 2 Answers
Convert RGB heightmap to greyscale 2 Answers
Round to decimals 2 Answers
Change InputField decimal mark 1 Answer