开发者

Generate Unique Random prominently color?

开发者 https://www.devze.com 2023-02-26 16:38 出处:网络
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.

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.开发者_JAVA技巧Next(100, 230))
    End Function


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


Try randomly choosing one of the RGB values to be low (0-100).


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

i.e.

  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
0

精彩评论

暂无评论...
验证码 换一张
取 消