[TypeConverter(ColorConverterInternal)] public struct Color : IEquatable<Color>, IComparable<Color>, IControlObjectSource
Represents a color with RGBA (Red, Green, Blue, and Alpha) components

Constructors

Color

#
public Color(Color color, float? alpha = null)
Initializes a new instance of the Color object as a copy of the specified color
color (Color)Color to copy
alpha (float? = null)Alpha to use for the new color, or null to use the alpha component from color

Color

#
public Color(float red, float green, float blue, float alpha = 1)
Initializes a new instance of the Color object with the specified red, green, blue, and alpha components
red (float)Red component (0-1)
green (float)Green component (0-1)
blue (float)Blue component (0-1)
alpha (float = 1)Alpha component (0-1)

Color

#
public Color(object controlObject, float red, float green, float blue, float alpha)
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.
controlObject (object)Native control object representing this color.
red (float)Red component (0-1)
green (float)Green component (0-1)
blue (float)Blue component (0-1)
alpha (float)Alpha component (0-1)

Properties

Ab

#
public int Ab { get; set; }
Gets or sets the alpha/opacity component as a byte of a 32-bit color (0-255)

Bb

#
public int Bb { get; set; }
Gets or sets the blue component as a byte of a 32-bit color (0-255)

ControlObject

#
public object ControlObject { get; }
Gets the native color control object.
Remarks: This can be null for platforms that do not have (or need) to store the native object.
The native color control object.
ControlObject

Gb

#
public int Gb { get; set; }
Gets or sets the green component as a byte of a 32-bit color (0-255)

Rb

#
public int Rb { get; set; }
Gets or sets the red component as a byte of a 32-bit color (0-255)

Methods

Blend

#
public static Color Blend(Color baseColor, Color blendColor, float blendFactor)
Blends the blendColor onto the specified baseColor, including the Alpha component.
Remarks: This computes the blended value of two colors.
baseColor (Color)Base color
blendColor (Color)Color to blend onto the base color
blendFactor (float)Blend amount from 0 (baseColor) to 1 (blendColor).

Blend

#
public static Color Blend(Color baseColor, Color blendColor)
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.
baseColor (Color)Base color
blendColor (Color)Color to blend onto the base color

Distance

#
public static float Distance(Color value1, Color value2)
Calculates the distance of the two colors in the RGB scale
value1 (Color)First color to compare
value2 (Color)Second color to compare with
The overall distance/difference between the two colours. A lower value indicates a closer match

Equals

#
public bool Equals(Color other)
Compares the specified color for equality
other (Color)Other color to determine equality
True if all components of the specified color are equal to this object

Equals

#
public override bool Equals(object obj)
Tests if the specified object has the same value as this Color
obj (object)Color to compare with
True if the specified object is a Color and has the same ARGB components as this color, false otherwise
ValueType.Equals

FromArgb

#
public static Color FromArgb(int argb)
Creates a Color from a 32-bit ARGB value
argb (int)32-bit ARGB value with Alpha in the high byte
A new instance of the Color object with the specified color

FromArgb

#
public static Color FromArgb(int red, int green, int blue, int alpha = 255)
Creates a color from 8-bit ARGB components
red (int)The red component (0-255)
green (int)The green component (0-255)
blue (int)The blue component (0-255)
alpha (int = 255)The alpha component (0-255)
A new instance of the Color object with the specified components

FromElementId

#
public static Color FromElementId(int id, int alpha = 255)
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.
id (int)Identifier.
alpha (int = 255)Alpha.
A new instance of a Color with the value from the element id

FromGrayscale

#
public static Color FromGrayscale(float val, float alpha = 1)
Creates a Color with a specified value for the Red, Green, and Blue components
val (float)Value for each RGB component
alpha (float = 1)Alpha value
A new instance of the Color object with the specified grayscale color

FromPremultipliedArgb

#
public static Color FromPremultipliedArgb(int argb)
Creates a Color from a premultiplied 32-bit ARGB value
argb (int)32-bit ARGB value with Alpha in the high byte
A new instance of the Color object with the specified color, unmultiplied

FromPremultipliedArgb

#
public static Color FromPremultipliedArgb(int red, int green, int blue, int alpha = 255)
Creates a color from premultiplied 8-bit ARGB components
red (int)The red component (0-255)
green (int)The green component (0-255)
blue (int)The blue component (0-255)
alpha (int = 255)The alpha component (0-255)
A new instance of the Color object with the specified components unmultiplied

FromRgb

#
public static Color FromRgb(int argb)
Creates a Color from a 24-bit RGB value
argb (int)24-bit RGB value with Red in the high byte
A new instance of the Color object with the specified color

GetHashCode

#
public override int GetHashCode()
Gets the hash code for this Color

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

#
public static Color Parse(string value)
Converts a string into a new instance of a Color
Explicit

IComparable<Color>.CompareTo

#
int IComparable<Color>.CompareTo(Color other)
Compares two colors for sorting Colors in standard collections.
other (Color)The other color value to compare with.
A signed value indicating whether this instance is greater, less, or equal to other.

ToArgb

#
public int ToArgb()
Converts this color to a 32-bit ARGB value.

ToArgb

#
public int ToArgb(ColorStyles style)
Converts this color to a 32-bit ARGB value given the specified style
style (ColorStyles)
The 32-bit ARGB value that corresponds to this color

ToCMYK

#
public ColorCMYK ToCMYK()
Converts this RGB color to the CMYK colorspace.

ToElementId

#
public int ToElementId()
Converts this color to an Element ID value

ToHex

#
public string ToHex(bool includeAlpha = true)
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.
includeAlpha (bool = true)True to include the alpha component, false to exclude it
A hex representation of this color, with 8 digits if includeAlpha is true, or 6 digits if false

ToHex

#
public string ToHex(ColorStyles style)
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 style
style (ColorStyles)Styles for converting to hex
A hex representation of this color, with 6 digits if style has ColorStyles.ExcludeAlpha, or 8 digits if not

ToHSB

#
public ColorHSB ToHSB()
Converts this RGB color to the HSB colorspace.

ToHSL

#
public ColorHSL ToHSL()
Converts this RGB color to the HSL colorspace.

ToPremultipliedArgb

#
public int ToPremultipliedArgb()
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

ToString

#
public override string ToString()
Converts this object to a string

TryParse

#
public static bool TryParse(string value, out Color color, ColorStyles style)
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 style has 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 style has 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 style has ColorStyles.AlphaLast)
If the string is null or empty, this will return Colors.Transparent
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
True if the value was successfully parsed into a color, false otherwise

TryParse

#
public static bool TryParse(string value, out Color color)
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)
If the string is null or empty, this will return Colors.Transparent
value (string)String value to parse
color (out Color)Color struct with the parsed value, or Transparent if value is invalid
True if the value was successfully parsed into a color, false otherwise

Operators

OperationResultDescription
Color==ColorboolCompares two Color structs for equality
Color!=ColorboolCompares two Color structs for inequality