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
Properties
A
Gets or sets the alpha component value of this UIColor structure.
public byte A { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 255
.
B
Gets or sets the RGB blue component value of this UIColor structure.
public byte B { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 255
.
ExceedsW3CBrightnessThreshold
Gets whether the color exceeds the W3C threshold for brightness.
public bool ExceedsW3CBrightnessThreshold { get; }
Property Value
- bool:
true
if the color exceeds the W3C threshold for brightness; otherwise,false
.
G
Gets or sets the RGB green component value of this UIColor structure.
public byte G { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 255
.
HlsHue
Gets or sets the HLS hue component value of this UIColor structure.
public double HlsHue { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 360
.
HlsLightness
Gets or sets the HLS lightness component value of this UIColor structure.
public double HlsLightness { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 1
.
HlsSaturation
Gets or sets the HLS saturation component value of this UIColor structure.
public double HlsSaturation { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 1
.
HsbBrightness
Gets or sets the HSB brightness component value of this UIColor structure.
public double HsbBrightness { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 1
.
HsbHue
Gets or sets the HSB hue component value of this UIColor structure.
public double HsbHue { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 360
.
HsbSaturation
Gets or sets the HSB saturation component value of this UIColor structure.
public double HsbSaturation { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 1
.
IsGrayscale
Gets whether the color is grayscale.
public bool IsGrayscale { get; }
Property Value
- bool:
true
if the color is grayscale; otherwise,false
.
IsLight
Gets whether the color is a light color.
public bool IsLight { get; }
Property Value
- bool:
true
if the color is a light color; otherwise,false
.
Luminance
Gets the luminance of the color.
public byte Luminance { get; }
Property Value
- byte:
The luminance of the color.
Remarks
Valid values are in the range 0
to 255
.
R
Gets or sets the RGB red component value of this UIColor structure.
public byte R { get; set; }
Property Value
Remarks
Valid values are in the range 0
to 255
.
W3CBrightness
Gets the brightness of the color, based on the W3C formula for calculating brightness.
public byte W3CBrightness { get; }
Property Value
- byte:
The brightness of the color, based on the W3C formula for calculating brightness.
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 |
Equals(Color)
Determines whether the specified Object
is equal to the current Object
.
public bool Equals(Color obj)
Parameter | Type | Description |
---|---|---|
obj | Color | The |
Returns
- bool:
true
if the specifiedObject
is equal to the currentObject
; otherwise,false
.
Equals(object)
Determines whether the specified Object
is equal to the current Object
.
public override bool Equals(object obj)
Parameter | Type | Description |
---|---|---|
obj | object | The |
Returns
- bool:
true
if the specifiedObject
is equal to the currentObject
; 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
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
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
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)
public static UIColor FromColor(Color color)
Parameter | Type | Description |
---|---|---|
color | Color | The Color to use as a template. |
Returns
FromColorComplement(Color)
public static UIColor FromColorComplement(Color color)
Parameter | Type | Description |
---|---|---|
color | Color | The Color to complement and use as a template. |
Returns
FromColorComplement(Color, byte)
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
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
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
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
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
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
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
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
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 double GetContrastRatioWith(UIColor contrastingColor)
Parameter | Type | Description |
---|---|---|
contrastingColor | UIColor | The contrasting color to compare. |
Returns
- double:
The contrast ratio of this color with the specified color.
GetHashCode()
Returns a hash code for this object.
public override int GetHashCode()
Returns
- int:
An integer value that specifies a hash value for this object.
GetStandardCustomColors()
Returns a Color array containing all of the standard custom values.
GetSystemColors()
Returns a Color array containing all of the SystemColors values.
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:
The custom tinted color for the specified base color.
GetWebColors()
Returns an array containing all of the web color values.
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 |
Tint(Color)
Tints this color towards the specified tint Color.
ToColor()
ToKnownColor()
Gets the KnownColor value of this KnownColor structure.
public 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 a String
that represents the current Object
.
ToWebColor()
Converts the UIColor to a web color.
public string ToWebColor()
Returns
- string:
The color in HTML color specification format (e.g. #FF00FF).
ToWebColor(bool)
Converts the UIColor to a web color.
public 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 ofleft
andright
are equal; otherwise,false
.
implicit operator Color(UIColor)
public static implicit operator Color(UIColor color)
Parameter | Type | Description |
---|---|---|
color | UIColor | the UIColor to examine. |
Returns
implicit operator UIColor(Color)
public static implicit operator UIColor(Color color)
Parameter | Type | Description |
---|---|---|
color | Color | the Color to examine. |
Returns
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 ofleft
andright
are unequal; otherwise,false
.