Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:50:23+00:00 2026-05-27T10:50:23+00:00

I normally use the following code to generate random colors: Random rand = new

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:50:24+00:00Added an answer on May 27, 2026 at 10:50 am

    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.

    1. Generate a random number between 0 and 1.
    2. Assign this number randomly to one fo the R, G and B elements.
    3. Randomly assign zero to one of the remaining elements.
    4. set the final element to 1.

    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.

    1. Generate random values for each of the R, G and B components
    2. Find the max component.
    3. Find the min component.
    4. Set max component to 1.
    5. Set min component to 0.

    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

        int[] rgb = new int[3];
        rgb[0] = rnd.Next(256);  // red
        rgb[1] = rnd.Next(256);  // green
        rgb[2] = rnd.Next(256);  // blue
    
        // Console.WriteLine("{0},{1},{2}", rgb[0], rgb[1], rgb[2]);
    
        // find max and min indexes.
        int max, min;
    
        if (rgb[0] > rgb[1])
        {
            max = (rgb[0] > rgb[2]) ? 0 : 2
            min = (rgb[1] < rgb[2]) ? 1 : 2;
        }
        else
        {
            max = (rgb[1] > rgb[2]) ? 1 : 2;
            int notmax = 1 + max % 2;
            min = (rgb[0] < rgb[notmax]) ? 0 : notmax;
        }
        rgb[max] = 255;
        rgb[min] = 0;
    
        // Console.WriteLine("{0},{1},{2}", rgb[0], rgb[1], rgb[2]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I normally use the following code in SQL Server: IF EXISTS (SELECT * FROM
Normally, I use the following code to serialize object to XML file. Everyday, I
I would like to use the following code in C# but I just can't
normally on would use the following :- aspx page:- <a ID=a1 href=javascript:void(0);>Link1 </a> code
I normally use the following idiom to check if a String can be converted
I normally use scp to copy stuff, but now I'm trying to get used
I normally use gVim for editing, but I sometimes use vim when remotely connecting
Normally I use imagecreatefromjpeg() and then getimagesize() , but with Firefox 3 I need
Normally in an ASP.NET view one could use the following function to obtain a
I use the following code in my app when user click on a button

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.