Skip to main content
 首页 » 编程设计

C#取得随机颜色的方法(转)

2022年07月19日137leader

public System.Drawing.Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
// 对于C#的随机数,没什么好说的
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);

// 为了在白色背景上显示,尽量生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;13
return System.Drawing.Color.FromArgb(int_Red, int_Green, int_Blue);
}

public string GetRandomColor()
{
        Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
        //  对于C#的随机数,没什么好说的
        System.Threading.Thread.Sleep(RandomNum_First.Next(50));
        Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);

        //  为了在白色背景上显示,尽量生成深色
        int int_Red = RandomNum_First.Next(256);
        int int_Green = RandomNum_Sencond.Next(256);
        int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
     return Color.FromArgb(int_Red, int_Green, int_Blue).Name;
}

https://www.cnblogs.com/marblemm/p/7083408.html


本文参考链接:https://www.cnblogs.com/xihong2014/p/15828935.html