BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Graphics
.NET 1.1+

.NET Known and System Colours

The Color class can represent over sixteen million colour shades with over two hundred and fifty levels of transparency. Often, however, a simple, named colour or a system colour is desired. These can be obtained using the KnownColor enumeration.

Known Colours

When you are working with colours, you will almost certainly use the Color structure. This type allows colours to be represented using properties for the Red, Green and Blue elements of the colour, as well as an alpha value that determines how opaque the colour appears. An alpha value of zero is completely transparent and a value of 255 is solid. Together, the Alpha, Red, Green and Blue elements give an ARGB value.

In many cases exact control of the colour is not required. You may instead want to select from a specific set of named colours. The KnownColor enumeration provides such a collection of colours, each with a name, that can be converted to ARGB values. To perform the conversion, you simply pass the KnownColor value to the FromKnownColor method of the Color structure, which is a static method.

KnownColor kc = KnownColor.Red;
Color c = Color.FromKnownColor(kc);
Console.WriteLine("R: {0}", c.R);
Console.WriteLine("G: {0}", c.G);
Console.WriteLine("B: {0}", c.B);

/* OUTPUT

R: 255
G: 0
B: 0

*/

In addition, the KnownColor enumeration contains constants that relate to system colours, such as the colour of the desktop or of a window's background. These colours vary according to the user's preferences. There are a large number of such colours that can be obtained and converted to a Color value. They are as follows:

  • ActiveBorder. Represents the colour of the border of active windows.
  • ActiveCaption. Represents the colour of the title bar for active windows.
  • ActiveCaptionText. Represents the colour of the title bar's text for active windows.
  • AppWorkspace. Represents the colour of the application workspace, which is the background colour for multiple document interface (MDI) windows.
  • ButtonFace. Represents the main colour for buttons and other window elements with a three-dimensional appearance. This property is available in .NET 2.0 and later versions.
  • ButtonHighlight. Represents the highlight colour for buttons and other window elements with a three-dimensional appearance. This property is available in .NET 2.0 and later versions.
  • ButtonShadow. Represents the shadow colour for buttons and other window elements with a three-dimensional appearance. This property is available in .NET 2.0 and later versions.
  • Control. Represents the main colour for window elements that have a three-dimensional appearance.
  • ControlDark. Represents the shadow colour for window elements with a three-dimensional appearance.
  • ControlDarkDark. Represents the dark shadow colour for window elements with a three-dimensional appearance.
  • ControlLight. Represents the highlight colour for window elements with a three-dimensional appearance.
  • ControlLightLight. Represents the light highlight colour for window elements with a three-dimensional appearance.
  • ControlText. Represents the text colour for window elements with a three-dimensional appearance.
  • Desktop. Represents the colour of the desktop. This is the background colour for Microsoft Windows that is visible when no wallpaper or pattern has been applied.
  • GradientActiveCaption. Represents the colour applied to the title bar of active windows to form a gradient with the ActiveCaption colour. This property is available in .NET 2.0 and later versions.
  • GradientInactiveCaption. Represents the colour applied to the title bar of inactive windows to form a gradient with the InactiveCaption colour. This property is available in .NET 2.0 and later versions.
  • GrayText. Represents the colour of text that has been "greyed out".
  • Highlight. Represents the background colour of text that has been selected or highlighted.
  • HighlightText. Represents the foreground colour of text that has been selected or highlighted.
  • HotTrack. Represents the colour used to designate an item that is being hot-tracked.
  • InactiveBorder. Represents the colour of the border of inactive windows.
  • InactiveCaption. Represents the colour of the title bar for inactive windows.
  • InactiveCaptionText. Represents the colour of the title bar's text for inactive windows.
  • Info. Represents the background colour of tooltips.
  • InfoText. Represents the text colour of tool tips.
  • Menu. Represents the background colour of menus.
  • MenuBar. Represents the background colour of menu bars.
  • MenuHighlight. Represents the background colour of selected or highlighted items in flat menus. This property is available in .NET 2.0 and later versions.
  • MenuText. Represents the text colour of menu items.
  • ScrollBar. Represents the background colour of scroll bars.
  • Window. Represents the background colour of the client areas of windows.
  • WindowFrame. Represent the border colour for window client areas.
  • WindowText. Represents the text colour of the client areas of windows.

The following code obtains the main colour for the buttons and three-dimensional elements and outputs the red, green and blue values for the colour. The results are dependent upon your Windows preferences so may differ from those shown in the comment.

KnownColor kc = KnownColor.ButtonFace;
Color c = Color.FromKnownColor(kc);
Console.WriteLine("R: {0}", c.R);
Console.WriteLine("G: {0}", c.G);
Console.WriteLine("B: {0}", c.B);

/* OUTPUT

R: 240
G: 240
B: 240

*/
12 March 2012