Xamarin.Forms Guides | Samples

Xamarin.Forms.Color Structure

Class that represents a color and exposes it as RGBA and HSL values.

See Also: Color

Syntax

[System.Diagnostics.DebuggerDisplay("R={R}, G={G}, B={B}, A={A}, Hue={Hue}, Saturation={Saturation}, Luminosity={Luminosity}")]
[Xamarin.Forms.TypeConverter(typeof(Xamarin.Forms.ColorTypeConverter))]
public struct Color

Remarks

This type is immutable. Colors can be expressed in the RGB or HSL modes. While the Forms.Color constructor takes R, G, B, and A values, the Color class also calculates and makes available HSL data for the color.

In XAML, application developers can specify any property that is of type Color, for example, Label.TextColor either as a XAML attribute or as a nested tag. The following code example shows how to specify the text color for a label by using an attribute:

XAML Example

<Label VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" TextColor="Green">
    <Label.Text>Hello, World!</Label.Text>
</Label>

The example below shows how to specify the text color for a label by using a nested tag:

XAML Example

<Label VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
    <Label.Text>Hello, World!</Label.Text>
    <Label.TextColor>Color.Blue</Label.TextColor>
</Label>

Application developers can specify colors in XAML either as a hexadecimal number or as a valid color name.

When specifying a color with a hexadecimal number, app developers can use 3, 4, or 6 digits. If the developer specifies 3 digits, they are interpreted as RGB doublet data for a fully opaque color. For example, "#123" specifies the color that is represented by "#FF112233". If the developer provides a 4-digit hexadecimal number, then the data are interpreted as above, except that the first digit specifies the alpha channel. For example, "#1234" specifies the color that is represented by "#11223344". Finally, if the developer provides a 6 digit hexadecimal number, the data are interpreted as a fully opaque color with those RGB values. For example, "#112233" specifies the color that is represented by "#FF112233".

When specifying a color with a string, app developers can use color name in isolation, or prefix it with "Color.". For example, both Purple and Color.Purple are valid ways to specify the color purple. The following table describes the valid color names that can be used to specify colors in XAML.

ColorShort NameRGB Value
Color.TransparentTransparent0, 0, 0 (With the alpha channel set to 0.)
Color.AquaAqua0, 255, 255
Color.BlackBlack0, 0, 0
Color.BlueBlue0, 0, 255
Color.FuchsiaFuchsia255, 0, 255
Color.GrayGray128, 128, 128
Color.GreenGreen0, 128, 0
Color.LimeLime0, 255, 0
Color.MaroonMaroon128, 0, 0
Color.NavyNavy0, 0, 128
Color.OliveOlive128, 128, 0
Color.OrangeOrange255, 165, 0
Color.PurplePurple128, 0, 128
Color.PinkPink255, 102, 255
Color.RedRed255, 0, 0
Color.SilverSilver192, 192, 192
Color.TealTeal0, 128, 128
Color.WhiteWhite255, 255, 255
Color.YellowYellow255, 255, 0

Requirements

Namespace: Xamarin.Forms
Assembly: Xamarin.Forms.Core (in Xamarin.Forms.Core.dll)
Assembly Versions: 1.0.0.0, 1.1.0.0, 1.2.0.0, 1.3.0.0, 1.4.0.0, 1.5.0.0, 2.0.0.0

The members of Xamarin.Forms.Color are listed below.

See Also: ValueType

Public Constructors

Initialize a new gray color.
Initializes a new RGB color.
Initializes a new RGB color with an alpha channel.

Public Fields

static readonly
AquaColor. Aqua, the color that is represented by the RGB value #00ffff.
static readonly
BlackColor. Black, the color that is represented by the RGB value #000000.
static readonly
BlueColor. Blue, the color that is represented by the RGB value #0000ff.
static readonly
FuchsiaColor. Fucshia, the color that is represented by the RGB value #ff00ff.
static readonly
FuschiaColor. Fucshia, the color that is represented by the RGB value #ff00ff. Deprecated. See Remarks.
static readonly
GrayColor. Gray, the color that is represented by the RGB value #808080.
static readonly
GreenColor. Green, the color that is represented by the RGB value #008000.
static readonly
LimeColor. Lime, the color that is represented by the RGB value #00ff00.
static readonly
MaroonColor. Maroon, the color that is represented by the RGB value #800000.
static readonly
NavyColor. Navy, the color that is represented by the RGB value #000080.
static readonly
OliveColor. Olive, the color that is represented by the RGB value #808000.
static readonly
OrangeColor. Orange, the color that is represented by the RGB value #ffa500.
static readonly
PinkColor. Pink, the color that is represented by the RGB value #ff66ff.
static readonly
PurpleColor. Purple, the color that is represented by the RGB value #800080.
static readonly
RedColor. Red, the color that is represented by the RGB value #ff0000.
static readonly
SilverColor. Silver, the color that is represented by the RGB value #c0c0c0.
static readonly
TealColor. Teal, the color that is represented by the RGB value #008080.
static readonly
TransparentColor. The transparent color, represented by the RGB value #00000000.
static readonly
WhiteColor. White, the color that is represented by the RGB value #ffffff.
static readonly
YellowColor. Yellow, the color that is represented by the RGB value #ffff00..

Public Properties

[read-only]
ADouble. Gets the alpha component of the color.
[read-only]
static
AccentColor. Gets the accent or tint color from the application.
[read-only]
BDouble. Gets the blue component of the color.
[read-only]
static
DefaultColor. Returns the default color.
[read-only]
GDouble. Gets the Green component of the color.
[read-only]
HueDouble. Gets the Hue of the color.
[read-only]
LuminosityDouble. Gets the Luminosity fo the color.
[read-only]
RDouble. Gets the Red component of the color.
[read-only]
SaturationDouble. Gets the Saturation of the color

Public Methods

AddLuminosity(Double) : Color
Returns a new Color with a modified luminosity channel.
override
Equals(Object) : Boolean
Determine if the provided Object is equivalent to the current Color.
static
FromHex(String) : Color
Returns a new RGB Color instance with the requested Red, Green, and Blue channels. The Alpha channel is set if hex contains one.
static
FromHsla(Double, Double, Double, Double) : Color
Returns a new HSL Color with an alpha channel
static
FromRgb(Double, Double, Double) : Color
Returns a new rgb color instance.
static
FromRgb(Int32, Int32, Int32) : Color
Returns a new rgb color instance.
static
FromRgba(Double, Double, Double, Double) : Color
Returns a new RGBA color instance.
static
FromRgba(Int32, Int32, Int32, Int32) : Color
Returns a new RGBA color instance.
static
FromUint(UInt32) : Color
Returns a new Color with the requested RGBA value.
override
GetHashCode() : Int32
Returns the Hashcode for this Object.
MultiplyAlpha(Double) : Color
Returns a new color with the alpha channel multiplied by alpha, clamped to the inclusive range [0-1].
override
ToString() : String
Returns a string representation of the Color.
WithHue(Double) : Color
Returns a color with modified Hue, but keeping the same Saturation and Luminosity.
WithLuminosity(Double) : Color
Returns a color with modified Luminosity.
WithSaturation(Double) : Color
Returns a new color with modified Saturation.

Public Operators

static
Equality(Color, Color)Returns true if color1 represents the same color as color1.
static
Inequality(Color, Color)Returns true if color1 does not represent the same color as color1.