Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Randomly Generate Unique Colors?

Tags:

c#

I'm using a graphing package that can draw lines of color (255,255,255). So basically what I'm doing is (Random.Next(0,255),Random.Next(0,255),Random.Next(0,255)) to generate a color each time a line is added.

This is all well and good, but sometimes, I get colors that look very similar, making it difficult for the user to discern which data corresponds to which line.

Is there a more clever way to generate random and unique colors in the (255,255,255) format?

like image 823
sooprise Avatar asked Nov 30 '22 10:11

sooprise


2 Answers

A better option is to typically generate a random hue, and the convert the hue to an RGB color using an HSL or HSV color (with that hue). By using a random "hue" instead of random color, you'll get much more variation in your colors. You can also randomize the other components (saturation/value, etc), if you need even more variation.

See Wikipedia for details on working in colors using HSV/HSL, including how to convert HSV to RGB.

like image 193
Reed Copsey Avatar answered Dec 03 '22 00:12

Reed Copsey


Check the "color distance"

Assume RGB are XYZ coordinates, do a 3D distance calculation. If a color isn't at least N away from all previously generated colors, try again.

N is a value you decide.

like image 22
Neil N Avatar answered Dec 02 '22 23:12

Neil N