In This Article

UIColor Struct

Provides an enhanced representation of a Color object that supports the RGB, HSB, and HLS color models, conversion between models, and numerous other color helper methods.

public struct UIColor : IEquatable<UIColor>, IEquatable<Color?>
Implements:
IEquatable<UIColor> IEquatable<Color?>

Properties

A

The alpha component value of this UIColor structure.

public byte A { readonly get; set; }

Property Value

byte

Remarks

Valid values are in the range 0 to 255.

B

The RGB blue component value of this UIColor structure.

public byte B { readonly get; set; }

Property Value

byte

Remarks

Valid values are in the range 0 to 255.

ExceedsW3CBrightnessThreshold

Indicates whether the color exceeds the W3C threshold for brightness.

public readonly bool ExceedsW3CBrightnessThreshold { get; }

Property Value

bool:

true if the color exceeds the W3C threshold for brightness; otherwise, false.

G

The RGB green component value of this UIColor structure.

public byte G { readonly get; set; }

Property Value

byte

Remarks

Valid values are in the range 0 to 255.

HlsHue

The HLS hue component value of this UIColor structure.

public double HlsHue { readonly get; set; }

Property Value

double

Remarks

Valid values are in the range 0 to 360.

HlsLightness

The HLS lightness component value of this UIColor structure.

public double HlsLightness { readonly get; set; }

Property Value

double

Remarks

Valid values are in the range 0 to 1.

HlsSaturation

The HLS saturation component value of this UIColor structure.

public double HlsSaturation { readonly get; set; }

Property Value

double

Remarks

Valid values are in the range 0 to 1.

HsbBrightness

The HSB brightness component value of this UIColor structure.

public double HsbBrightness { readonly get; set; }

Property Value

double

Remarks

Valid values are in the range 0 to 1.

HsbHue

The HSB hue component value of this UIColor structure.

public double HsbHue { readonly get; set; }

Property Value

double

Remarks

Valid values are in the range 0 to 360.

HsbSaturation

The HSB saturation component value of this UIColor structure.

public double HsbSaturation { readonly get; set; }

Property Value

double

Remarks

Valid values are in the range 0 to 1.

IsGrayscale

Indicates whether the color is grayscale.

public readonly bool IsGrayscale { get; }

Property Value

bool:

true if the color is grayscale; otherwise, false.

IsLight

Indicates whether the color is a light color.

public readonly bool IsLight { get; }

Property Value

bool:

true if the color is a light color; otherwise, false.

Luminance

The luminance of the color.

public readonly byte Luminance { get; }

Property Value

byte

Remarks

Valid values are in the range 0 to 255.

R

The RGB red component value of this UIColor structure.

public byte R { readonly get; set; }

Property Value

byte

Remarks

Valid values are in the range 0 to 255.

W3CBrightness

The brightness of the color, based on the W3C formula for calculating brightness.

public readonly byte W3CBrightness { get; }

Property Value

byte

Methods

AdaptToBackground(double, double, double, bool)

Converts the color so that it renders clearly on the specified background color.

public void AdaptToBackground(double backColorHue, double backColorLightness, double backColorSaturation, bool isHighContrast)
Parameter Type Description
backColorHue double

The background color hue.

backColorLightness double

The background color lightness.

backColorSaturation double

The background color saturation.

isHighContrast bool

Whether in a high-contrast theme.

AdaptToBackground(Color, bool)

Converts the color so that it renders clearly on the specified background color.

public void AdaptToBackground(Color backColor, bool isHighContrast)
Parameter Type Description
backColor Color

The background color.

isHighContrast bool

Whether in a high-contrast theme.

Darken(double)

Darkens the color by the specified amount.

public void Darken(double percentage)
Parameter Type Description
percentage double

The percentage by which to darken the color. The value must be between 0 and 1.

Equals(UIColor)

Indicates whether the current object is equal to another object of the same type.

public readonly bool Equals(UIColor other)
Parameter Type Description
other UIColor

An object to compare with this object.

Returns

bool:

true if the current object is equal to the other parameter; otherwise, false.

Equals(Color?)

Determines whether the specified color is equal to the current color.

public readonly bool Equals(Color? obj)
Parameter Type Description
obj Color?

The color to compare to the current color.

Returns

bool:

true if the specified color is equal to the current color; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

public override readonly bool Equals(object? obj)
Parameter Type Description
obj object

The object to compare with the current instance.

Returns

bool:

true if obj and this instance are the same type and represent the same value; otherwise, false.

FromAhls(byte, double, double, double)

Creates a UIColor structure from an alpha value and the specified HLS color values (hue, lightness, and saturation).

public static UIColor FromAhls(byte alpha, double hue, double lightness, double saturation)
Parameter Type Description
alpha byte

The alpha component value for the new UIColor structure. Valid values are 0 through 255.

hue double

The hue component value for the new UIColor structure. Valid values are 0 through 360.

lightness double

The lightness component value for the new UIColor structure. Valid values are 0 through 1.

saturation double

The saturation component value for the new UIColor structure. Valid values are 0 through 1.

Returns

UIColor

Remarks

Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to the values indicated above.

FromAhsb(byte, double, double, double)

Creates a UIColor structure from an alpha value and the specified HSB color values (hue, saturation, and brightness).

public static UIColor FromAhsb(byte alpha, double hue, double saturation, double brightness)
Parameter Type Description
alpha byte

The alpha component value for the new UIColor structure. Valid values are 0 through 255.

hue double

The hue component value for the new UIColor structure. Valid values are 0 through 360.

saturation double

The saturation component value for the new UIColor structure. Valid values are 0 through 1.

brightness double

The brightness component value for the new UIColor structure. Valid values are 0 through 1.

Returns

UIColor

Remarks

Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to the values indicated above.

FromArgb(byte, byte, byte, byte)

Creates a UIColor structure from an alpha value and the specified RGB color values (red, green, and blue).

public static UIColor FromArgb(byte alpha, byte red, byte green, byte blue)
Parameter Type Description
alpha byte

The alpha component value for the new UIColor structure. Valid values are 0 through 255.

red byte

The red component value for the new UIColor structure. Valid values are 0 through 255.

green byte

The red component value for the new UIColor structure. Valid values are 0 through 255.

blue byte

The red component value for the new UIColor structure. Valid values are 0 through 255.

Returns

UIColor

Remarks

Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to 8 bits.

FromColor(Color)

Creates a UIColor structure from the specified Color.

public static UIColor FromColor(Color color)
Parameter Type Description
color Color

The Color to use as a template.

Returns

UIColor

FromColorComplement(Color)

Creates a UIColor structure from the complement of the specified Color.

public static UIColor FromColorComplement(Color color)
Parameter Type Description
color Color

The Color to complement and use as a template.

Returns

UIColor

FromColorComplement(Color, byte)

Creates a UIColor structure from the complement of the specified Color.

public static UIColor FromColorComplement(Color color, byte alpha)
Parameter Type Description
color Color

The Color to complement and use as a template.

alpha byte

The alpha channel value to use for the new color.

Returns

UIColor

FromHls(double, double, double)

Creates a UIColor structure from the specified HLS color values (hue, lightness, and saturation).

public static UIColor FromHls(double hue, double lightness, double saturation)
Parameter Type Description
hue double

The hue component value for the new UIColor structure. Valid values are 0 through 360.

lightness double

The lightness component value for the new UIColor structure. Valid values are 0 through 1.

saturation double

The saturation component value for the new UIColor structure. Valid values are 0 through 1.

Returns

UIColor

Remarks

The alpha value is implicitly 255 (fully opaque). Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to the values indicated above.

FromHsb(double, double, double)

Creates a UIColor structure from the specified HSB color values (hue, saturation, and brightness).

public static UIColor FromHsb(double hue, double saturation, double brightness)
Parameter Type Description
hue double

The hue component value for the new UIColor structure. Valid values are 0 through 360.

saturation double

The saturation component value for the new UIColor structure. Valid values are 0 through 1.

brightness double

The brightness component value for the new UIColor structure. Valid values are 0 through 1.

Returns

UIColor

Remarks

The alpha value is implicitly 255 (fully opaque). Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to the values indicated above.

FromKnownColor(KnownColor)

Creates a UIColor structure from the specified pre-defined color.

public static UIColor FromKnownColor(KnownColor color)
Parameter Type Description
color KnownColor

An element of the KnownColor enumeration.

Returns

UIColor:

The UIColor structure that this method creates.

FromMix(Color, Color, double)

Creates a UIColor structure that is the specified percentage between the value of two Color objects.

public static UIColor FromMix(Color color1, Color color2, double percentage)
Parameter Type Description
color1 Color

The first Color to examine.

color2 Color

The second Color to examine.

percentage double

The percentage from the first color to the second in which to return a color.

Returns

UIColor

FromName(string)

Creates a UIColor structure from the specified name of a pre-defined color.

public static UIColor FromName(string name)
Parameter Type Description
name string

A string that is the name of a pre-defined color. Valid names are the same as the names of the elements of the KnownColor enumeration.

Returns

UIColor

FromRgb(byte, byte, byte)

Creates a UIColor structure from the specified RGB color values (red, green, and blue).

public static UIColor FromRgb(byte red, byte green, byte blue)
Parameter Type Description
red byte

The red component value for the new UIColor structure. Valid values are 0 through 255.

green byte

The red component value for the new UIColor structure. Valid values are 0 through 255.

blue byte

The red component value for the new UIColor structure. Valid values are 0 through 255.

Returns

UIColor

Remarks

The alpha value is implicitly 255 (fully opaque). Although this method allows a 32-bit value to be passed for each color component, the value of each component is limited to 8 bits.

FromWebColor(string)

Creates a UIColor structure from the specified web color.

public static UIColor FromWebColor(string text)
Parameter Type Description
text string

A string containing the color.

Returns

UIColor

Remarks

This method can process HTML color specifications (e.g. #FF00FF) and known color names.

GetContrastRatioWith(UIColor)

Returns the contrast ratio of this color with the specified color.

public readonly double GetContrastRatioWith(UIColor contrastingColor)
Parameter Type Description
contrastingColor UIColor

The contrasting color to compare.

Returns

double

GetHashCode()

Returns the hash code for this instance.

public override readonly int GetHashCode()

Returns

int:

A 32-bit signed integer that is the hash code for this instance.

GetStandardCustomColors()

Returns a Color array containing all of the standard custom values.

public static Color[] GetStandardCustomColors()

Returns

Color[]

GetSystemColors()

Returns a Color array containing all of the SystemColors values.

public static Color[] GetSystemColors()

Returns

Color[]:

An array of Color objects.

GetTintedColor(Color, Color)

Returns the custom tinted color for the specified base color.

public static Color GetTintedColor(Color baseColor, Color tintColor)
Parameter Type Description
baseColor Color

The base Color to examine.

tintColor Color

The tint Color.

Returns

Color

GetWebColors()

Returns an array containing all of the web color values.

public static Color[] GetWebColors()

Returns

Color[]:

An array of Color objects.

Grayscale()

Converts the color to grayscale.

public void Grayscale()

Lighten(double)

Lightens the color by the specified amount.

public void Lighten(double percentage)
Parameter Type Description
percentage double

The percentage by which to lighten the color. The value must be between 0 and 1.

Tint(Color)

Tints this color towards the specified tint Color.

public void Tint(Color tintColor)
Parameter Type Description
tintColor Color

The tint Color.

ToColor()

The Color value of this UIColor structure.

public readonly Color ToColor()

Returns

Color

ToKnownColor()

The KnownColor value of this KnownColor structure.

public readonly KnownColor ToKnownColor()

Returns

KnownColor:

An element of the KnownColor enumeration, if the UIColor structure is created from a pre-defined color by using either the FromName(string) method or the FromKnownColor(KnownColor) method; otherwise, zero.

ToString()

Returns the string representation of this object.

public override readonly string ToString()

Returns

string:

The string representation of this object.

ToWebColor()

Converts the UIColor to a web color.

public readonly string ToWebColor()

Returns

string:

The color in HTML color specification format (e.g. #FF00FF).

ToWebColor(bool)

Converts the UIColor to a web color.

public readonly string ToWebColor(bool includeAlpha)
Parameter Type Description
includeAlpha bool

Whether to include the alpha specification.

Returns

string:

The color in HTML color specification format (e.g. #FFFF00FF).

TryFromName(string, out UIColor)

Tries to create a UIColor structure from the specified name of a pre-defined color.

public static bool TryFromName(string name, out UIColor color)
Parameter Type Description
name string

A string that is the name of a pre-defined color. Valid names are the same as the names of the elements of the KnownColor enumeration.

color UIColor

Returns the UIColor structure that this method creates.

Returns

bool:

true if the name was recognized successfully; otherwise, false.

TryFromWebColor(string, out UIColor)

Tries to create a UIColor structure from the specified web color.

public static bool TryFromWebColor(string text, out UIColor color)
Parameter Type Description
text string

A string containing the color.

color UIColor

Returns the UIColor structure that this method creates.

Returns

bool:

true if the text was parsed successfully; otherwise, false.

Remarks

This method can process HTML color specifications (e.g. #FF00FF) and known color names.

Operators

operator ==(UIColor, UIColor)

Compares two color objects for equality.

public static bool operator ==(UIColor left, UIColor right)
Parameter Type Description
left UIColor

A UIColor to compare.

right UIColor

A UIColor to compare.

Returns

bool:

true if the color values of left and right are equal; otherwise, false.

implicit operator Color(UIColor)

Implicitly converts the specified UIColor into a Color instance.

public static implicit operator Color(UIColor color)
Parameter Type Description
color UIColor

the UIColor to examine.

Returns

Color

implicit operator UIColor(Color)

Implicitly converts the specified Color into a UIColor instance.

public static implicit operator UIColor(Color color)
Parameter Type Description
color Color

the Color to examine.

Returns

UIColor

operator !=(UIColor, UIColor)

Compares two color objects for inequality.

public static bool operator !=(UIColor left, UIColor right)
Parameter Type Description
left UIColor

A UIColor to compare.

right UIColor

A UIColor to compare.

Returns

bool:

true if the color values of left and right are unequal; otherwise, false.

Inherited Members