RGB values via script

From Unify Community Wiki
Jump to: navigation, search

The first time I saw the way you had to access RGB values using script it gave me pause. The manual states that each RGB values is represented by a number between 0 and 1. The little voice in my head said, "just go ahead and take the numbers and divide by 100. That will move the decimal over, and all will be well."

That did not work at all. I guess I got lucky and actually got a color the first time. Every other time after that I got black. Then I noticed that the manual states that Solid Black is represented as 0 and white is 1. That meant that the number that Unity uses as its component value(R or G or B) is the percentage that component number is to 255.

For example: if you take Light Blue R-102, G-153, B-255. What percentage of 255 is 102, what percentage of 255 is 153, and finally what percentage of 255 is 255. After you find the percentage all you have to is divide it by 100 to get it between 0 and 1. Voila! Correct RGB values via script.

UPDATE: Myran has been kind enough to come up with a much more efficient way of doing this. See his comment below for the JS. I hope this helps. Cheers rudeness

--- Actually, to map a value from 0-255 to 0-1 all you need to do is divide by 255.

function parseColorValues (incomingColorValue) {
    return incomingColorValue / 255.0;


Personal tools