Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  38] [ 3]  / answers: 1 / hits: 24628  / 13 Years ago, mon, december 12, 2011, 12:00:00

How do I convert a RGB colour value to just plain decimal?



So I have: RGB(255,255,255) is white

Its decimal equivalent is: 16777215



I have tried thinking it might just be:



var dec = r*g*b; // but this doesn't work


Although that doesn't work.



Anyone know of the algorithm/equation to convert from RGB (or rgba) to decimal?


More From » c++

 Answers
76

RGB integers are typically treated as three distinct bytes where the left-most (highest-order) byte is red, the middle byte is green and the right-most (lowest-order) byte is blue. You can retrieve the values of these individual bytes like this:



var c = 0xff03c0; // 16712640
var components = {
r: (c & 0xff0000) >> 16,
g: (c & 0x00ff00) >> 8,
b: (c & 0x0000ff)
};


You can re-create a color from its components by shifting the bytes back in:



c = (components.r << 16) + (components.g << 8) + (components.b);


In your case, simply substitute components.r (etc) with your actual variables.


[#88611] Friday, December 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
blaynetheoi

Total Points: 146
Total Questions: 116
Total Answers: 103

Location: India
Member since Thu, Apr 8, 2021
3 Years ago
;