C#将delphi TColor转换为颜色(Hex) [英] C# Converting delphi TColor to color (Hex)
问题描述
>
这些数字存储在数据库中。它们源于Delphi代码。虽然我假设他们遵循某种标准。我试过 Color.FromArgb(255);
These numbers are stored in the Database. They origionate from Delphi code. Although I assume they follow some kind of standard. I have tried Color.FromArgb(255);
但我知道一个事实,第一个是RED在delphi中),其中在ASP.NET中它认为它的蓝色 Color [A = 0,R = 0,G = 0,B = 255]
But i know for a fact that the first is RED (in the delphi side), where as in ASP.NET it thinks its blue Color [A=0, R=0, G=0, B=255]
我想把这些数字转换为十六进制。也就是说#000000,#FFFF99等等
I want these numbers into Hexidecimal anyway. I.e. #000000 , #FFFF99 etc etc
任何人都知道如何将这些整数(见DB图片)转换为十六进制。 $ b
推荐答案
Delphi颜色( TColor
) XXBBGGRR
当不是从调色板或特殊颜色。
Delphi colors (TColor
) are XXBBGGRR
when not from a palette or a special color.
请参阅这篇文章有关格式的更多细节(以及其他特殊情况)。 -k> Christian.K 还包含了关于特殊情况的一些细节。
See this article for more detail on the format (And other special cases). The article pointed by Christian.K also contains some details on the special cases.
要转换为标准颜色,您应该使用类似:
To convert to a standard color you should use something like :
var color = Color.FromArgb(0xFF, c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF);
要转换为十六进制,:
string ColorToHex(Color color)
{
return string.Format("#{0:X2}{1:X2}{2:X2}",
color.R, color.G, color.B);
}
系统颜色
对于系统颜色(数据库中的负值),它们只是由 0x80000000
掩盖的窗口常量。
感谢 David Heffernan 了解详情。
Thanks to David Heffernan for the info.
Color DelphiColorToColor(uint delphiColor)
{
switch((delphiColor >> 24) & 0xFF)
{
case 0x01: // Indexed
case 0xFF: // Error
return Color.Transparent;
case 0x80: // System
return Color.FromKnownColor((KnownColor)(delphiColor & 0xFFFFFF));
default:
var r = (int)(delphiColor & 0xFF);
var g = (int)((delphiColor >> 8) & 0xFF);
var b = (int)((delphiColor >> 16) & 0xFF);
return Color.FromArgb(r, g, b);
}
}
void Main()
{
unchecked
{
Console.WriteLine(DelphiColorToColor((uint)(-2147483646)));
Console.WriteLine(DelphiColorToColor(
(uint)KnownColor.ActiveCaption | 0x80000000
));
Console.WriteLine(DelphiColorToColor(0x00FF8000));
}
}
这篇关于C#将delphi TColor转换为颜色(Hex)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!