不了解Win32 API中的RGB宏声明. [英] Don't understand RGB macro declaration in Win32 API.

查看:141
本文介绍了不了解Win32 API中的RGB宏声明.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RGB宏在WinGDI.h 头文件中定义如下:

RGB macro is defined in WinGDI.h header file as follows:

#define RGB(r,g,b)      ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))



请需要帮助.我知道如何使用此宏,但我不理解它的声明.学习它可能会有所帮助.请逐步回答!

谢谢.



Please Need help. I know how to use this macro, but I don''t understand the declaration of it. Learning it may be helpful. Please Need a step by step answer !

Thanks.

推荐答案

它只是一个宏,用于将3个颜色值打包到32位内存中.

只需查找按位运算符<< (向左移动)和>> (右移)

它接受字节/字符大小的输入,并返回一个32位var,其格式如下:

00000000BBBBBBBBBBGGGGGGGGRRRRRRRR

它的R值,与G值左移8位或,或与B值向左移16位

It''s simply a macro to pack the 3 colour values into a 32 bit piece of memory.

Just look-up the bitwise operators << (shift left) & >> (shift right)

It takes byte/char sized inputs and returns a 32 bit var, formatted thusly:

00000000BBBBBBBBGGGGGGGGRRRRRRRR

Its the R value, ORed with the G value shifted left 8 bits, ORed with the B value shifted left by 16 bits

000000000000000000000000RRRRRRRR
0000000000000000GGGGGGGG00000000
00000000BBBBBBBB0000000000000000

00000000BBBBBBBBGGGGGGGGRRRRRRRR


它以32位整数/以"0x00bbggrr"的形式创建无符号的"24位颜色参考".

红色是低8位(0-7),绿色是后8位(8-15),蓝色是后8位(16-23).

所有的"typcasting"都是为了指示编译器如何对值进行数学运算(移位).围绕(r),(g)和(b)的括号是这样,因此您可以使用表达式"作为参数,并将其正确处理.

对于您来说,散布此语句并慢慢删除子表达式以查看括号(parenthesii?)如何包装要由编译器评估的部分可能是一个有趣的练习.那么对您来说可能更有意义.

最后一件事,对于"#define"宏,括号用于为编译器提供清晰度,具有讽刺意味的是,它消除了人类(您)的清晰度.
It creates a "24 bit color reference" in 32 bit integer / unsigned in the form of "0x00bbggrr"

Red is the low 8 bits (0-7), Green is the next 8 bits (8-15) and Blue is the next 8 bits (16-23).

All the "typcasting" is to instruct the compiler how to do the math (shifts) on the values. The parenthesis around (r), (g), and (b) is so you can use an "expression" as the argument and it will be treated properly.

It might be a fun exercise for you to spread this statement out and slowly remove the sub-expressions to see how the parenthesises (parenthesii?) wrap the pieces to be evaluated by the compiler. Then it might make more sense to you.

One last thing, for "#define" macros, parenthesis are used to provide clarity to the compiler, which, ironically, removes clarity for the human (you).


它仅由32个字符组成.三个分量r,g和b的位值.这是一步一步来的.首先,我们将外部括号删除并转换为COLORREF:

It just composes a 32-bit value from the three components r, g, and b. Here is it step by step. First let''s take the outer parentheses and cast to COLORREF away:

(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16))



通过强制转换为BYTE将r值减小为8位,并复制到低8位:



The r value is just reduced to 8-bits by the cast to BYTE and copied into the lower 8 bits:

(BYTE)(r)



绿色值也被其低8位掩盖,然后转换为16位(这是不必要的)并向左移动8位,因此绿色组件现在占据了8 ... 15位:



The green value is also masked by its lower 8 bits, then cast into 16-bits (which is unnecessary) and shifted 8 bits to the left so that the green component now occupies bits 8...15:

((WORD)((BYTE)(g))<<8)



两个组件按位或在一起:



Both components are bitwise or-ed together:

((BYTE)(r)|((WORD)((BYTE)(g))<<8))



最终,蓝色值被屏蔽为8位,转换为32位(再次不需要),并向左移动16位,这样蓝色分量现在占据了16 ... 23:



Finally the blue values is masked to 8 bits, cast up to 32-bit (again unnecessary) and shifted 16 bits to the left such that the blue component occupies now bits 16...23:

(((DWORD)(BYTE)(b))<<16)



然后将其合并为上一步中的红色和绿色合并值.所以我们最后以



Then it''s combined to the red and green combined value that we did in the previous step. So we end up with

red:   bits 0...7
green: bits 8...15
blue:  bits 16...23



很简单,不是吗.



Easy, wasn''t it.


这篇关于不了解Win32 API中的RGB宏声明.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆