This C# struct
provides a way to specify (and mutate) colors in the C# language. With System.Drawing
, we can access Color and reuse logic.
We can access known colors, convert colors to integers, parse colors, and generally make the world a brighter place with this type.
Let us examine a program that uses a named color property. It then accesses several instance properties on the variable and also calls methods.
using System; using System.Drawing; // Get the AliceBlue color. Color color = Color.AliceBlue; // Write some details about the color. Console.WriteLine(color); Console.WriteLine(color.ToArgb()); Console.WriteLine(color.A); Console.WriteLine(color.R); Console.WriteLine(color.G); Console.WriteLine(color.B); Console.WriteLine(color.GetBrightness()); Console.WriteLine(color.GetHue()); Console.WriteLine(color.GetSaturation());Color [AliceBlue] -984833 255 240 248 255 0.9705882 208 1
These are colors used in the UI. It is possible to query the system for the color of UI elements, like menu text (KnownColor.MenuText
).
using System; using System.Drawing; // You can see what the menu text color is. Color color = Color.FromKnownColor(KnownColor.MenuText); // All values are zero so it is black. Console.WriteLine(color.R); Console.WriteLine(color.G); Console.WriteLine(color.B);0 0 0
It is possible to represent colors as an integer that represents the byte
values of the ARGB values. You can persist the ARGB integer to text files or databases to save storage space.
byte
—the 4 bytes together constitute a single 32-bit integer.ToArgb()
and FromArgb()
methods are used to go back and forth.using System.Drawing; // Get ARGB of black. int argb = Color.Black.ToArgb(); // Get the color black by using the argb integer. Color color = Color.FromArgb(argb);
FromName
In some cases, you may have to convert from a string
to a color type. The word "white" can be converted into a real color with the Color.FromName
method.
using System; using System.Drawing; // Create the color from a string. Color white = Color.FromName("white"); // Color will have appropriate R G B values. Console.WriteLine(white.R); Console.WriteLine(white.G); Console.WriteLine(white.B);255 255 255
The Color type is a struct
type, and so cannot be set to null
. Instead of null
, use the Color.Empty
value—then use the IsEmpty
property to test this.
using System; using System.Drawing; // Color is a struct so it cannot be null. // ... Use Color.Empty instead. Color color = Color.Empty; Console.WriteLine(color.IsEmpty); color = Color.Black; Console.WriteLine(color.IsEmpty);True False
A common place to use the Color type is Windows Forms and WPF. Forms require the Color type for when you want to set their backgrounds and foregrounds.
Color.FromName
method to convert the string
value into an actual color. We then set the form's background to that color.using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication16 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // You can assign colors to the BackColor property in Windows Forms. this.BackColor = Color.FromName("green"); } } }
There are many different known and named colors in .NET. These are accessible through static
properties on the Color type.
By providing color methods, the Color type provides important functionality in .NET. It is found in the System.Drawing
namespace works in .NET 6.0 on all platforms.