Color Structure

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

Namespace: System.Windows.Media
Assembly: System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

Syntax

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
A Gets or sets the sRGB alpha channel value of the color.
B Gets or sets the sRGB blue channel value of the color.
G Gets or sets the sRGB green channel value of the color.
R Gets or sets the sRGB red channel value of the color.

Top

Methods

Name Description
Equals(Color) Tests whether the specified Color structure is identical to the current color.
Equals(Object) Tests whether the specified object is a Color structure and is equivalent to the current color. (Overrides ValueType..::.Equals(Object).)
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.)
FromArgb Creates a new Color structure by using the specified sRGB alpha channel and color channel values.
GetHashCode Gets a hash code for the current Color structure. (Overrides ValueType..::.GetHashCode()()().)
GetType Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
ToString()()() Creates a string representation of the color using the ARGB channels in hex notation. (Overrides ValueType..::.ToString()()().)
ToString(IFormatProvider) Creates a string representation of the color by using the ARGB channels and the specified format provider.

Top

Operators

Name Description
Equality Tests whether two Color structures are identical.
Inequality Tests whether two Color structures are not identical.

Top

Explicit Interface Implementations

Name Description
IFormattable..::.ToString Infrastructure. For a description of this member, see ToString.

Top

Remarks

The Windows Phone implementation of Color does not support Color.FromRgb. The Windows Phone 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.

Properties of Color do not support an attribute syntax in XAML for Windows Phone. 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 dictionary.

Note

Windows Phone uses a resource dictionary for theme resources, which includes colors. These color resources are the recommended way to apply color in Windows Phone applications because they ensure UI elements appear consistency and can change color based on the theme the user has selected. For a more consistent user experience, avoid using static colors. For more information, see Theme resources for Windows Phone.

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

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 Windows Phone, 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=\"http://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

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

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

System.Windows.Media Namespace

Brush

Other Resources

Theme resources for Windows Phone