James123
James123

Reputation: 11652

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

Upvotes: 1

Views: 4401

Answers (3)

David
David

Reputation: 8650

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

Upvotes: 3

001
001

Reputation: 13533

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

Upvotes: 2

Iain Ward
Iain Ward

Reputation: 9936

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

Upvotes: 2

Related Questions