How to generate random color names in C#

后端 未结 15 1688
清酒与你
清酒与你 2020-11-29 21:57

I need to generate random color names e.g. \"Red\", \"White\" etc. How can I do it? I am able to generate random color like this:

Random randonGen = new Ran         


        
相关标签:
15条回答
  • 2020-11-29 22:37

    Or you could try out this: For .NET 4.5

    public Windows.UI.Color GetRandomColor()
    {
                Random randonGen = new Random();
                Windows.UI.Color randomColor = 
                    Windows.UI.Color.FromArgb(
                    (byte)randonGen.Next(255), 
                    (byte)randonGen.Next(255),
                    (byte)randonGen.Next(255), 
                    (byte)randonGen.Next(255));
                return randomColor;
    }
    
    0 讨论(0)
  • 2020-11-29 22:37
    private string getRandColor()
            {
                Random rnd = new Random();
                string hexOutput = String.Format("{0:X}", rnd.Next(0, 0xFFFFFF));
                while (hexOutput.Length < 6)
                    hexOutput = "0" + hexOutput;
                return "#" + hexOutput;
            }
    
    0 讨论(0)
  • 2020-11-29 22:37

    generate a random number and cast it to KnownColor Type

    ((KnownColor)Random.Next());
    
    0 讨论(0)
  • 2020-11-29 22:41

    Sounds like you just need a random color from the KnownColor enumeration.

    0 讨论(0)
  • 2020-11-29 22:41

    If you wanted to get random color from specific colors list,
    then try below one

    Color[] colors = new[] { Color.Red, Color.Blue, Color.FromArgb(128, 128, 255) };    //Add your favorite colors list
    Random randonGen = new Random();
    Color randomColor = colors[Generator.Next(0, colors.Length)];    //It will choose random color from "colors" array
    
    0 讨论(0)
  • 2020-11-29 22:42

    To clear up the syntax errors in the original question, it's

    Color.FromRgb, and

    (Byte)random2.Next(255)

    converts the integer to a byte value needed by Color:

        Random random2 = new Random();
        public int nn = 0x128;
        public int ff = 0x512;
        Color randomColor = Color.FromRgb((Byte)random2.Next(nn, ff), (Byte)random2.Next(nn, ff), (Byte)random2.Next(nn, ff));
    
    0 讨论(0)
提交回复
热议问题