How to convert 32(24) bit color to 16 bit?

Asked

Viewed 682 times

1

So guys, I’m in big trouble rsrs, I’m creating an editing tool for a PS2 football game.

And this game has two "systems" of colors, which are they:

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

And the, or think is rsrs 5bitRGB; R: 0 to 31, G: 0 to 31, B: 0 to 31.

And here’s the thing, the "normal" RGB I can send its value to the textbox. And after sending the value to the textbox, I save this value directly in the game file via Hex, thus changing to the color I want, until then beauty.

This works because... because the "slot" of bytes of this color in the game file are actually 3 bytes, so saving the value sent in Hex of the textbox works.

Except that now the problem is the 5bitRGB, the "slot" of it in the game file is only 2 bytes, not 3, and colorDialog the 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 worst problem, how to send the value of colorDialog in this 5bitRGB format in 2 bytes to the textbox? rsrs will be possible?

1 answer

2


Colors obtained in Colordialog are in RGB8888 format, where each color is represented by 8 bits plus 8 bits for 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 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:

  • I did some tests here and the RGB565 seems to be the correct one, but this way it still does not give the correct value, choosing the white color it even gives the correct value that is 65535 in Hex FFFF. I noticed that the RGB555 has the value from 0 to 32767 while the RGB565 has the value of 32768 which is correct in Hex 0080 (Black color) up to 65535 which is also correct in Hex FFFF, so I think that something else is missing to give the correct value to convert to Hex.

  • Your comment is a bit confusing. In which situations RGB565 does not give the correct value?

  • So I did the following to get the value of colorDialog in RGB565: pictureBox1.Backcolor = colorDialog1.Color; var C1 = Convert8888rgbto555rgb(colorDialog1.Color); var C2 = Convert.Tostring(C1); Messagebox.Show(C2); Messagebox.Show(((int)C1).Tostring("X2")); it give the value of normal and is converted into Hex which is what I want.

  • 1

    @ramaral just for the record: http://answall.com/questions/194790/70

  • Already get here guys thanks there for the help! :D

  • @Bacco My code is correct or not?

  • @ramaral I imagine so, the author even accepted. I only mentioned the other one so you were aware that it was the same subject. About be 555, 565, by the posted images I deduce that it is 555 (all sliders stop at 31), but then only the author testing same.

Show 2 more comments

Browser other questions tagged

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