简体   繁体   中英

Generate Unique Random prominently color?

I am trying to generate Unique and prominently random colors in c# or vb.net. I am using below code now. But this code generating color almost same color, not big change.

How can unique kind of colors?

  Private Function GetRandomColor() As Color
        Dim RandGen As New Random()
        Return Color.FromArgb(RandGen.Next(70, 200), RandGen.Next(100, 225), 
        RandGen.Next(100, 230))
    End Function

Its more than likely because you are creating a new instance of Random everytime. Try moving the instance outside of the method, such as at class level, and see if that helps

ie

  Dim RandGen As New Random()

  Private Function GetRandomColor() As Color
        Return Color.FromArgb(RandGen.Next(70, 200), RandGen.Next(100, 225), 
        RandGen.Next(100, 230))
    End Function

尝试随机选择一个较低的RGB值(0-100)。

You would be best using a class that generates HSL colours, that way it is easier to control prominence. There are a few class out there

Such as...

http://www.geekymonkey.com/Programming/CSharp/RGB2HSL_HSL2RGB.htm

or

http://richnewman.wordpress.com/hslcolor-class/

Having control over the luminosity/saturation means you can get vibrant colours at any hue..

Using richnewmans class...

var c = new HSLColor(new Random(0,100),80,80);

we only set a random value in the hue and the sat and lum can be fixed at whatever looks best

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM