-1

The program is currently filling out charts with colours chosen for it, once the chart exceeds the limit of colours given, it picks a colour at random.

Is there a viable way of acquiring the colour without it being of the same shade as the previous ones?

Code:

public List<string> getColors(int columns)
{
    List<string> colors = new List<string>();
    colors.Add("\"rgba(77,77,77,0.8)\"");
    colors.Add("\"rgba(241,88,84,0.8)\"");
    colors.Add("\"rgba(93,165,218,0.8)\"");
    colors.Add("\"rgba(96,189,104,0.8)\"");
    colors.Add("\"rgba(222,207,63,0.8)\"");
    colors.Add("\"rgba(178,118,178,0.8)\"");
    colors.Add("\"rgba(187,253,241,0.8)\"");
    colors.Add("\"rgba(178,145,47,0.8)\"");
    if (columns > colors.Count)
    {
        Color c = GetRandomColour();
        colors.Add(String.Format("\"rgba({0},{1},{2},0.8)\"", 
                   c.R.ToString(), c.G.ToString(), c.B.ToString()));
    }
    return colors;
}

private static readonly Random rand = new Random();

private Color GetRandomColour()
{
    return Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
}
user7943
  • 803
  • 7
  • 20
  • 2
    What does the same shade mean to you? Is (100,100,100) a different shade from (100,100,101)? – Enigmativity Jul 19 '17 at 05:46
  • 1
    If you want to generate the similar colours I would recommend using a HSV palete, because you can control specific variables, ang generate a similar shade by moving (hue just a little bit), moving saturation and darknes a by a bit more – Ernis Jul 19 '17 at 05:52
  • 1
    Fill an array with a selection of suitable colors (more than just the eight you have, making sure each color is sufficiently different from the others), shuffle that array, and select them from that array. See marked duplicate for shuffling the array. If you just try to pick colors directly and compare them with the ones you already picked to ensure no duplicates or close shades, you're going to waste a lot of CPU time picking colors. – Peter Duniho Jul 19 '17 at 06:02
  • If you don't even already have an idea of what counts as a different enough shade or how to generate colors with sufficiently different shades, then you've got a whole other question, but one with is far too broad. You'll need to be a lot more specific about the constraints and requirements in your question. See other marked duplicates for inspiration. – Peter Duniho Jul 19 '17 at 06:05
  • It in fact isn't a duplicate as I don't really want it to be performed in an array. In any case, what I meant was that I don't particularly want the chosen random colour to be fairly similar to the previous ones. For example; I would rather not have two of the same colours and colour shades in the same chart. – user7943 Jul 19 '17 at 06:12
  • for bright colors, comparing the [`Color.GetHue`](https://msdn.microsoft.com/en-us/library/system.drawing.color.gethue) values can be a good estimate, but in your case probably comparing the `Color.GetSaturation` and `Color.GetBrightness` too – Slai Jul 19 '17 at 16:51

2 Answers2

0

You can implement something like this

public List<string> getColors(int columns)
    {
        List<string> colors = new List<string>();
        colors.Add("\"rgba(77,77,77,0.8)\"");
        colors.Add("\"rgba(241,88,84,0.8)\"");
        colors.Add("\"rgba(93,165,218,0.8)\"");
        colors.Add("\"rgba(96,189,104,0.8)\"");
        colors.Add("\"rgba(222,207,63,0.8)\"");
        colors.Add("\"rgba(178,118,178,0.8)\"");
        colors.Add("\"rgba(187,253,241,0.8)\"");
        colors.Add("\"rgba(178,145,47,0.8)\"");
        if (columns > colors.Count)
        {
            while (true)
            {
                Color c = GetRandomColour();
                string cs = String.Format("\"rgba({0},{1},{2},0.8)\"", c.R.ToString(), c.G.ToString(), c.B.ToString());
                if (!colors.Contains(cs))
                {
                    colors.Add(cs);
                    break;
                }
            }
        }
        return colors;
    }
Ben
  • 763
  • 1
  • 5
  • 14
0

You could initialize your colour List outside of the List method

List<string>colors = null
public List<string> getColors(int columns)
    {
        colors = new List<string>();

Then you could change your Random color function to match your purpose by maybe going through a while loop to see if the random color is already in the given list.

    private Color GetRandomColour()
    {
        Color coltemp = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
        while (colors.Contains(coltemp) 
        {
            coltemp = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
        }
        return coltemp;
    }
Flomax
  • 1