转自:http://www.cnblogs.com/wall/articles/1147074.html
#Region "将.NET中的Color结构转换至于ArcGIS Engine中的IRgbColor接口"
''' <summary>
''' 将.NET中的Color结构转换至于ArcGIS Engine中的IRgbColor接口
''' </summary>
''' <param name="pColor">.NET中的System.Drawing.Color结构表示ARGB颜色</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ConvertColorToIRgbColor(ByVal pColor As Color) As IRgbColor
Dim pRgbColor As IRgbColor = New RgbColor
pRgbColor.RGB = pColor.B * 65536 + pColor.G * 256 + pColor.R
Return pRgbColor
End Function
#End Region
#Region "将.NET中的Color结构转换至于ArcGIS Engine中的IColor接口"
''' <summary>
''' 将.NET中的Color结构转换至于ArcGIS Engine中的IColor接口
''' </summary>
''' <param name="pColor">.NET中的System.Drawing.Color结构表示ARGB颜色</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function ConvertColorToIColor(ByVal pColor As Color) As IColor
Dim pESRIColor As IColor = New RgbColor
pESRIColor.RGB = pColor.B * 65536 + pColor.G * 256 + pColor.R
Return pESRIColor
End Function
#End Region
#Region "将ArcGIS Engine中的IRgbColor接口转换至.NET中的Color结构"
''' <summary>
''' 将ArcGIS Engine中的IRgbColor接口转换至.NET中的Color结构
''' </summary>
''' <param name="pRgbColor">IRgbColor</param>
''' <returns>.NET中的System.Drawing.Color结构表示ARGB颜色</returns>
''' <remarks></remarks>
Public Function ConvertIRgbColorToColor(ByVal pRgbColor As IRgbColor) As Color
Return ColorTranslator.FromOle(pRgbColor.RGB)
End Function
#End Region
#Region "将ArcGIS Engine中的IColor接口转换至.NET中的Color结构"
''' <summary>
''' 将ArcGIS Engine中的IColor接口转换至.NET中的Color结构
''' </summary>
''' <param name="pColor">IColor</param>
''' <returns>.NET中的System.Drawing.Color结构表示ARGB颜色</returns>
''' <remarks></remarks>
Public Shared Function ConvertIColorToColor(ByVal pColor As IColor) As Color
Return ColorTranslator.FromOle(pColor.RGB) '特别注意此处不能用Color.FromArgb(pColor.RGB),用该函数得到的将是R与B位置相反的颜色值
End Function
#End Region
注:以上代码用VB.Net编写
COLORREF类型颜色的值COLORREF cr=RGB(123,200,12);
其中的R、G、B三个分量的排列顺序是BGR。
.NET中通过数据类型Color表示颜色,该类有一个函数FromArgb(int,int,int),可以通过输入RGB三个值得到一个Color类型的颜色。同时也有一个ToArgb()函数,得到一个32位的整数值,
32 位 ARGB 值的字节顺序为 AARRGGBB。由 AA 表示的最高有效字节 (MSB) 是 alpha 分量值。分别由 RR、GG 和 BB 表示的第二、第三和第四个字节分别为红色、绿色和蓝色颜色分量
了解了上面的内容,颜色的转换就很简单了。
1、从Color到COLORREF
int nColor = crColor.ToArgb();
int blue = nColor & 255;
int green = nColor >> 8 & 255;
int red = nColor >> 16 & 255;
//注意COLORREF中颜色的排列是 BGR,而通过Color.ToArgb()得到的数值中颜色排列是AARRGGBB
int nColorRef = blue << 16 | green << 8 | red;
2、从COLORREF到Color(注意COLORREF中颜色的排列是BGR,红色分量在最后面)
int red=nColorRef & 255;
int green= nColorRef >> 8 & 255;
int blue= nColor Ref>> 16 & 255;
Color crColor=Color.FromArgb(red,green,blue);
或者直接通过下面的代码:
Color.FromArgb(nColorRef & 255, nColorRef >> 8 & 255, nColor Ref>> 16 & 255);
注:上面的代码使用C#编写。
3 ArcGIS Engine与Win32中Color互转
IColorPtr ipColor;
IRgbColorPtr ipRgbColor;
//IColor转为IRgbColor
ipRgbColor = (IRgbColorPtr)ipColor;
//获取R,G,B
ipRgbColor->get_Red(&R);
ipRgbColor->get_Green(&G);
ipRgbColor->get_Blue(&B); //转为Win32的Color
COLORREF color(RGB(R,G,B));
转自:http://blog.csdn.net/linghe301/article/details/7204954
public class ColorHelper
{
// Fields
private static Random m_random = new Random();
// Methods
public static Color CreateColor(IColor esriColor)
{
Color black = Color.Black;
if (esriColor != null)
{
if (esriColor is IRgbColor)
{
IRgbColor color2 = esriColor as IRgbColor;
black = Color.FromArgb(color2.Red, color2.Green, color2.Blue);
}
else
{
int red = esriColor.RGB % 0x100;
int green = (esriColor.RGB / 0x100) % 0x100;
int blue = ((esriColor.RGB / 0x100) / 0x100) % 0x100;
black = Color.FromArgb(red, green, blue);
}
}
return black;
}
public static IColor CreateColor(Color msColor)
{
return CreateColor(msColor.R, msColor.G, msColor.B);
}
public static IColor CreateColor(int red, int green, int blue)
{
RgbColorClass class2 = new RgbColorClass();
class2.Red = red;
class2.Green = green;
class2.Blue = blue;
return class2;
}
public static IColor CreateColor(byte alpha, int red, int green, int blue)
{
RgbColorClass class2 = new RgbColorClass();
class2.Red = red;
class2.Green = green;
class2.Blue = blue;
class2.Transparency = alpha;
return class2;
}
public static IColor CreateRandomColor()
{
return CreateRandomColor(100);
}
public static IColor CreateRandomColor(byte alpha)
{
int red = m_random.Next(0xff);
int green = m_random.Next(0xff);
int blue = m_random.Next(0xff);
return CreateColor(alpha, red, green, blue);
}
public static List<IColor> CreateRandomColorList(int pCount)
{
List<Color> list = new List<Color>();
List<IColor> list2 = new List<IColor>();
while (list.Count < pCount)
{
Color item = CreateRandomMSColor();
if (!list.Contains(item))
{
list.Add(item);
}
}
foreach (Color color2 in list)
{
list2.Add(CreateColor(color2));
}
return list2;
}
public static Color CreateRandomMSColor()
{
int red = m_random.Next(0xff);
int green = m_random.Next(0xff);
int blue = m_random.Next(0xff);
return Color.FromArgb(red, green, blue);
}
}