Some notes. We can access known colors, convert colors to integers, parse colors, and generally make the world a brighter place with this type.
First example. Let us examine a program that uses a named color property. It then accesses several instance properties on the variable and also calls methods.
Tip Colors can be represented as ARGB values, which store the alpha transparency, as well as the red, green and blue values.
Tip 2 The "Argb" values are stored as bytes which gives them a range of 0 to 255 inclusive.
Info In 2022 on .NET 6, this program compiles and runs without any configuration steps.
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
Known colors. 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).
And Then convert it into a regular color. This lets you examine the components of the color in code.
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
Convert. 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.
Info Each of those values is one byte—the 4 bytes together constitute a single 32-bit integer.
And Please notice how the 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.
Note This won't work for color names that are not recognized in the list of properties.
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
Empty colors. 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
Windows Forms. 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.
Info We use the 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");
}
}
}
Named colors. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 14, 2023 (edit).