Search code examples
mathcolorsrgbcolor-codes

How can I convert an RGB input into a decimal color code?


Say I have an rgb color code with values 255 for red, 255 for green and 0 for blue. How can I get the decimal color code from those numbers, using only mathematical operations? So my setup, in pseudo-code, would look like:

int r = 255;
int g = 255;
int b = 0;

int result = /*decimal color code for yellow*/

Please help, I have spent ages trying to find an answer already and would love a simple quick answer :)


Solution

  • int result = (r * 256 * 256) + (g * 256) + b
    

    Or, if your language has a bit shift operator,

    int result = (r << 16) + (g << 8) + b