I want to generate random colors for a graphic chart but what I tried is not recognizable and generated color are similar to each other generally. Is it possible to create such colors?
My method:
public Color[] GenerateColors(int n)
{
Color[] colors = new Color[n];
for (int i = 0; i < n; i++)
{
int R = rnd.Next(0, 250);
int G = rnd.Next(0, 250);
int B = rnd.Next(0, 250);
Color color = Color.FromArgb(R, G, B);
colors[i] = color;
}
return colors;
}
Here is a RandomColors class that combines the suggestions from Taw and OldBoyCoder. Based on how many color you want to generate the method Generate either picks from the KnownColors or generates random new colors where it used the last color as a mix color.
public class RandomColors
{
static Color lastColor = Color.Empty;
static KnownColor[] colorValues = (KnownColor[]) Enum.GetValues(typeof(KnownColor));
static Random rnd = new Random();
const int MaxColor = 256;
static RandomColors()
{
lastColor = Color.FromArgb(rnd.Next(MaxColor), rnd.Next(MaxColor), rnd.Next(MaxColor));
}
public static Color[] Generate(int n)
{
var colors = new Color[n];
if (n <= colorValues.Length)
{
// known color suggestion from TAW
// https://stackoverflow.com/questions/37234131/how-to-generate-randomly-colors-that-is-easily-recognizable-from-each-other#comment61999963_37234131
var step = (colorValues.Length-1) / n;
var colorIndex = step;
step = step == 0 ? 1 : step; // hacky
for(int i=0; i<n; i++ )
{
colors[i] = Color.FromKnownColor(colorValues[colorIndex]);
colorIndex += step;
}
} else
{
for(int i=0; i<n; i++)
{
colors[i] = GetNext();
}
}
return colors;
}
public static Color GetNext()
{
// use the previous value as a mix color as demonstrated by David Crow
// https://stackoverflow.com/a/43235/578411
Color nextColor = Color.FromArgb(
(rnd.Next(MaxColor) + lastColor.R)/2,
(rnd.Next(MaxColor) + lastColor.G)/2,
(rnd.Next(MaxColor) + lastColor.B)/2
);
lastColor = nextColor;
return nextColor;
}
}
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