从 4 个浮点数创建 UINT32
Creating a UINT32 from 4 floats
好的。
我正在使用用于 DirectX 的 FW1FontWrapper 代码
: https://archive.codeplex.com/?p=fw1
这让我不再需要使用由纹理驱动的过时且无用的字体引擎。
但是,此包装器中的 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 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;
}
我的错误
...我相信,我试图将 UINT8 推回,导致溢出,所以我需要先转换为 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);
}
好的。
我正在使用用于 DirectX 的 FW1FontWrapper 代码
: https://archive.codeplex.com/?p=fw1
这让我不再需要使用由纹理驱动的过时且无用的字体引擎。
但是,此包装器中的 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 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;
}
我的错误
...我相信,我试图将 UINT8 推回,导致溢出,所以我需要先转换为 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);
}