locked
How to create an array of colors RRS feed

  • Question

  • User1090916182 posted

    I'm not sure whether this post should be in the System.Drawing topic, but here goes...

    I need to create an array of colors to be applied into a Dundas Chart.  The chart code is not the issue, but I can't seem to create an array of colours.

    Here's what I need to be able to do.

    1. Initialise an array (of say, 10 items)
    2. Manually add five colours to the array based on values I choose (e.g. FromHtml("#0000cc"))
    3. For the remaining array items, randomly create colors

    Therefore within the Dundas Chart I can refer the chart to my array of colors like so...

    For i as integer = 0 to myChart.Series.Count - 1
    myChart.series(i).Color = myColorArray(i)
    Next

    I would appreciate some pointers on how to do achieve this please!

    Tuesday, January 5, 2010 12:22 PM

Answers

  • User-1360095595 posted

    Not sure what you need help with specifically, but maybe this will help (not a VB person, so try the link in my signature to translate):

    Color[] colors = new Color[5];
    colors[0] = ColorTranslator.FromHtml("#1efefe");
    colors[1] = ColorTranslator.FromHtml("#2efefe");
    colors[2] = ColorTranslator.FromHtml("#3efefe");
    colors[3] = ColorTranslator.FromHtml("#4efefe");
    colors[4] = ColorTranslator.FromHtml("#5efefe");
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 5, 2010 1:32 PM
  • User42760572 posted

    just use generics i.e. Generics.List(Of Color), this way u'll avoid boxing - unboxing performance issues and its as easy

    as an arraylist to work with.

    Dim lst as new List(Of Color)

    lst.Add(Color.FromHTML("....."))


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 6, 2010 6:37 AM

All replies

  • User-1360095595 posted

    Not sure what you need help with specifically, but maybe this will help (not a VB person, so try the link in my signature to translate):

    Color[] colors = new Color[5];
    colors[0] = ColorTranslator.FromHtml("#1efefe");
    colors[1] = ColorTranslator.FromHtml("#2efefe");
    colors[2] = ColorTranslator.FromHtml("#3efefe");
    colors[3] = ColorTranslator.FromHtml("#4efefe");
    colors[4] = ColorTranslator.FromHtml("#5efefe");
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 5, 2010 1:32 PM
  • User1090916182 posted

    Hi.  Thanks for your response.

    Is it possible to modify your example to use an ArrayList?  I was just thinking that it would make the array easier to work if at the number of colours needed might vary somewhat.

    Also, it is possible to create colours randomly to add to what you've already shown me?

    Thanks again, much appreciated  Smile

    Wednesday, January 6, 2010 6:24 AM
  • User-1360095595 posted

    Yes, it is possible.  You can wait for someone else to give you an answer, or challenge yourself and try to find it on your own.  You can make use of the Random class to generate random numbers; and you can follow this link for use of ArrayList: http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx

    Wednesday, January 6, 2010 6:32 AM
  • User42760572 posted

    just use generics i.e. Generics.List(Of Color), this way u'll avoid boxing - unboxing performance issues and its as easy

    as an arraylist to work with.

    Dim lst as new List(Of Color)

    lst.Add(Color.FromHTML("....."))


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 6, 2010 6:37 AM
  • User1090916182 posted

    I'm comfortable with using ArrayLists, and wasn't aware of the generic list, which is one to remember for next time.  Thank you both for your pointers.  Here's what I end up with, which works beautifully...

    Dim myPalette As New Generic.List(Of Color)
    myPalette.Add(ColorTranslator.FromHtml("#990000"))
    myPalette.Add(ColorTranslator.FromHtml("#009900"))
    myPalette.Add(ColorTranslator.FromHtml("#000099"))
    myPalette.Add(ColorTranslator.FromHtml("#480099"))
    myPalette.Add(ColorTranslator.FromHtml("#006899"))
    myPalette.Add(ColorTranslator.FromHtml("#009977"))
    myPalette.Add(ColorTranslator.FromHtml("#4c9900"))
    myPalette.Add(ColorTranslator.FromHtml("#996500"))
    myPalette.Add(ColorTranslator.FromHtml("#643e90"))
    myPalette.Add(ColorTranslator.FromHtml("#55903e"))
    If myPalette.Count < Chart1.Series.Count Then
    Dim r As New Random
    While myPalette.Count <= Chart1.Series.Count - 1
    myPalette.Add(Color.FromArgb(r.Next(200), r.Next(200), r.Next(200)))
    End While
    End If
    For i As Integer = Chart1.Series.Count - 1 To 0 Step -1
    Chart1.Series(i).Color = myPalette(i)
    Next



    Wednesday, January 6, 2010 7:38 AM
  • User1090916182 posted

    Hmmm.... that code looked okay in the preview... Undecided


    Wednesday, January 6, 2010 7:39 AM