I have uint16_t color
and need to convert it into its RGB equivalent. The hex is set up so the first 5 bits represent red, next 6 for green, and last 5 for blue.
So far I have found something close to a solution but not quite due to truncation.
void hexToRGB(uint16_t hexValue)
{
int r = ((hexValue >> 11) & 0x1F); // Extract the 5 R bits
int g = ((hexValue >> 5) & 0x3F); // Extract the 6 G bits
int b = ((hexValue) & 0x1F); // Extract the 5 B bits
r = ((r * 255) / 31) - 4;
g = ((g * 255) / 63) - 2;
b = ((b * 255) / 31) - 4;
printf("r: %d, g: %d, b: %d\n",r, g, b);
}
int main()
{
//50712=0xC618
hexToRGB(50712);
return 0;
}
The example above yields r: 193, g: 192, b: 193
which should be r: 192, g: 192, b: 192
I have been using this question as reference, but I essentially need a backwards solution to what they are asking.