+  RHDN Forum Archive
|-+  Romhacking
| |-+  ROM Hacking Discussion
| | |-+  How can i convert 2 bytes of color values to RGB value
Pages: [1]
Author Topic: How can i convert 2 bytes of color values to RGB value  (Read 370 times)
kenghot
Guest
« on: September 03, 2007, 11:02:00 pm »

Hi,
   How can i convert 2 bytes of color values to RGB value. for example 0x0000  = r: 0 g: 0 b : 0
Thanks,
kenghot
Spikeman
Guest
« Reply #1 on: September 03, 2007, 11:11:20 pm »

In my experience this is 15-bit color (5 bits per color), the GBA for example uses this sort of color. The bits are generally stored like this:

0bbbbbgg gggrrrrr

Generic code:

Code:
color = 0x1234; //in binary this is 00010010 00110100

r = color & 0x1F; //1F in binary is 11111 - bitmask to isolate red
g = (color >> 5) & 0x1F; //similar except shift the bits to isolate blue
b = (color >> 10) & 0x1F;

Hope this helps. Smiley
kenghot
Guest
« Reply #2 on: September 03, 2007, 11:20:31 pm »

Thanks Spikeman,
   if i get value form rom , should i reverse byte berfor covert. for example value in rom is 0x1234 . should i reverse byte to 0x3412 first
Spikeman
Guest
« Reply #3 on: September 03, 2007, 11:28:43 pm »

If you are reading byte by byte then yes (for the majority of systems, I can't think of a system of the top of my head that is big-endian).
Disch
Guest
« Reply #4 on: September 04, 2007, 12:22:02 am »

for clarity sake... put a space between individual bytes and only group them together after endianness has been resolved.


That is... if the file contains two bytes:  $34 and $26.  It's $2634 or 34 26... not $3426
kenghot
Guest
« Reply #5 on: September 04, 2007, 05:23:04 am »

Thanks guys, i got it .

creaothceann
Guest
« Reply #6 on: September 04, 2007, 07:39:44 am »

Quote from: Spikeman on September 03, 2007, 11:11:20 pm
In my experience this is 15-bit color (5 bits per color), the GBA for example uses this sort of color. The bits are generally stored like this:

0bbbbbgg gggrrrrr

Generic code:

Code:
color = 0x1234; //in binary this is 00010010 00110100

r = color & 0x1F; //1F in binary is 11111 - bitmask to isolate red
g = (color >> 5) & 0x1F; //similar except shift the bits to isolate blue
b = (color >> 10) & 0x1F;

Hope this helps. Smiley

I know the question has been answered, but... >_>


Write either "0BBBBBGGGGGRRRRR" or "byte 0 = GGGRRRRR; byte 1 = 0BBBBBGG".

And some more code:

Code:
const   Bit0   = 1 <<  0;                 // e.g. in a header file
const   Bit1   = 1 <<  1;
const   Bit2   = 1 <<  2;
const   Bit3   = 1 <<  3;
const   Bit4   = 1 <<  4;
const   Bit5   = 1 <<  5;
const   Bit6   = 1 <<  6;
const   Bit7   = 1 <<  7;
const   Bit8   = 1 <<  8;
const   Bit9   = 1 <<  9;
const   Bit10  = 1 << 10;
const   Bit11  = 1 << 11;
const   Bit12  = 1 << 12;
const   Bit13  = 1 << 13;
const   Bit14  = 1 << 14;
const   Bit15  = 1 << 15;

const   Bits0  = Bit0  - 1;
const   Bits1  = Bit1  - 1;
const   Bits2  = Bit2  - 1;
const   Bits3  = Bit3  - 1;
const   Bits4  = Bit4  - 1;
const   Bits5  = Bit5  - 1;
const   Bits6  = Bit6  - 1;
const   Bits7  = Bit7  - 1;
const   Bits8  = Bit8  - 1;
const   Bits9  = Bit9  - 1;
const   Bits10 = Bit10 - 1;
const   Bits11 = Bit11 - 1;
const   Bits12 = Bit12 - 1;
const   Bits13 = Bit13 - 1;
const   Bits14 = Bit14 - 1;
const   Bits15 = Bit15 - 1;


Color = 0x1234;   // in binary this is 1001000110100

R = (Color      ) & Bits5;
G = (Color >> 5 ) & Bits5;
B = (Color >> 10) & Bits5;

These bit constants make the code more readable IMO, and you don't have to write hex literals or even use the calculator.


Quote from: kenghot on September 03, 2007, 11:20:31 pm
if i get value from rom, should i reverse byte before covert. for example value in rom is 0x1234. should i reverse byte to 0x3412 first

Only if the emulated hardware is of another endianess than the hardware your code's running on.
Pages: [1]  


Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC