I normally use the following code to generate random colors:
Random rand = new Random();
Color random = Color.FromArgb((int)(rand.NextDouble() * 255),
(int)(rand.NextDouble() * 255),
(int)(rand.NextDouble() * 255));
But most of them look like a variation of gray. How can I restrict the output to only fully saturated colors?
Thanks.
Theory
Full Saturation (as used by HSL and similar colour schemes) in effect means that you have one RGB value at full, one at 0 and one at any other value.
The reason for this is that the saturation is based on the difference between the highest and lowest colour components and is highest when they are at the extremes. The actual definitions are more complicated (and involve the luminance) but it is sufficient to say that a component of 0 and another of 1 will give max saturation.
Algorithm 1
This leads to a relatively simple way of doing it.
This will give you a maximum saturation colour relatively simply.
For implementation it is probably easiest to generate a random number 1 to 6 for the 6 possible choices of which component you assign the 0, 1 and random element to and then use a switch of some kind.
This is the easiest algorithm but not necessarily the easiest implementation due to the choices/branching.
Algorithm 2
Second method as suggested by Jim Mischel based on a similar theory but just implemented a bit differently.
This has the same effect of setting one value to 1, one to 0 and one to a random value. It has the advantage that it doesn’t require you to use the messy switch statement but you might end up with some messy loops instead. Also depending on the precision of your components (eg if you go straight in with bytes) then if your mid value is actually equal to your top or bottom (or all three are the same) then this might get reset too depending on how you code your algorithm. This will mainly have the effect of skewing the randomness but this is not likely to be significantly noticeable.
Code implementation for method two also courtesy of Jim