gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Graphics > how to convert an RGB value to a color palette value

#41763 - kouky - Sun May 01, 2005 7:07 pm

Could someone give me a formula to convert a rgb value
(between 0,0,0 to 255,255,255) into a color value used by the gba ( between 0000 to 7fff).

I searched by myself all the afternoon, but can't resolve it.

Thank you

#41778 - DekuTree64 - Sun May 01, 2005 9:17 pm

8-bit RGB (255,255,255) is rrrrrrrrggggggggbbbbbbbb, and GBA's 5-bit BGR (31,31,31) is 1bbbbbgggggrrrrr. To convert, separate out the colors, shift them down, and put them back together.

Generally it's more normal to make macros that take the colors in RGB order and put them together as BGR, so I've named mine RGB15.

Code:
   // Shift the component you want into the
   // bottom 8 bits, and mask off the rest
#define RGB_R(color)    (((color) >> 16) & 0xff)
#define RGB_G(color)    (((color) >> 8) & 0xff)
#define RGB_B(color)    ((color) & 0xff)

   // Put 5-bit (0,31) values together in GBA's BGR order
#define RGB15(r, g, b)  ((r) | ((g) << 5) | ((b) << 10)

   // Shift each component down by 3 bits
   // to get from 8-bit to 5-bit
#define RGB8_TO_RGB15(color)   RGB15( RGB_R(color)>>3, RGB_G(color)>>3, RGB_B(color)>>3 )

_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku