从4个浮子创建UINT32
Creating a UINT32 from 4 floats
ok。
我正在使用fw1fontwrapper代码,供directx
:https://archive.codeplex.com/?p=fw1
这已经消除了我使用纹理提供动力的过时且无用的字体引擎的需求。
但是,此包装器中的拉链功能具有颜色表示的特殊要求。
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;
}
sendenceType
红色,绿色和蓝色只是floats flas flat flat flot flogb
从0.0-1.0f我的想法。
大致是我可以:
将每个浮子值转换为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;
}
我的错误
...我相信,试图将UINT8推回,引起溢出,因此我需要先转换为UINT32S。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 从4个浮子创建UINT32
- 如何创建独立<UINT32>窗口 COM 接口