In this article, we will learn how to generate a random color name using the KnownColor enumeration. This enum is particularly useful in UI design and data visualization, as it provides a wide variety of preset colors.

To download the source code for this article, you can visit our GitHub repository.

Let’s begin.

Understanding the KnownColor Enumeration

To understand why we would even need to generate a random color name, let’s imagine we are creating an application that analyzes stock market trends. To be able to visually differentiate and compare the stocks via a line graph, we would benefit from having different colors for them. Thanks to the .NET KnownColor enumeration, we wouldn’t need to manually assign a line color to every known stock tracked by our application. Instead, we can use this enum to randomly generate unique line colors representing the individual stock and its fluctuations in price. 

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

That said, we first need to understand what the KnownColor enumeration is. It’s essentially a list of all the preset colors offered by .NET. Also, let’s be clear about what an enumeration (i.e. enum) is. It’s a specific type in C# designed to hold a set of named constants. So, just as the bool type contains the values true and false, the KnownColor enum contains 175 possible color values, such as AntiqueWhite, CornflowerBlue and LavenderBlush, to name a few.

To learn more about enumerations, check out our other article C# Intermediate – Enumerations.

Now, let’s see how to generate a random color name using the KnownColor enum.

Creating a Random Color Name Generator

Before we begin, it’s important to include the using directive for System.Drawing. With this directive in our code, we have access to several classes and objects related to drawing, including the KnownColor enum. 

Now that we have System.Drawing, we can create our KnownColorGenerator class:

public static class KnownColorGenerator
{
    private static readonly KnownColor[] _allColors = Enum.GetValues<KnownColor>();
}

Here, our KnownColorGenerator class will manage the generation of random colors from the KnownColor enum. Within the class, we initialize a static readonly array of KnownColor objects via the Enum.GetValues<KnownColor>(). Enum.GetValues<T>() returns an array containing all possible values for an enum.

Now, within our class, we can create the method that will generate a random color:

public static Color GetRandomKnownColor()
{
    int index = Random.Shared.Next(_allColors.Length);
    KnownColor randomColorName = _allColors[index];

    return Color.FromKnownColor(randomColorName);
}

Here, the GetRandomKnownColor() method produces a random KnownColor by selecting an index from the _allColors array using Random.Shared.

To learn more about the Random class in C#, see this article Random Class in C#.

First, we use Random.Shared.Next(_allColors.Length) to generate a random index, which produces a number between 0 and one less than the length of the array. We then use this index retrieve a KnownColor from our _allColors array.

Lastly, we return a Color based on our randomly chosen KnownColor. The Color.FromKnownColor() method is a static function in the System.Drawing namespace, which transforms a KnownColor value into its corresponding Color object.

Generating and Displaying a Random Color Name

Now, we can finally get a random color in our Program class:

Color randomColor = KnownColorGenerator.GetRandomKnownColor();

Console.WriteLine("Random Known Color: " + randomColor.Name);

Here, we call our static method GetRandomKnownColor() to generate a random color. Then, we store the resulting Color object in our variable randomColor. To verify our work, we print out the Name property of the random color we received from our generator.

As expected, in our console output, we see the color name that our generator gave us:

Random Known Color: MistyRose

Of course, the color will vary every time we run the code.

Conclusion

In this article, we saw how to generate a random color name using the KnownColor enumeration. There are many applications and use cases for this technique. Generating a random color could be useful in data visualization, interactive dashboards, or even in character customization.

Did we miss any interesting scenario where generating a random color could be useful? Feel free to share ideas in the comments below.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!