How to convert color from 32 (24) bits to 16 bits?

1

So, guys, I'm having a problem, I'm creating an editing tool for a PS2 football game.

And this game has two color "systems", which are:

The "normal" RGB that is; A: 0 to 255, G: 0 to 255, B: 0 to 255.

And the, or I think it's rsrs 5bitRGB; A: 0 to 31, G: 0 to 31, B: 0 to 31.

And it's as follows, the "normal" RGB I can send its value to the textbox. And after sending the value to the textBox, I saved that direct value in the game file via Hex, thus changing to the color I want, until then beauty.

This works because ... because the byte "slot" of this color in the game file is actually 3 bytes, so saving the value sent in Hex from the textBox works.

Except that now the problem is 5bitRGB, its slot in the game file is only 2 bytes, not 3, and the colorDialog color options are "normal" from 0 to 255 in both R , G and B, can not only do 0 to 31, and the problem worse, how to send the value of colorDialog in that format of 5bitRGB in 2 bytes to the textBox? Is it possible?

    
asked by anonymous 04.04.2017 / 05:23

1 answer

2

The colors obtained in ColorDialog are in RGB8888 format, where each color is represented by 8 bits plus 8 bits for the alpha channel.

There are two 16-bit RGB formats, RGB555 and RGB565.

In the first, the colors are represented by 5 bits each, in the second, the colors RED and BLUE are represented with 5 bits and the color GREEN with 6.

As I do not know which format is concerned, I leave two methods that convert RGB8888 to each of them.

RGB565:

public ushort Convert8888RGBto565RGB(Color color)
{
    //reduz para 5 bits significativos
    byte r = (byte) (color.R >> 3);
    //reduz para 6 bits significativos
    byte g = (byte)(color.G >> 2);
    //reduz para 5 bits significativos
    byte b = (byte)(color.B >> 3);

    //Junta
    return (ushort)((r << 11) | (g << 5) | b);
}

RGB555

public ushort Convert8888RGBto555RGB(Color color)
{
    //reduz para 5 bits significativos
    byte r = (byte)(color.R >> 3);
    //reduz para 5 bits significativos
    byte g = (byte)(color.G >> 3);
    //reduz para 5 bits significativos
    byte b = (byte)(color.B >> 3);

    //Junta
    return (ushort)((r << 10) | (g << 5) | b);
}

References:

04.04.2017 / 14:57