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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With