C# 获取System.Color 中所有颜色

System.Color 中的全部颜色提取出来,经过简单筛选后打乱顺序,做成随机颜色数组。 

//用于存取取出的颜色对象 
List colorList = new List(); 
//通过GetMember获取所有的公共成员 
foreach (var item in typeof(Color).GetMembers()) 
{ 
	//只取属性且为属性中的已知Color,剔除byte属性以及一些布尔属性等(A B G R IsKnownColor Name等) 
	if (item.MemberType == System.Reflection.MemberTypes.Property 
	    && System.Drawing.Color.FromName(item.Name).IsKnownColor == true) 
	{ 
		Color color = System.Drawing.Color.FromName(item.Name); 
		double r = color.R, g = color.G, b = color.B;
		double d = (r + g + b) / 3.0;
		double s = Math.Sqrt(((r - d) * (r - d) + (g - d) * (g - d) + (b - d) * (b - d)) / 2);
		if (d < 225 && s > 15)  // 简单的颜色筛选
			colorList.Add(color);
	} 
} 

// 打乱顺序做成随机颜色
Color[] colors = new Color[colorList.Count];
Random rnd = new Random();
int n = rnd.Next(10, colorList.Count);
for (int i = 0; i < GlobalRes.colors.Length;)
{
	int c = colorList.Count / n;
	for (int j = 0; j < c; j++)
	{
		int k = (j + 1) * n;
		if (k < colorList.Count)
		{
			GlobalRes.colors[i++] = colorList[k];
			colorList.RemoveAt(k);
		}
	}
	if (colorList.Count > 20)
		n = rnd.Next(10, colorList.Count);
	else
	{
		for (int j = 0; j < colorList.Count; j++)
		{
			GlobalRes.colors[i++] = colorList[j];
		}
	}
}

C# 获取System.Color 中所有颜色_第1张图片

你可能感兴趣的:(C#,c#,开发语言)