[英]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.