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 > Converting RGB values

#576 - XyzZyx - Wed Jan 08, 2003 9:41 pm

I just managed to load graphics onto the screen! But something seems to be wrong with the palette. I have all the RGB values with each color in range 0-255 and am trying to do a real-time conversion into the 15-bit format the gameboy uses. Here is the formula I came up with:
u16 rgb = (r >> 3) | (g >> 3) << 5 | (b >> 3) << 10;
I don't know, have I mixed the bit positions or something? Thanks.

#583 - Lord Graga - Wed Jan 08, 2003 9:55 pm

r + g <<5+ b<<10

#585 - Nessie - Wed Jan 08, 2003 10:05 pm

Lord Graga's formula is correct for converting a (0-31) RGB value to a 15 bit color.

The formula you have posted should work for a run-time conversion from (0-255) RGB to (0-31) RGB....except, I suspect you are falling trap to an order of operations problem.

I'd try augmenting it with a few parens just to make sure:

u16 rgb = (r >> 3 ) + (( g >> 3 ) << 5 ) + (( b >> 3 ) << 10 );