3

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;
    }
Ali Tor
  • 2,772
  • 2
  • 27
  • 58
  • 1
    In that case, you better don't use random, but some sort of pattern – Ian May 15 '16 at 03:24
  • 2
    See [here](http://stackoverflow.com/questions/27374550/how-to-compare-color-object-and-get-closest-color-in-an-color/27375621?s=1|1.0441#27375621) for a discussion of Color Distance. Also do look at the [`KnownColors`](https://msdn.microsoft.com/en-us/library/system.drawing.knowncolor(v=vs.110).aspx)! – TaW May 15 '16 at 06:56
  • 1
    There's some ideas here http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ and here http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette – OldBoyCoder May 15 '16 at 10:10
  • Thanks, @OldBoyCoder the article explains the problem well and gives a good solution – Ali Tor May 15 '16 at 11:45
  • Depending on how many colors you need, you could just hardcode them. Cynthia Brewer's [ColorBrewer](http://colorbrewer2.org/) is widely used for generating easily-distinguishable color schemes for the presentation of data. Pick the nature of your data, pick the number of data classes, and finally pick a color scheme/palette of your preference. Copy those hex values, store them in a list or array, profit! – Cody Gray - on strike Jul 09 '16 at 17:31

1 Answers1

1

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;
    }
}
Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152