C method for obtaining random colors

  • 2020-12-18 01:53:59
  • OfStack

This article gives an example of how C# gets random colors. Share to everybody for everybody reference. Specific implementation methods are as follows:

public string GetRandomColor()
{
        Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
        //  for C# Random number, nothing to talk about, right
        System.Threading.Thread.Sleep(RandomNum_First.Next(50));
        Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);         //  To display on a white background, try to produce a darker color
        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;
}

Hopefully this article has helped you with your C# programming.


Related articles: