Convert RGB color to 16 bit color

Source: Internet
Author: User

Convert RGB color to 16 bit color

[Suitable for beginners with game programming]

I need to judge the colorkey when doing 16bit alpha blending. In my game engine, the colorkey is represented by Windows RGB color, in this case, we need to convert the RGB color to a 16-bit color. below is my practice.
The RGB color is a DWORD Value, 32bit in the format of 0x00rrggbb. Three colors are obtained through three macros: getrvalue, getgvalue, and getbvalue, which are represented by 8 bitbyte. There are two types of 16-bit colors: 555 and 565. For example, the 16-bit color format in 565 mode is rrrrrggggggbbbbb. Our task is to convert 0x00rrggbb to rrrrrggggggbbbbb.
First, separate the three RGB components and convert them into colors represented by 5bit, 6bit, and 5bit (the first 3bit, 2bit, and 3bit are 0 ). Because 8 bits are 256-level chromients and 5 bits are 32-level chromients, each 8-level chromium is converted into 1-level chromium, divided by 8 (three shifts right; if the value is changed to 6 BITs by 4, because 6 bits can express 64-level color. Then, clear the 5bit, 0x1f, 6bit, and 0x3f before the 8bit, and clear the 3bit or 2bit before the 8bit. The following three components are obtained: r = 000 rrrrr, G = 00 gggggg, and B = 000 bbbbb. Finally, you can shift them to a 16-bit565 color. The formula is as follows:
Color16bit = (getrvalue (colorrgb)> 3) & 0x1f) <11 |
(Getgvalue (colorrgb)> 2) & 0x3f) <5 |
(Getbvalue (colorrgb)> 3) & 0x1f;
In fact, there is no need to compare it with 0x1f and 0x3f, because the color range after conversion is 0 ~ 31 or 0 ~ 63, the first 3bit or 2bit must be 0 (from the bit operation point of view, the bit that is left blank after the right shift is automatically filled with 0); therefore, remove the above and the operation, yes (note that the shift operation cannot be merged; otherwise, the first 3bit and 2bit cannot be cleared to 0 ):
Color16bit = getrvalue (colorrgb)> 3 <11 | getgvalue (colorrgb)> 2 <5 | getbvalue (m_colorrgb)> 3;

It's quite simple, but it's just a bunch of bit operations. As long as we are excited, we can easily write conversion formulas between various formats.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/n5/archive/2004/01/24/14273.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.