Help with 16bit Hexadecimal RGB colors

Asked

Viewed 386 times

5

So I’m creating an editor of a PS2 football game in C#. And the game’s "color system" is RGB, and by then it was all going very well, because I was coming across "normal" Hex codes in the game like choosing 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" which left me lost even because I am a mere beginner rsrs which is as follows: the RGB color options in this part of the game go only up to 31, R: 0 to 31 G: 0 to 31 B: 0 to 31 and the code in Hex "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 even ) and not 3 as it should be for example: in the game I choose R:245 B:130 G:05 in Hex is F58205 and already in the options of RGB 31 is thus R:0 G:0 B:0 and the code Hex is 0080, So... do you have any idea how this works? a way exists even because the guy managed to do in this old program is not true rsrs

1 answer

6


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

In this 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, ensuring not to "burst" the bits:

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

Or even

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

Or, if it’s arranged that way:

   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 to facilitate reading)

The X can be ignored, or used as transparency (depends a lot on the specific platform). In some cases, some of the colors use an extra bit, 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 in the 2 bytes, just multiply by the above values instead of divide, and make a OR as an alternative to the sum of examples.

  • Has a color scheme, RGB 565, which uses 6 bits for green and 5 bits for other colors.

  • @I mentioned it in the penultimate paragraph. And in 8 bits there are those who leave the blue with 2 bits and the others with 3, because the blue is the one that we least perceive nuances. Then, in a next Edit, I elaborate better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.