Search Framework:
WLColor
Namespace: WealthLab.Core
Parent: Object

The WLColor class represents a color in a platform-independent way. A WLColor consists of alpha, red, green, and blue components, each represented by a byte from 0 to 255. WealthLab uses WLColor throughout its API wherever a color needs to be specified without depending on a platform-specific color implementation. The original WLColor is not modified. WLColor provides static properties for a comprehensive set of standard named colors. For example:

WLColor.Black
WLColor.Blue
WLColor.Cyan
WLColor.DarkGray
WLColor.Gold
WLColor.Gray
WLColor.Green
WLColor.LightBlue
WLColor.Magenta
WLColor.Orange
WLColor.Purple
WLColor.Red
WLColor.Silver
WLColor.Teal
WLColor.White
WLColor.Yellow

These return fully opaque WLColor instances using the predefined RGB values. In the current implementation, Transparent and Empty therefore contain identical ARGB component values. WLColor also provides a set of predefined Neon colors:

WLColor.NeonPurple
WLColor.NeonBlue
WLColor.NeonYellow
WLColor.NeonOrange
WLColor.NeonRed
WLColor.NeonFuschia
WLColor.NeonGreen

These colors are useful for charts and other visual elements where a brighter, high-contrast color is desired.

Constructors
WLColor
public WLColor()
public WLColor(
byte r,
byte g,
byte b)
public WLColor(
byte a,
byte r,
byte g,
byte b)
public WLColor(WLColor c)

The parameterless constructor creates a WLColor whose component values are initially zero. The three-component constructor creates an opaque color from the specified red, green, and blue values. A is set to 255. The four-component constructor creates a color using the specified alpha, red, green, and blue values. The copy constructor creates a new WLColor containing the same ARGB component values as c.



Members
A
public byte A

Gets or sets the alpha component of the color. A value of 0 is fully transparent, while 255 is fully opaque.


B
public byte B

Gets or sets the blue component of the color. Values range from 0 to 255.


Brighten
public WLColor Brighten(double? min = null)

Returns a new, brighter version of the color. Each RGB component is increased by 20, up to a maximum of 255. The original alpha component is retained. If min is supplied, WealthLab continues brightening the result until its Brightness reaches at least the specified value, or until the method has made its maximum number of additional attempts. The original WLColor is not modified.


Brightness
public double Brightness

Returns the perceived brightness of the color as a value approximately ranging from 0.0 for black to 1.0 for white. Brightness is calculated using weighted RGB components:

Sqrt(
    R² × 0.241 +
    G² × 0.691 +
    B² × 0.068
) / 255

The alpha component does not affect Brightness.


Darken
public WLColor Darken(double? max = null)

Returns a new, darker version of the color. Each RGB component is multiplied by 0.8. The original alpha component is retained. If max is supplied, WealthLab continues darkening the result until its Brightness is no greater than the specified value, or until the method has made its maximum number of additional attempts. The original WLColor is not modified.


Equals
public override bool Equals(object obj)

Returns true if obj is a WLColor with the same alpha, red, green, and blue component values.


G
public byte G

Gets or sets the green component of the color. Values range from 0 to 255.


GetHashCode
public override int GetHashCode()

Returns a hash code for the WLColor. WLColor instances having identical ARGB component values produce equivalent hash codes.


MakeTransparent
public WLColor MakeTransparent(byte a)

Returns a new WLColor using the same red, green, and blue components but with the specified alpha component. For example:

Example Code
WLColor translucentRed = WLColor.Red.MakeTransparent(128);

R
public byte R

Gets or sets the red component of the color. Values range from 0 to 255.


SetAlpha
public WLColor SetAlpha(byte a)

Returns a new WLColor using the same red, green, and blue components but with its alpha component set to a. The original WLColor is not modified.


ToString
public override string ToString()

Returns the WLColor as a comma-separated ARGB string:

A,R,G,B

For example:

255,255,0,0

represents opaque red. The resulting string can be restored using Parse.



Operators
Equality
public static bool operator ==(
WLColor c1,
WLColor c2)
public static bool operator !=(
WLColor c1,
WLColor c2)

Compares two WLColor instances by their ARGB component values. Two WLColor instances are considered equal when their A, R, G, and B properties are equal. The operators also correctly handle null references.



Predefined Colors
Empty
public static WLColor Empty

Returns a WLColor whose alpha, red, green, and blue components are all zero. Equivalent to:

Example Code
new WLColor(0, 0, 0, 0)

Transparent
public static WLColor Transparent

Returns a fully transparent black WLColor. Equivalent to:

Example Code
new WLColor(0, 0, 0, 0)


Static Methods
FromArgb
public static WLColor FromArgb(
byte a,
byte r,
byte g,
byte b)
public static WLColor FromArgb(
byte a,
WLColor baseColor)

Creates a WLColor from ARGB component values. The second overload uses the red, green, and blue components of baseColor while replacing its alpha component with a. These methods provide an API similar to the corresponding System.Drawing.Color methods.


FromRgb
public static WLColor FromRgb(
byte r,
byte g,
byte b)

Creates an opaque WLColor using the specified red, green, and blue components. The alpha component is set to 255.


GetRandom
public static WLColor GetRandom()

Returns a pseudo-random opaque color. The red, green, and blue components are each generated in the range 0 through 255.


Parse
public static WLColor Parse(string s)

Creates a WLColor from a string containing its alpha, red, green, and blue components. The standard format, as generated by ToString, is:

A,R,G,B

For compatibility, Parse also accepts pipe-separated values:

A|R|G|B

An ArgumentException is thrown if the string does not contain exactly four components.