1

So I'm creating an editor of a PS2 game. And this game has two "Systems" of colors.

The "normal" RGB R: 0 to 255 G: 0 to 255 B: 0 to 255.

And the or I think it is 5bitRGB R: 0 to 31 G: 0 to 31 B: 0 to 31.

And to make the color change in the game, I have to convert the chosen values in the colorDialog in Hexadecimal for example: R: 255 G: 176 B: 15 In Hexadecimal stands FFB00F.

And then later change these values in the "slots" of 3 bytes via Hex.

Beauty so far so good, but 5bitRGB only have "slots" of 2 bytes.

Example: 5bitRGB R: 31 G: 0 B: 0 in Hex 1F80.

And that's where I do not know what to do, because the colors of the normal RGB I can send the values in Hexadecimal to the textBox.

And then I saved these values textBox in "slots" of 3 bytes via Hex.

Meanwhile the slots for the 5bitRGB color change via Hex They are only "slots" of 2 bytes.

So I would have to send the converted colorDialog value to 5bitRGB for textBox in 2 bytes, is this really possible?

1 Answers1

0

So I would have to send the converted colorDialog value to 5bitRGB for textBox in 2 bytes, is this really possible?

Sure! 5-bits x 3 fields = 15-bits, and 2 bytes = 16-bits. You'll even have room left over, but not much.

It sounds like it's just a matter of handling a reduction in the resolution of each field. This can be done with a bitwise shift operation to reduce each 8-bit field to a 5-bit field.

You'll need to right shift the value for storage as a 5-bit field. You can also left shift the value to use as an 8-bit (byte) field for setting the Color property - but note that this is just an 8-bit representation of a 5-bit value and the loss of resolution from shifting to 5-bits will persist. You will have to decide on a convention to handle the loss of resolution - doing nothing is an option.

For example:

// 8-bit color values
byte rValue = 255;
byte gValue = 127;
byte bValue = 63;

// set color
pictureBox1.BackColor = Color.FromArgb(rValue, gValue, bValue);

// 5-bit color values
var rBits = new BitArray(5, false);
var gBits = new BitArray(5, false);
var bBits = new BitArray(5, false);

// bit position comparison operator
byte op = 0x80;

// convert to 5-bit arrays
for (int i = 0; i < 5; i++)
{
    if (rValue > op) { rBits[i] = true; rValue -= op; }
    if (gValue > op) { gBits[i] = true; gValue -= op; }
    if (bValue > op) { bBits[i] = true; bValue -= op; }
    op >>= 1;
}

byte rRetrieved = 0;
byte gRetrieved = 0;
byte bRetrieved = 0;

// bit position comparison operator
op = 0x80;

// convert back to 8-bit bytes
for (int i = 0; i < 5; i++)
{
    if (rBits[i]) { rRetrieved += op; }
    if (gBits[i]) { gRetrieved += op; }
    if (bBits[i]) { bRetrieved += op; }
    op >>= 1;
}

// set color - note loss of resolution due to shifting
pictureBox1.BackColor = Color.FromArgb(rRetrieved, gRetrieved, bRetrieved);
khargoosh
  • 1,450
  • 15
  • 40