[英]Creating a UINT32 from 4 floats
好的。
我正在使用用于 DirectX 的 FW1FontWrapper 代码
: https://archive.codeplex.com/?p=fw1
这消除了我使用由纹理驱动的过时且无用的字体引擎的需要。
但是,此 Wrapper 中的 DrawString 函数对颜色表示有特殊要求。
UINT32 Color : In the format 0xAaBbGgRr
我为此任务提供的数据是一个恒定的 Alpha 值:1.0f。
以及 R、G 和 B 的 3 个可变浮点值,范围从 0.0f 到 1.0f。
鉴于 UNIT32 中颜色的特殊排列,我正在尝试编写一个函数,该函数将使用给定的 3 个浮点值创建此 UNIT32。
我的尝试
UINT32 TextClassA::getColour(SentenceType* sentence)
{
//Convert each float value to its percentage of 255
int colorb = 255 * sentence->blue;
int colorg = 255 * sentence->green;
int colorr = 255 * sentence->red;
//convert each int to 8 bit Hex
UINT8 ucolorb = colorb;
UINT8 ucolorg = colorg;
UINT8 ucolorr = colorr;
//Push each hex back onto a UNIT32
UINT32 color = 0xFF + (ucolorb << 6) + (ucolorg << 4) + (ucolorr << 2);
return color;
}
句型
红色、绿色和蓝色只是 0.0-1.0f 的每个 RGB 值的浮点数
我的点子。
大概是我可以:
将每个浮点值转换为其 255 的百分比(不要太担心完美的准确性。
将这些整数值转换为 UINT8s
然后将它们推回 UINT32
通过避免使用所有临时变量并使用类似下面的代码,可以使实现更加清晰。 也就是说,任何合理的优化编译器都应该在两种情况下生成相同的代码。
UINT32 TextClassA::getColour(SentenceType* sentence)
{
//Convert color components to value between 0 and 255.
UINT32 r = 255 * sentence->red;
UINT32 b = 255 * sentence->blue;
UINT32 g = 255 * sentence->green;
//Combine the color components in a single value of the form 0xAaBbGgRr
return 0xFF000000 | r | (b << 16) | (g << 8);
}
我想到了!!!
UINT32 TextClassA::getColour(SentenceType* sentence)
{
//Convert each float value to its percentage of 255
int colorb = 255 * sentence->blue;
int colorg = 255 * sentence->green;
int colorr = 255 * sentence->red;
//convert each int to 8 bit Hex
UINT8 ucolorb = 0x00 + colorb;
UINT8 ucolorg = 0x00 + colorg;
UINT8 ucolorr = 0x00 + colorr;
//Convert each UINT8 to a UINT32
UINT32 u32colorb = ucolorb;
UINT32 u32colorg = ucolorg;
UINT32 u32colorr = ucolorr;
//Create final UINT32s and push the converted UINT8s back onto each.
UINT32 u32finalcolorb = 0x00000000 | (u32colorb << 16);
UINT32 u32finalcolorg = 0x00000000 | (u32colorg << 8);
UINT32 u32finalcolorr = 0x00000000 | (u32colorr);
//0xAaBbGgRr
//Push each hex back onto a UNIT32
UINT32 color = 0xFF000000 | u32finalcolorb | u32finalcolorg |
u32finalcolorr;
return color;
}
我的错
...我相信,试图将 UINT8s 推回,导致溢出,所以我需要先转换为 UINT32s。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.