Reputation: 11754
I'm storing a 3 value RGB representation into an 16bit integer by bit shifting it based off an answer from here (Convert RGB values to Integer). It seems to work for the g
and b
values, but r
is always returned as 0.
My code is:
uint16_t rgb = ((r&0x0ff) << 16) | ((g&0x0ff) << 8) | (b&0x0ff);
qDebug() << "wrote rgb: " << rgb;
qDebug() << "wrote r: " << r << " g: " << g << " b: " << b;
qDebug() << "unshifted r: " << ((rgb >> 16) & 0x0ff) << " g: " << ((rgb >> 8) & 0x0ff) << " b: " << (rgb & 0x0ff);
Upvotes: 2
Views: 3524
Reputation: 490098
If you need to store RGB in a 16-bit integer, you can only store the colors with limited resolution. Typically you use 5 bits each for red and blue, and 6 bits for green.
This was once fairly common -- while it does reduce color resolution, the result remains fairly usable for many purposes (business graphics and such, but not decent photo editing or anything like that where color accuracy matters).
Upvotes: 5
Reputation: 360592
You're using a 16bit int, and shifting your red value by 16 bits, so you're effectively just doing r = 0
. You're literally shifting your values past the end of the available space. To store three 8 bit values, you need at LEAST 24 bits, meaning you'll have to use a uint32_t instead.
Upvotes: 5