In last post we wrote extension method to get complementary color of a given color. In this post we will write some more extension methods to get colors. Co-incidentally India is totally covered in colors because this is the Holi week .

If you want to have a dynamic feel to your app (or some part of your app) you could fill it with random colors. Let’s write following extension methods which return random color with parameterized control over what type of color output you need:

Color.GetRandom – This method returns a random color.

Color.GetRandom(Brightness Control) – This method returns a random color between given brightness.

Color.GetRandomShade – This returns a random color of the Color’s shade.

Color.GetRandomShade(Brightness Control) – It returns a random color of given color between given brightness.

Color.GetPastelShade() – Returns a pastel shade of given color.

Let’s code.

We will utilize the same code from our last post and extend the ColorExtensions class. To implement aforesaid methods, add following code to the said class

static Random randomizer = new Random(); /// <summary> /// Returns a pastel shade of the color /// </summary> /// <param name="source">Source color</param> /// <returns></returns> public static Color GetPastelShade(this Color source) { return (generateColor(source, true, new HSB { H = 0, S = 0.2d, B = 255 }, new HSB { H = 360, S = 0.5d, B = 255 })); } /// <summary> /// Returns a random color /// </summary> /// <param name="source">Ignored(Use RandomShade to get a shade of given color)</param> /// <returns></returns> public static Color GetRandom(this Color source) { return (generateColor(source, false, new HSB { H = 0, S = 0, B = 0 }, new HSB { H = 360, S = 1, B = 255 })); } /// <summary> /// Returns a random color within a brightness boundry /// </summary> /// <param name="source">Ignored (Use GetRandomShade to get a random shade of the color)</param> /// <param name="minBrightness">A valued from 0.0 to 1.0, 0 is darkest and 1 is lightest</param> /// <param name="minBrightness">A valued from 0.0 to 1.0</param> /// <returns></returns> public static Color GetRandom(this Color source, double minBrightness, double maxBrightness) { if (minBrightness >= 0 && maxBrightness <= 1) { return (generateColor(source, false, new HSB { H = 0, S = 1 * minBrightness, B = 255 }, new HSB { H = 360, S = 1 * maxBrightness, B = 255 })); } else { throw new ArgumentOutOfRangeException(); } } /// <summary> /// Returns a random shade of the color /// </summary> /// <param name="source">Base color for the returned shade</param> /// <returns></returns> public static Color GetRandomShade(this Color source) { return (generateColor(source, true, new HSB { H = 0, S = 1, B = 0 }, new HSB { H = 360, S = 1, B = 255 })); } /// <summary> /// Returns a random color within a brightness boundry /// </summary> /// <param name="source">Base color for the returned shade</param> /// <param name="minBrightness">A valued from 0.0 to 1.0, 0 is brightest and 1 is lightest</param> /// <param name="minBrightness">A valued from 0.0 to 1.0</param> /// <returns></returns> public static Color GetRandomShade(this Color source, double minBrightness, double maxBrightness) { if (minBrightness >= 0 && maxBrightness <= 1) { return (generateColor(source, true, new HSB { H = 0, S = 1 * minBrightness, B = 255 }, new HSB { H = 360, S = 1 * maxBrightness, B = 255 })); } else { throw new ArgumentOutOfRangeException(); } } /// <summary> /// Process parameters and returns a color /// </summary> /// <param name="source">Color source</param> /// <param name="isaShadeOfSource">Should source be used to generate the new color</param> /// <param name="min">Minimum range for HSB</param> /// <param name="max">Maximum range for HSB</param> /// <returns></returns> private static Color generateColor(Color source, bool isaShadeOfSource, HSB min, HSB max) { HSB hsbValues = ConvertToHSB(new RGB { R = source.R, G = source.G, B = source.B }); double h_double = randomizer.NextDouble(); double s_double = randomizer.NextDouble(); double b_double = randomizer.NextDouble(); if (max.B - min.B == 0) b_double = 0; //do not change Brightness if(isaShadeOfSource) { min.H = hsbValues.H; max.H = hsbValues.H; h_double = 0; } hsbValues = new HSB { H = Convert.ToDouble(randomizer.Next(Convert.ToInt32(min.H), Convert.ToInt32(max.H))) + h_double, S = Convert.ToDouble((randomizer.Next(Convert.ToInt32(min.S * 100), Convert.ToInt32(max.S * 100)))/100d), B = Convert.ToDouble(randomizer.Next(Convert.ToInt32(min.B), Convert.ToInt32(max.B))) + b_double }; Debug.WriteLine("H:{0} | S:{1} | B:{2} [Min_S:{3} | Max_S{4}]", hsbValues.H,_hsbValues.S,_hsbValues.B, min.S, max.S) ; RGB rgbvalues = ConvertToRGB(_hsbValues); return new Color { A = source.A, R = (byte)_rgbvalues.R, G = (byte)_rgbvalues.G, B = (byte)_rgbvalues.B }; }

You could call these methods like so:

Color randomPurplishPastel = Colors.Purple.GetPastelShade();

Here is an example :

My app uses random colors pretty extensively:

Holi hai!!!