We are working on modding an old 16Bit era video game cartridge. We are hoping to inject some our own Sprites into the game to dip our toes in the water.
To do so, we are developing an app to both display the Sprites and convert new ones to the Hex (to make it easier to inject.)
The game stores individual pixels as 2 Digit Hexidecimal Values (0x0~0xFFFF). The game uses bitwise shifts to establish the individual Red, Green, and Blue colors. There's some old documentation we had to fall back from the Sprite Resources community to confirm this. This confirmed use of two masks.
We have the display function working perfectly. The function receives the HEX then returns an ARRAY with the 3 values: R, G, B.
In the group, we do not have anyone particularly good working with bitwise shifts. We are looking for help turning the 3 "int" colors back into it's original single 2 Digit Hex.
ANSWERED!! THANKS
First, are you sure you want to use ~
in your calculation here:
colorRGB0R = ~(((HexPixelValue >> 11) & PixelMask1) << 3);
colorRGB0G = ~(((HexPixelValue >> 5) & PixelMask2) << 2);
colorRGB0B = ~((HexPixelValue & PixelMask1) << 3);
Because the math appears fine except for that? Maybe the commented part does something with the values but I'm not quite sure why you're inverting them. In any case...
Basically, you are working with a 565 16 bit color then. Rather than that bitmask, it is a lot easier to understand if you write the bit layout of the 16 bit value like this: rrrrrggg gggbbbbb
as it visualizes which bits you want to set with what values.
Meaning that red and blue are 5 bit values (0-31) and green is a 6 bit value (0-63). However, since color values are meant to be in the range of 0-255, after extracting the bits, you have to multiply them to get the range. The multiplying here is done by bit shifting as well.
To reconstruct the 16 bit value, you can do something like this:
int ToHex(int red, int green, int blue)
{
if (red < 0 || red >= 256)
throw new ArgumentOutOfRangeException(nameof(red));
if (green < 0 || green >= 256)
throw new ArgumentOutOfRangeException(nameof(green));
if (blue < 0 || blue >= 256)
throw new ArgumentOutOfRangeException(nameof(blue));
// red & 0xF8 cuts off the bottom 3 bits to be save
// green & 0xFC cuts off the bottom 2 bits
// blue needs to be shifted to the right anyway, so we use that to cut off 3 bits
return ((red & 0xF8) << 8) |
((green & 0xFC) << 3) |
(blue >> 3);
}