Convert RGB color to 16bit colors [suitable for game programming beginners]

zhaozj2021-02-16  43

Convert RGB color to 16bit colors

[Suitable for programming initiator]

I need to judge the color key when doing 16bit alpha beding, and in my game engine, ColorKe is expressed in Windows RGB color, which requires the RGB color to 16bit colors, below is my approach. RGB color is a DWORD value, 32bit, format 0x00RGGBB, three macros: GtrValue, getGValue, and getBvalue can get three color components, with 8bitbyte. The 16bit colors are 555 and 565, with 565 as an example, the 565 mode 16bit color format is: rrrrrrgggggbbbb. Our task is to convert 0x00RrGGBB to rrrrrrggggggbbbb. First separate the three components of RGB, twist them into 5Bit, 6bit, 5bit, 5bit (the first 3bit, 2bit, 3bit 0). Because 8bit is 256 grade chromolyte, it is transferred to 5Bit to be 32 grade, so it is transferred to 1 grade chroma every 8 levels, divided by 8 (right shift 3); 4. Because 6bit can express 64 grade color. Then, the resulting 5bit and the upper 0x1f, 6bit and the upper 0x3f, 3bit or 2 bit clear 0 in front of the 8bit. The three components obtained at this time are: r = 000rrrrr, g = 00ggggg, b = 000bbbbb. Finally, you can displace them or get a 16bit565 mode color. The operation is as follows: color16bit = (GtrValue (colorRGB) >> 3) & 0x1f) << 11 | ((GetGvalue (Colorrgb) >> 2) & 0x3f) << 5 | (Getbvalue (Colorrgb) >> 3) & 0x1f; actually does not need to be with upper 0x1f and 0x3f, because the transformation is 0 ~ 31 or 0 ~ 63, the first 3bit or 2Bit must be 0 (from the bit operation angle to the right shift, the bit is automatically opened Fill in 0); so remove the above, it is (note that the shift operation cannot be merged, otherwise you can't make the first 3bit, 2bit Qing 0): color16bit = GtrValue (colorRGB) >> 3 << 11 | getgvalue (colorRGB) >> 2 << 5 | GetBValue (M_Colorrgb) >> 3;

It's very simple, just a bunch of operations. As long as you have a multi-dynasty, we can easily write a conversion between various formats.

转载请注明原文地址:https://www.9cbs.com/read-27935.html

New Post(0)