-1

I'm writing an asp.net application where I have to accomplish following 2 tasks:

  1. Generate the 'n' number of random colors based on user input 'n'(n is the no of colors user wants to generate).

  2. All generated colors should be solid colors.

Note : I should generate solid colors only, because when I override one color(color_new) on top of another color(color_old), the overridden color(color_old) should not be visible any more.

What I have done:

I Have written following code in C#(code behind file) for generating random colors and it is working fine.

 private Color GenerateNewColor()
 {
     var values = Guid.NewGuid().ToByteArray().Select(b => (int)b);
     int red = values.Take(5).Sum() % 255;
     int green = values.Skip(5).Take(5).Sum() % 255;
     int blue = values.Skip(10).Take(5).Sum() % 255;

     return Color.FromArgb(255,red, green, blue);
 }

Here now my question is "How should I ensure that generated color is Solid or not from the above function?"

Thanks in Advance.

Microsoft DN
  • 9,706
  • 10
  • 51
  • 71
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
  • What do you mean by a solid colour? – Jim Jeffries Oct 31 '13 at 13:17
  • by solid you mean no transparency? – Jonesopolis Oct 31 '13 at 13:17
  • You are setting the alpha to 255, therefore they are solid colours. – David Arno Oct 31 '13 at 13:18
  • You have employed an *interesting* way of generating random values BTW. Not sure if I like it or loath it :) – David Arno Oct 31 '13 at 13:19
  • @Jonsey: yes, solid means = no transparency. – Sudhakar Tillapudi Oct 31 '13 at 13:19
  • 3
    Regarding your "randomness", any reason in particular to be generating them based on a GUID's byte value modulos and not using [`Random.Next(256)`](http://msdn.microsoft.com/en-us/library/zd1bc8e5.aspx)? EDIT: Also, do you plan to introduce a method to handle avoiding the random selection of a duplicate colour (however rare that might be) or ensure a minimum level of change (so you don't change from say, black to very very very dark grey) so it isn't _perceived_ as the same colour by the user? – Chris Sinclair Oct 31 '13 at 13:21
  • @Chris: there is no particular reason to use GUID, as i initially thought of using GUID as it is always unique , i have used it.i can use random function also.regarding duplicate colours: yes, i have to still implement some logic to avoid duplicates. Thanks :) – Sudhakar Tillapudi Oct 31 '13 at 13:26
  • @Sudhakar: In that case, I would suggest using the [Random class](http://msdn.microsoft.com/en-us/library/system.random.aspx) then whenever you need to generate random numbers. It's what it was specifically designed for (unlike GUID), and will offer a good guarantee of random _distribution_ unlike GUID which may have repeating patterns or distributions that skew towards one direction or another (for example, some bits of it represent a version number which are, IIRC, always the same). Plus most .NET programmers are familiar with how to use the Random class; its use will _expected_. – Chris Sinclair Oct 31 '13 at 13:32
  • @Chris: Thanks a lot Chris,actually I thought it's in reverse order like: GUID generates unique always compared to Random(),now I got it. sure now I will change my code by implementing Random() instead of GUID. Thank You. – Sudhakar Tillapudi Oct 31 '13 at 13:40
  • @Sudhakar: You're welcome. Just watch out for [this common pitfall](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) when using it in tight loops. (so in your usage here, you may want to consider declaring a single instance of `Random` on the class to use in the method; do account for thread-safety if applicable) – Chris Sinclair Oct 31 '13 at 13:42
  • @Chris: yes I will create singleton object of Random class and also take care of thread safety.thanks for guiding me towards better coding techniques :) – Sudhakar Tillapudi Oct 31 '13 at 14:15

1 Answers1

7

Unless you have a weird definition of "solid color", any color generated by FromArgb with the alpha channel at 255 is solid.

Medinoc
  • 6,577
  • 20
  • 42
  • Thanks Medinoc , from the link http://en.wikipedia.org/wiki/RGBA_color_space i got to know that FromArgb() function first parameter specifies transparency from 0% to 100%. if the value is 255 it is 100%, hence it is solid colour :) Thanks – Sudhakar Tillapudi Oct 31 '13 at 13:35