任何人都可以告诉我如何从ARGB,相应的Windows颜色计算?我知道有一个ColorTranslator.ToWin32()进行计算,但它是如何完成的?
另外,OLE颜色和windows(win32)颜色有什么区别?
JD。
答案 0 :(得分:3)
使用IL反汇编程序(如Reflector),您可以获取ColorTranslator.ToWin32()的代码。请注意,ToWin32颜色不使用alpha值。
public static int ToWin32(Color c)
{
return ((c.R | (c.G << 8)) | (c.B << 0x10));
}
ColorTranslator.ToOle(...)正在测试颜色是否为标准系统颜色,否则,它正在使用ToWin32转换:
public static int ToOle(Color c)
{
if (c.IsKnownColor)
{
switch (c.ToKnownColor())
{
case KnownColor.ActiveBorder:
return -2147483638;
case KnownColor.ActiveCaption:
return -2147483646;
case KnownColor.ActiveCaptionText:
return -2147483639;
case KnownColor.AppWorkspace:
return -2147483636;
case KnownColor.Control:
return -2147483633;
case KnownColor.ControlDark:
return -2147483632;
case KnownColor.ControlDarkDark:
return -2147483627;
case KnownColor.ControlLight:
return -2147483626;
case KnownColor.ControlLightLight:
return -2147483628;
case KnownColor.ControlText:
return -2147483630;
case KnownColor.Desktop:
return -2147483647;
case KnownColor.GrayText:
return -2147483631;
case KnownColor.Highlight:
return -2147483635;
case KnownColor.HighlightText:
return -2147483634;
case KnownColor.HotTrack:
return -2147483635;
case KnownColor.InactiveBorder:
return -2147483637;
case KnownColor.InactiveCaption:
return -2147483645;
case KnownColor.InactiveCaptionText:
return -2147483629;
case KnownColor.Info:
return -2147483624;
case KnownColor.InfoText:
return -2147483625;
case KnownColor.Menu:
return -2147483644;
case KnownColor.MenuText:
return -2147483641;
case KnownColor.ScrollBar:
return -2147483648;
case KnownColor.Window:
return -2147483643;
case KnownColor.WindowFrame:
return -2147483642;
case KnownColor.WindowText:
return -2147483640;
case KnownColor.ButtonFace:
return -2147483633;
case KnownColor.ButtonHighlight:
return -2147483628;
case KnownColor.ButtonShadow:
return -2147483632;
case KnownColor.GradientActiveCaption:
return -2147483621;
case KnownColor.GradientInactiveCaption:
return -2147483620;
case KnownColor.MenuBar:
return -2147483618;
case KnownColor.MenuHighlight:
return -2147483619;
}
}
return ToWin32(c);
}
这意味着如果您声明Color color = SystemColors.ControlText,ColorTranslator.ToOle(颜色)将返回-2147483630,但如果您将颜色设置为自定义ARGB颜色,ColorTranslator.ToOle将返回标准ToWin32( )价值。
答案 1 :(得分:2)
.NET中的颜色对象包括alpha通道(即透明度级别),而Win32颜色纯粹是RGB。因此,要在两者之间进行转换,您需要以下内容:
static public int ConvertColourToWindowsRGB(Color dotNetColour)
{
int winRGB = 0;
// windows rgb values have byte order 0x00BBGGRR
winRGB |= (int)dotNetColour.R;
winRGB |= (int)dotNetColour.G << 8;
winRGB |= (int)dotNetColour.B << 16;
return winRGB;
}
static public Color ConvertWindowsRGBToColour(int windowsRGBColour)
{
int r = 0, g = 0, b = 0;
// windows rgb values have byte order 0x00BBGGRR
r = (windowsRGBColour & 0x000000FF);
g = (windowsRGBColour & 0x0000FF00) >> 8;
b = (windowsRGBColour & 0x00FF0000) >> 16;
Color dotNetColour = Color.FromArgb(r, g, b);
return dotNetColour;
}