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

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; R: 0 to 255, G: 0 to 255, B: 0 to 255.

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

And is as follows, the RGB "normal" I can send the value of it to the textbox. And after sending the value to the textBox, I save this direct value in the game file via Hex, thus changing to the color I want, until then beauty.

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

Only that now the problem is the 5bitrgb ,the "slot" of it in the game file is only 2 bytes, and not 3, and the colorDialog color options are "normal" from 0 to 255 both in R, G and B, there is no way to do only from 0 to 31, and the problem worse, how to send the value of colorDialog in this format of 5bitrgb in 2 bytes to the textBox? rsrs is it possible?

Author: Pedro Silva, 2017-04-04

1 answers

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.

Since I do not know which format in question, I leave two methods that convert RGB8888 for 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:

 2
Author: ramaral, 2017-04-04 15:24:42