locked
Random colors question ... RRS feed

  • Question

  • User-1110347893 posted

    Hi to all. I am new in programming. 

     I have this problem. I need to generate a new color in my for loop and I don't know in advance the number of iterations that for loop will have. It might be only 2 or 3 iterations, and it could be up to 30 or so. The two neighboring colors should be pretty different for the naked eye. The flash chart is in question. I use a for loop to draw the different categories of that chart and each categorie has a color property that would need to be assigned. Since the better part of the charts have only 3 categories tops could I use one array for them with three distinct colors (ex. blue, red, green) ... and for the chart with more than three categories a random different color? I really can't know the number of the categories upfront and that gives me a headache. How programatically to change that many different colors? I would appreciate any help.

     

    Thanks. 

     

    Here is the part of the code ..

      

    found = false;
                        foreach(FcDataSetNodeMultyCharts elem in graph.getDataSetNodeList())
                        {
                            if(dr["PRODUCTTYPE"].ToString() == elem.seriesName)
                            {
                                found = true;
                                break;
                            }
                        }
                        if(!found)
                        {
                            FcDataSetNodeMultyCharts elemDataSet = new FcDataSetNodeMultyCharts();
                            elemDataSet.seriesName = dr["PRODUCTTYPE"].ToString();
                            elemDataSet.showValues =  FcDataSetNode.yesNoEnum.yes;
                            elemDataSet.color =colors[col%3] + colors[(col+1)%3] + colors[(col+2)%3];
                            col += 2;
                            graph.addDataSetNodeToList(elemDataSet);
                        }
     
     
    Sunday, January 13, 2008 8:31 AM

All replies

  • User-1063239682 posted

    You can use the method System.Drawing.Color.FromArgb(...) and give it the red, green and blue channels.

    Now the channel values should be between 0 and 255, however you need to ensure that you get colors difference from each other, here is what you should do

    1. Generate random values for each channel between 0 and 4 only (inclusive) (you might want to try not to generate zero so as not to get very dark colors and possibly black)

    2. For each value that you have generated (say R: 2 G: 3  B: 4) multiply the value by 64 (to cover the full 256 range)

    3. Use this values in the method fromArgb to generate your colors.

    Since we hare dividing each color into 4 segments then you will get 4*4*4 = 64 unqiue colors which should be good for your purposes. If you need more color difference and less color choices, divide your range into 3 instead of 4.

    Hope this helps.

    Monday, January 14, 2008 5:46 AM
  • User559089557 posted

    This article may help:

    Generating Random Colors in VB.NET

    Wednesday, June 23, 2010 4:36 AM