2

I want to generate 12 different visible Brush Colors in WPF in my code behind and the number of colors which is initially 12 may vary as the application evolves i.e. I want to generate as many different visible Brush Colors depending on a given count?

I would explain it a little more:

I am creating Rectangles in a for loop and for each rectangle created at run time I have to assign a Fill Color e.g.

    for (i=0; i<12; i++)
    {
        Rectangle rect = new Rectangle();
        rect.Fill = <I want to assign a unique visible color>;
        rect.Stroke = Brushes.Black;
        rect.StrokeThickness = 1;
    }
S2S2
  • 8,322
  • 5
  • 37
  • 65

3 Answers3

1

What you probably need is an RGB to HSL, and HSL to RGB converter. You can then divide the total hue (usually represented as degrees in a circle, but sometimes a percent value) by the number of colors required. Incrementing the hue value by the segment amount should produce the most differentiated colors possible.

Most examples use the WinForms Color object since it was able to provide H S and L values. There are lots of online examples:

https://web.archive.org/web/20141023005253/http://bobpowell.net/RGBHSB.aspx

how to use HSL in Asp.net

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
mbursill
  • 2,911
  • 1
  • 32
  • 44
0

Brushes can be assigned colors, This SO question should help you in getting the colors, and then assign them each time you create a new brush for any number of brushes.

Just for reference:

Brush Class

Brushes class

Community
  • 1
  • 1
SpeedBirdNine
  • 4,610
  • 11
  • 49
  • 67
  • 1
    Thanks SpeedBirdNine..I have added a for loop example to more clearly explain my problem..!! – S2S2 Sep 24 '11 at 21:47
0

Use a random number generator to create the RGB triple for the colour. Save it in a list. Then the next time round the loop check the newly generated colour against the list. If it's not in the list use it, if it is choose again.

Potentially this could run into trouble if you have a lot of colours so you're more and more likely to hit an existing colour, but for 12 (or so) colours it should be OK.

Alternatively create a list of 100's of colours and remove each one from the list when it's picked randomly. This will ensure you don't get any clashes but would require you extend the list if you needed more colours.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • I think a problem with this approach is that two colors may have slightly different RGB values, while still looking exactly the same to humans. – svick Sep 25 '11 at 12:26
  • ChrisF, I think the alternative approach is better with instead of loading only 100 Colors, load all the available colors using Reflection or similar approach with each Color assigned a unique index and then while accessing we can give the index and get the Color. Howz that, is it an OK solution? – S2S2 Sep 25 '11 at 15:49
  • @svick - agreed. I think you'd need to use some "neighbouring" colour algorithm. – ChrisF Sep 25 '11 at 18:00
  • @Vijay - I wouldn't choose all colours for the reason svick mentioned. – ChrisF Sep 25 '11 at 18:01