How to generate random color names in C#

后端 未结 15 1795
清酒与你
清酒与你 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:51

    Ignore the fact that you're after colors - you really just want a list of possible values, and then take a random value from that list.

    The only tricky bit then is working out which set of colors you're after. As Pih mentioned, there's KnownColor - or you could find out all the public static properties of type Color within the Color structure, and get their names. It depends on what you're trying to do.

    Note that randomness itself can be a little bit awkward - if you're selecting multiple random colors, you probably want to use a single instance of Random`. Unfortunately it's not thread-safe, which makes things potentially even more complicated. See my article on randomness for more information.

提交回复
热议问题