Share via


Color Structure

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Describes a color in terms of alpha, red, green, and blue channels.

Namespace:  System.Windows.Media
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Structure Color _
    Implements IFormattable
public struct Color : IFormattable
<Color ...>predefinedColor</Color>
- or -
<Color ...>#rgb</Color>
- or -
<Color ...>#argb</Color>
- or -
<Color ...>#rrggbb</Color>
- or -
<Color ...>#aarrggbb</Color>
- or -
<Color ...>sc#scR,scG,scB</Color>
- or -
<Color ...>sc# scA,scR,scG,scB</Color>
<object property="predefinedColor"/>
- or -
<object property="#rgb"/>
- or -
<object property="#argb"/>
- or -
<object property="#rrggbb"/>
- or -
<object property="#aarrggbb"/>
- or -
<object property="sc#scR,scG,scB"/>
- or -
<object property="sc# scA,scR,scG,scB"/>

XAML Values

  • predefinedColor
    One of the colors predefined by the Colors class (static properties), or one of the other named colors. See Remarks.

  • rgb
    A three-character hexadecimal value. The first c specifies the color's R value, the second character specifies the G value, and the third character specifies the B value. For example, 00F.

  • argb
    A four-character hexadecimal value. The first character specifies the color's A value, the second character specifies its R value, the third character specifies the G value, and the fourth character specifies its B value. For example, F00F.

  • rrggbb
    A six-character hexadecimal value. The first two characters specify the color's R value, the next two specify its G value, and the final two specify its B value. For example, 0000FF.

  • aarrggbb
    An eight-character hexadecimal value. The first two characters specify the color's A value, the next two specify its R value, the next two specify its G value, and the final two specify its B value. For example, FF0000FF.

  • scA
    The color's ScA value as a value between 0 and 1. ScA is not exposed as a Color property directly.

  • scR
    The color's ScR value as a value between 0 and 1. ScR is not exposed as a Color property directly

  • scG
    The color's ScG value as a value between 0 and 1. ScG is not exposed as a Color property directly

  • scB
    The color's ScB value as a value between 0 and 1. ScB is not exposed as a Color property directly

The Color type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone A Gets or sets the sRGB alpha channel value of the color.
Public propertySupported by Silverlight for Windows Phone B Gets or sets the sRGB blue channel value of the color.
Public propertySupported by Silverlight for Windows Phone G Gets or sets the sRGB green channel value of the color.
Public propertySupported by Silverlight for Windows Phone R Gets or sets the sRGB red channel value of the color.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone Equals(Color) Tests whether the specified Color structure is identical to the current color.
Public methodSupported by Silverlight for Windows Phone Equals(Object) Tests whether the specified object is a Color structure and is equivalent to the current color. (Overrides ValueType.Equals(Object).)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows Phone FromArgb Creates a new Color structure by using the specified sRGB alpha channel and color channel values.
Public methodSupported by Silverlight for Windows Phone GetHashCode Gets a hash code for the current Color structure. (Overrides ValueType.GetHashCode().)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ToString() Creates a string representation of the color using the ARGB channels in hex notation. (Overrides ValueType.ToString().)
Public methodSupported by Silverlight for Windows Phone ToString(IFormatProvider) Creates a string representation of the color by using the ARGB channels and the specified format provider.

Top

Operators

  Name Description
Public operatorStatic memberSupported by Silverlight for Windows Phone Equality Tests whether two Color structures are identical.
Public operatorStatic memberSupported by Silverlight for Windows Phone Inequality Tests whether two Color structures are not identical.

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate methodSupported by Silverlight for Windows Phone IFormattable.ToString Infrastructure. For a description of this member, see ToString.

Top

Remarks

The Silverlight implementation of Color does not support Color.FromRgb. The Silverlight 2 implementation of Color also does not support synchronized properties that report both the sRGB and scRBG values; only the sRGB values are reported as properties. It is possible to specify a Color using an scRGB format string, but this requires a deliberate XAML usage to access the type conversion behavior, along with Load. See Using XamlReader.Load.

Properties of Color do not support an attribute syntax in XAML for Silverlight. In XAML you should always specify Color-type properties through one of the following usages:

  • The XAML attribute usage, which infers properties that use the Color type and uses a type converter to process the attribute string into the specific values for the Color.

  • A property element usage, containing a Color object element. For that object element, set the Color properties using initialization text, as shown in the XAML Object Element Usage.

The XAML object element usage (with initialization text) is useful for declaring a Color as a resource in a XAML ResourceDictionary.

Predefined Colors

A predefined color can either be a static property name of Colors, or one of the other color names that can be processed by string type conversion. The following are the 16 colors that can be set in code using a static property value of the Colors class, or set in XAML by setting an attribute to their name string.

There are additional predefined named colors that do not have static properties on the Colors class that store their values. Instead, from-string type conversion behavior is enabled when parsing the attribute in XAML. The following is a table of the available named colors (which includes the 16 colors that have static properties available).

Predefined Colors

Colors.

Note that setting a color by this means is possible directly when setting a Brush or Color value as a XAML attribute, but setting the colors in code for the colors that are not specific Colors class static properties takes some indirection, as is described in the next section.

Setting Predefined Colors or scRGB Colors in Code

in Silverlight 2, setting predefined colors in code also uses the type conversion behavior, combined with XamlReader.Load. You must create an instance of an object element with a property of type Brush or Color, specifying the value of the property as one of the predefined color names. Then you can retrieve either the Brush or Color from the created object. In the case of the Brush (which will be a SolidColorBrush), you can also retrieve its Color property. You do not need to attach your created object to the element tree, you are using Load here as a construction mechanism that can access the predefined color string type conversion behavior. This same technique can be used to create colors from scRBG strings, or even sRGB strings (although for these it might be more convenient to use the FromArgb method, first splitting the string and performing math operations on the A,R,G,B atoms to match the FromArgb 0-255 scale).

The following is example code for using the XamlReader.Load technique to create both a Brush and Color from a predefined color name.

  String xamlString = "<Canvas xmlns=\"https://schemas.microsoft.com/winfx/2006/xaml/presentation\" Background=\"MistyRose\"/>";
  Canvas c = (Canvas) System.Windows.Markup.XamlReader.Load(xamlString);
  SolidColorBrush mistyRoseBrush = (SolidColorBrush) c.Background;
  Color mistyRose = mistyRoseBrush.Color;

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Other Resources