Help with RGB colors in Hexadecimal 16bits

So, I'm creating an editor for a PS2 football game in C#. And the" system " of colors of the game is RGB, and until then everything was going very well, because I was coming across "normal" hex codes in the game how to choose the color R: 255 G: 255 B: 255 in Hex is 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 that is the following: the RGB color options in this part in the game, it goes only up to the 31st of R: 0 to 31, G: 0-31, B: 0 and 31, and the code is in Hex, "generated" to choose the color of the game is only 2 bytes long (including the "slot" for changing the color via Hex-it's just 2 bytes and not 3 as it should be, for example, in the game, I want to R:245, B:130, G:05, in Hex, is F58205, and in the options of the RGB-31 is A:0, G:0, B:0 and code in Hex, it is 0080, Then... do you have any idea how this works? a way exists even because the guy managed to do in this program old is not true RSRs

Author: Bacco, 2017-04-03

1 answers

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

In the 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 be 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 don't "pop" the bits:

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

Or even same

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

Or, if arranged like this:

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

To extract the colors, the operation would be 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 (much depends on the specific platform). In some cases, some of the colors use one bit more, you have to study the correct one for your platform. The important thing is to understand the logic, the rest is adjustment.

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

 6
Author: Bacco, 2018-04-02 00:14:02