Color
References[TypeConverter(ColorConverterInternal)]
public struct Color : IEquatable<Color>, IComparable<Color>, IControlObjectSource
Represents a color with RGBA (Red, Green, Blue, and Alpha) components
Constructors
Color
#Initializes a new instance of the Color object as a copy of the specified
colorColor
#Initializes a new instance of the Color object with the specified red, green, blue, and alpha components
Color
#Initializes a new instance of the Color struct with the specified native control object.
Remarks: Some platforms (e.g. macOS) have system color objects that change their RGB value automatically based on the display mode, such as light & dark.
Properties
Ab
#Gets or sets the alpha/opacity component as a byte of a 32-bit color (0-255)
Value: The alpha component
ControlObject
#Gets the native color control object.
Remarks: This can be null for platforms that do not have (or need) to store the native object.
Value: The native color control object.
Implements: ControlObject
Methods
Blend
#Blends the
blendColor onto the specified baseColor, including the Alpha component.Blend
#Blends the
blendColor onto the specified baseColor. Uses alpha component of blendColor to detemine the blending factor.Remarks: This computes the blended value of two colors.
Distance
#Calculates the distance of the two colors in the RGB scale
Returns: The overall distance/difference between the two colours. A lower value indicates a closer match
Equals
#Compares the specified color for equality
Parameters:
| other (Color) | Other color to determine equality |
Returns: True if all components of the specified color are equal to this object
Equals
#Tests if the specified object has the same value as this Color
Parameters:
| obj (object) | Color to compare with |
Returns: True if the specified object is a Color and has the same ARGB components as this color, false otherwise
Overrides: ValueType.Equals
FromArgb
#Creates a Color from a 32-bit ARGB value
Parameters:
| argb (int) | 32-bit ARGB value with Alpha in the high byte |
Returns: A new instance of the Color object with the specified color
FromArgb
#Creates a color from 8-bit ARGB components
FromElementId
#Creates a new color from an Element ID value
Remarks: This algorithm is from the SVG# code base: The counter is used to generate IDs in the range [0,2^24-1] The 24 bits of the counter are interpreted as follows: [red 7 bits | green 7 bits | blue 7 bits |shuffle term 3 bits] The shuffle term is used to define how the remaining high bit is set on each color. The colors are generated in the range [0,127] (7 bits) instead of [0,255]. Then the shuffle term is used to adjust them into the range [0,255]. This algorithm has the feature that consecutive ids generate visually distinct colors.
Returns: A new instance of a Color with the value from the element id
FromGrayscale
#Creates a Color with a specified value for the Red, Green, and Blue components
Returns: A new instance of the Color object with the specified grayscale color
FromPremultipliedArgb
#Creates a Color from a premultiplied 32-bit ARGB value
Parameters:
| argb (int) | 32-bit ARGB value with Alpha in the high byte |
Returns: A new instance of the Color object with the specified color, unmultiplied
FromPremultipliedArgb
#Creates a color from premultiplied 8-bit ARGB components
FromRgb
#Creates a Color from a 24-bit RGB value
Parameters:
| argb (int) | 24-bit RGB value with Red in the high byte |
Returns: A new instance of the Color object with the specified color
GetHashCode
#Gets the hash code for this Color
Returns: Hash code for the color
Overrides: ValueType.GetHashCode
Invert
#public void Invert()
Inverts the RGB color values
Remarks: This inverts the color components (other than the alpha component) by making them equal to the 1 minus the component's value. This is useful for when you want to show a highlighted color but still show the variation in colors.
Parse
#Converts a string into a new instance of a Color
Remarks: Use TryParse(string, Color, ColorStyles) instead of try/catch.
Parameters:
| value (string) | Value to convert |
Exceptions:
| ArgumentOutOfRangeException | If the value is an invalid color |
Compares two colors for sorting Colors in standard collections.
Parameters:
| other (Color) | The other color value to compare with. |
Returns: A signed value indicating whether this instance is greater, less, or equal to other.
ToArgb
#Converts this color to a 32-bit ARGB value.
Returns: The 32-bit ARGB value that corresponds to this color
ToArgb
#Converts this color to a 32-bit ARGB value given the specified
styleParameters:
| style (ColorStyles) |
Returns: The 32-bit ARGB value that corresponds to this color
ToElementId
#Converts this color to an Element ID value
Returns: The element id value of this color
See Also: FromElementId(int, int)
ToHex
#Converts this color to a hex representation
Remarks: This will either return a hex value with 8 digits (two per component), or 6 digits (two per RGB) if the
includeAlpha is set to false.Parameters:
| includeAlpha (bool = true) | True to include the alpha component, false to exclude it |
Returns: A hex representation of this color, with 8 digits if
includeAlpha is true, or 6 digits if falseToHex
#Converts this color to a hex representation
Remarks: This will either return a hex value with 8 digits (two per component), or 6 digits (two per RGB) depending on the specified
styleParameters:
| style (ColorStyles) | Styles for converting to hex |
Returns: A hex representation of this color, with 6 digits if
style has ColorStyles.ExcludeAlpha, or 8 digits if notToPremultipliedArgb
#Converts this color to a premultiplied 32-bit ARGB value, where each of the R, G, B components are first multiplied by the alpha component
Returns: A premultiplied ARGB value
ToString
#Converts this object to a string
Remarks: This just calls ToHex(bool)
Returns: A string representation of this object
Overrides: ValueType.ToString
TryParse
#Converts the specified string to a color
Remarks: The string can be any of these formats:
- #AARRGGBB or 0xAARRGGBB (where ARGB are hex values)
- #RRGGBBAA or 0xRRGGBBAA (when
stylehas ColorStyles.AlphaLast) - #RRGGBB or 0xRRGGBB
- [named] (where [named] is a name of one of the properties in Colors)
- [uint] (where [uint] is a base-10 ARGB value)
- [uint] (where [uint] is a base-10 RGBA value when
stylehas ColorStyles.AlphaLast) - [red], [green], [blue] (where each component is a value from 0-255, 0%-100%, or 0.0-1.0)
- [alpha], [red], [green], [blue] (where each component is a value from 0-255, 0%-100%, or 0.0-1.0)
- [red], [green], [blue], [alpha] (when
stylehas ColorStyles.AlphaLast)
Parameters:
| value (string) | String value to parse |
| color (out Color) | Color struct with the parsed value, or Transparent if value is invalid |
| style (ColorStyles) | Styles for parsing |
Returns: True if the value was successfully parsed into a color, false otherwise
TryParse
#Converts the specified string to a color
Remarks: The string can be any of these formats
- #AARRGGBB or #RRGGBB (where ARGB are hex values)
- 0xAARRGGBB or 0xRRGGBB
- [named] (where [named] is a name of one of the properties in Colors)
- [uint] (where [uint] is a base-10 ARGB value)
- [red], [green], [blue] (where each component is a value from 0-255, 0%-100%, or 0.0-1.0)
- [alpha], [red], [green], [blue] (where each component is a value from 0-255, 0%-100%, or 0.0-1.0)
Parameters:
| value (string) | String value to parse |
| color (out Color) | Color struct with the parsed value, or Transparent if value is invalid |
Returns: True if the value was successfully parsed into a color, false otherwise