问题描述
我正在开发一个带有TFT触摸屏的项目,这个屏幕上有一个附带的库。但在一些阅读后,我仍然没有得到什么。在库中有关于颜色的一些定义:
I am working on a project with a TFT touch screen, with this screen there is an included library. But after some reading, i still dont get something. In the library there are some defines regarding colors:
/* some RGB color definitions */
#define Black 0x0000 /* 0, 0, 0 */
#define Navy 0x000F /* 0, 0, 128 */
#define DarkGreen 0x03E0 /* 0, 128, 0 */
#define DarkCyan 0x03EF /* 0, 128, 128 */
#define Maroon 0x7800 /* 128, 0, 0 */
#define Purple 0x780F /* 128, 0, 128 */
#define Olive 0x7BE0 /* 128, 128, 0 */
#define LightGrey 0xC618 /* 192, 192, 192 */
#define DarkGrey 0x7BEF /* 128, 128, 128 */
#define Blue 0x001F /* 0, 0, 255 */
#define Green 0x07E0 /* 0, 255, 0 */
#define Cyan 0x07FF /* 0, 255, 255 */
#define Red 0xF800 /* 255, 0, 0 */
#define Magenta 0xF81F /* 255, 0, 255 */
#define Yellow 0xFFE0 /* 255, 255, 0 */
#define White 0xFFFF /* 255, 255, 255 */
#define Orange 0xFD20 /* 255, 165, 0 */
#define GreenYellow 0xAFE5 /* 173, 255, 47 */
#define Pink 0xF81F
这些是16位颜色。但是他们如何从:0,128,128(深蓝色)到0x03EF。我的意思是,你如何将16位颜色转换为uint16?这不需要在代码中有anwser,因为我只是想在库中添加一些collors。到一个在线转换器的链接(我找不到)也可以:)
Those are 16 bit colors. But how do they go from: 0,128,128(dark cyan) to 0x03EF. I mean, how do you convert a 16bit color to a uint16? this doesn't need to have a anwser in code, because i just want to add some collors in the library. A link to a online converter (wich i could not find) would be okay as well :)
感谢
推荐答案
从这些可以很容易地找出公式:
From these one can easily find out the formula:
#define Red 0xF800 /* 255, 0, 0 */
#define Magenta 0xF81F /* 255, 0, 255 */
#define Yellow 0xFFE0 /* 255, 255, 0 */
F800设置了5个MSB位,FFE0没有设置5个LSB。
0xF81F显然具有5个LSB和5个MSB的集合,这证明格式是RGB565。
F800 has 5 MSB bits set and FFE0 has 5 LSB not set.0xF81F has obviously both 5 LSB's and 5 MSB's set, which proves the format to be RGB565.
将值173转换为Red的公式不是直接的,因为它可能看起来 - 你不能简单地删除3个最低有效位,但必须线性插值使255对应于31(或绿色255对应于63)。
The formula to convert a value 173 to Red is not as straightforward as it may look -- you can't simply drop the 3 least significant bits, but have to linearly interpolate to make 255 to correspond to 31 (or green 255 to correspond to 63).
NewValue = (31 * old_value) / 255;
(这只是一个截断的部分 - 可能需要适当的舍入)
(And this is still just a truncating division -- proper rounding could be needed)
适当舍入和缩放:
Uint16_value = (((31*(red+4))/255)<<11) |
(((63*(green+2))/255)<<5) |
((31*(blue+4))/255);
EDIT 添加括号,以符合JasonD的建议。
EDIT Added parenthesis to as helpfully suggested by JasonD.
这篇关于c ++定义了16bit(高)的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!