using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace Yw.WinFrmUI.Phart
|
{
|
/// <summary>
|
/// 随机色辅助类
|
/// </summary>
|
public class RandomColorHelper
|
{
|
|
private static readonly List<Color> _color_array = new List<Color>()
|
{
|
Color.Red, Color.Blue, Color.Green,Color.DodgerBlue,
|
Color.Fuchsia, Color.MidnightBlue, Color.Maroon, Color.Aquamarine,
|
Color.Bisque,Color.BurlyWood
|
};
|
|
/// <summary>
|
/// 获取随机颜色
|
/// </summary>
|
/// <returns></returns>
|
public static Color Get(int count)
|
{
|
if (count < _color_array.Count)
|
{
|
return _color_array[count];
|
}
|
|
//var _random = new Random();
|
//int r = _random.Next(1, 256);
|
//int g = _random.Next(1, 256);
|
//int b = _random.Next(1, 256);
|
//return Color.FromArgb(r, g, b);
|
|
return GetRandomDarkColor();
|
}
|
|
|
private static Color GetRandomDarkColor()
|
{
|
Random random = new Random();
|
int r, g, b;
|
do
|
{
|
r = random.Next(256);
|
g = random.Next(256);
|
b = random.Next(256);
|
} while (CalculateBrightness(r, g, b) > 128); // 确保颜色是深色
|
|
return Color.FromArgb(r, g, b);
|
}
|
|
private static int CalculateBrightness(int r, int g, int b)
|
{
|
// 计算颜色的亮度,亮度值越低,颜色越深
|
return (r + g + b) / 3;
|
}
|
|
}
|
}
|