Help with RGB color in 16bit Hexadecimal

5

So, I'm creating an editor for a PS2 football game in C #. And the "color system" of the game is RGB, and so far everything was going very well, because I was encountering with "normal" Hex codes in the game how to choose the color R: 255 G: 255 B: 255 in Hex gets R: FF G: FF B: FF, and then just make the exchange of values via Hex , but I came across a "system" that left me lost even because I am a mere beginner rsrs which is as follows: RGB color options in this part of the game goes only until 31, R: 0 to 31 G: 0 to 31 B: 0 to 31 and the Hex code "generated" when choosing the color in the game is only 2 bytes (including the "slot" for the color exchange via Hex is only 2 bytes same) and not 3 as it should for example: in the game I choose R: 245 B: 130 G: 05 in Hex is F58205 and in the options of RGB 31 is thus R: 0 G: 0 B: 0 and the Hex code stays 0080, So ... do you have any idea how this works? a form exists because the guy was able to do that old program is not true rsrs

    
asked by anonymous 03.04.2017 / 21:20

1 answer

6

Most likely the color palette is reduced (it may have to do with the video processor used, or simply be a compact way to store the data).

In case, being 5 bits per color (from 0 to 31) they can be stored like this (not necessarily in this order):

   1     2     3     3 componentes de cor
┌────┐┌────┐┌────┐
RRRR RGGG GGBB BBBX
└───────┘ └───────┘  2 bytes

To extract the colors, the operation would look something like this:

r = ( valor AND 0b1111100000000000 ) / 2048
g = ( valor AND 0b0000011111000000 ) / 64
b = ( valor AND 0b0000000000111110 ) / 2

To generate colors, just do the reverse, making sure you do not "pop" the bits:

cor = ( r * 2048 ) + ( g * 64 ) + ( b * 2 )

Or even

cor = ( r * 2048 ) OR ( g * 64 ) OR ( b * 2 )

Or, if it's organized like this:

   1     2     3     3 componentes de cor
 ┌────┐┌────┐┌────┐
XRRR RRGG GGGBB BBB
└───────┘ └───────┘  2 bytes

To extract the colors, the operation would look something like this:

r = ( valor AND 0b0111110000000000 ) / 1024
g = ( valor AND 0b0000001111100000 ) / 32
b = ( valor AND 0b0000000000011111 )

And, respectively:

cor = ( r * 1024 ) + ( g * 32 ) + ( b )

(parentheses for easy reading)

The X can be ignored, or used as transparency (it depends a lot on the specific platform). In some cases, some of the colors use a bit more, have to study the correct one for your platform. The important thing is to understand logic, the rest is tuning.

I put the values in binary for read only, convert to decimal or hex in the definitive code. To convert RGB back to 2 bytes , simply multiply by the above values instead of dividing, and make OR of bytes as an alternative to the sum of the examples.

    
03.04.2017 / 21:50