Table of Contents

Class MudColor

Namespace
MudBlazor.Utilities
Assembly
MudBlazor.dll

Represents a color with methods to manipulate color values.

public class MudColor : ISerializable, IEquatable<MudColor>, IParsable<MudColor>, IFormattable
Inheritance
MudColor
Implements
Inherited Members
Extension Methods

Constructors

MudColor()

Constructs a default instance of MudColor with default values (black with full opacity).

public MudColor()

MudColor(byte, byte, byte, MudColor)

Initializes a new instance of the MudColor class with the specified red, green, blue, and alpha values, copying the hue value from the provided color.

public MudColor(byte r, byte g, byte b, MudColor color)

Parameters

r byte

The red component value (0 to 255).

g byte

The green component value (0 to 255).

b byte

The blue component value (0 to 255).

color MudColor

The existing color to copy the hue value from.

MudColor(byte, byte, byte, byte)

Initializes a new instance of the MudColor class with the specified red, green, blue, and alpha values.

[JsonConstructor]
public MudColor(byte r, byte g, byte b, byte a)

Parameters

r byte

The red component value (0 to 255).

g byte

The green component value (0 to 255).

b byte

The blue component value (0 to 255).

a byte

The alpha component value (0 to 255).

MudColor(double, double, double, double)

Initializes a new instance of the MudColor class with the specified hue, saturation, lightness, and alpha values.

public MudColor(double h, double s, double l, double a)

Parameters

h double

The hue component value (0 to 360).

s double

The saturation component value (0.0 to 1.0).

l double

The lightness component value (0.0 to 1.0).

a double

The alpha component value (0 to 1.0).

MudColor(double, double, double, int)

Initializes a new instance of the MudColor class with the specified hue, saturation, lightness, and alpha values.

public MudColor(double h, double s, double l, int a)

Parameters

h double

The hue component value (0 to 360).

s double

The saturation component value (0.0 to 1.0).

l double

The lightness component value (0.0 to 1.0).

a int

The alpha component value (0 to 255).

MudColor(int, int, int, double)

Initializes a new instance of the MudColor class with the specified RGB values and alpha component.

public MudColor(int r, int g, int b, double alpha)

Parameters

r int

The red component value (0 to 255).

g int

The green component value (0 to 255).

b int

The blue component value (0 to 255).

alpha double

The alpha component value (0.0 to 1.0).

MudColor(int, int, int, int)

Initializes a new instance of the MudColor class with the specified RGB values and alpha component.

public MudColor(int r, int g, int b, int alpha)

Parameters

r int

The red component value (0 to 255).

g int

The green component value (0 to 255).

b int

The blue component value (0 to 255).

alpha int

The alpha component value (0 to 255).

MudColor(SerializationInfo, StreamingContext)

Deserialization constructor for MudColor.

protected MudColor(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

The SerializationInfo> containing the serialized data.

context StreamingContext

The StreamingContext>.

MudColor(string)

Initializes a new instance of the MudColor class with the specified string representation of a color.

public MudColor(string value)

Parameters

value string

The string representation of a color.

Remarks

The color can be represented in various formats, including hexadecimal (with or without alpha), RGB, and RGBA. Examples of valid color strings:

  • Hexadecimal format: "#ab2a3d", "#ab2a3dff"
  • RGB format: "rgb(12,15,40)"
  • RGBA format: "rgba(12,15,40,0.42)"

MudColor(uint)

Initializes a new instance of the MudColor class with the specified color.

public MudColor(uint rgba)

Parameters

rgba uint

the four bytes of this 32-bit unsigned integer contain the red, green, blue and alpha components

Properties

A

Gets the alpha component value of the color.

public byte A { get; }

Property Value

byte

APercentage

Gets the alpha component value as a percentage (0.0 to 1.0) of the color.

[JsonIgnore]
public double APercentage { get; }

Property Value

double

B

Gets the blue component value of the color.

public byte B { get; }

Property Value

byte

G

Gets the green component value of the color.

public byte G { get; }

Property Value

byte

H

Gets the hue component value of the color.

[JsonIgnore]
public double H { get; }

Property Value

double

L

Gets the lightness component value of the color.

[JsonIgnore]
public double L { get; }

Property Value

double

R

Gets the red component value of the color.

public byte R { get; }

Property Value

byte

S

Gets the saturation component value of the color.

[JsonIgnore]
public double S { get; }

Property Value

double

UInt32

Gets the 32-bit unsigned integer representation of the color.

[JsonIgnore]
public uint UInt32 { get; }

Property Value

uint

Value

Gets the hexadecimal representation of the color.

[JsonIgnore]
public string Value { get; }

Property Value

string

Methods

ChangeLightness(double)

Creates a new MudColor instance by adjusting the lightness component value by the specified amount.

public MudColor ChangeLightness(double amount)

Parameters

amount double

The amount to adjust the lightness by (-1.0 to 1.0).

Returns

MudColor

A new MudColor instance with the adjusted lightness.

ColorDarken(double)

Creates a new MudColor instance by darkening the color.

public MudColor ColorDarken(double amount)

Parameters

amount double

The amount to darken the color by.

Returns

MudColor

A new MudColor instance that is darker than the original color.

ColorLighten(double)

Creates a new MudColor instance by lightening the color.

public MudColor ColorLighten(double amount)

Parameters

amount double

The amount to lighten the color by.

Returns

MudColor

A new MudColor instance that is lighter than the original color.

ColorRgbDarken()

Creates a new MudColor instance by darkening the color using the RGB algorithm.

public MudColor ColorRgbDarken()

Returns

MudColor

A new MudColor instance that is darker than the original color.

ColorRgbLighten()

Creates a new MudColor instance by lightening the color using the RGB algorithm.

public MudColor ColorRgbLighten()

Returns

MudColor

A new MudColor instance that is lighter than the original color.

Deconstruct(out byte, out byte, out byte)

Deconstructs the MudColor into its red, green, and blue components.

public void Deconstruct(out byte r, out byte g, out byte b)

Parameters

r byte

The red component value (0 to 255).

g byte

The green component value (0 to 255).

b byte

The blue component value (0 to 255).

Deconstruct(out byte, out byte, out byte, out byte)

Deconstructs the MudColor into its red, green, blue, and alpha components.

public void Deconstruct(out byte r, out byte g, out byte b, out byte a)

Parameters

r byte

The red component value (0 to 255).

g byte

The green component value (0 to 255).

b byte

The blue component value (0 to 255).

a byte

The alpha component value (0 to 255).

Equals(MudColor?)

Determines whether this MudColor instance is equal to another MudColor instance.

public bool Equals(MudColor? other)

Parameters

other MudColor

The MudColor instance to compare.

Returns

bool

True if the instances are equal; otherwise, false.

Equals(object?)

public override bool Equals(object? obj)

Parameters

obj object

Returns

bool

GenerateAnalogousPalette(MudColor, int, double)

Generates an analogous palette of colors based on a specified base color.

public static IEnumerable<MudColor> GenerateAnalogousPalette(MudColor baseColor, int numberOfColors = 5, double angle = 30)

Parameters

baseColor MudColor

The base color to generate the analogous palette from.

numberOfColors int

The total number of colors in the analogous palette.

angle double

The angle between each color in the analogous palette.

Returns

IEnumerable<MudColor>

An enumerable collection of MudColor representing the analogous palette.

Exceptions

ArgumentOutOfRangeException

Thrown when numberOfColors is less than or equal to zero.

GenerateGradientPalette(MudColor, MudColor, int)

Generates a gradient palette of colors between two specified colors.

public static IEnumerable<MudColor> GenerateGradientPalette(MudColor startColor, MudColor endColor, int numberOfColors = 5)

Parameters

startColor MudColor

The starting color of the gradient.

endColor MudColor

The ending color of the gradient.

numberOfColors int

The total number of colors in the gradient palette.

Returns

IEnumerable<MudColor>

An enumerable collection of MudColor representing the gradient palette.

Exceptions

ArgumentOutOfRangeException

Thrown when numberOfColors is less than or equal to zero.

GenerateMultiGradientPalette(IReadOnlyList<MudColor>, int)

Generates a multi-gradient palette of colors between multiple specified colors.

public static IEnumerable<MudColor> GenerateMultiGradientPalette(IReadOnlyList<MudColor> colors, int numberOfColors = 5)

Parameters

colors IReadOnlyList<MudColor>

The list of colors to generate the multi-gradient palette from. Must contain at least two colors.

numberOfColors int

The total number of colors in the multi-gradient palette.

Returns

IEnumerable<MudColor>

An enumerable collection of MudColor representing the multi-gradient palette.

Exceptions

ArgumentOutOfRangeException

Thrown when numberOfColors is less than or equal to zero.

ArgumentException

Thrown when the colors collection contains fewer than two colors.

GenerateTintShadePalette(MudColor, int, double, double)

Generates a palette of colors by lightening and darkening the base color.

public static IEnumerable<MudColor> GenerateTintShadePalette(MudColor baseColor, int numberOfColors = 5, double tintStep = 0.075, double shadeStep = 0.075)

Parameters

baseColor MudColor

The base color to generate the palette from.

numberOfColors int

The total number of colors in the palette.

tintStep double

The step value for lightening the color. If tintStep is 0, no lighter colors will be added to the palette.

shadeStep double

The step value for darkening the color. If shadeStep is 0, no darker colors will be added to the palette.

Returns

IEnumerable<MudColor>

A read-only list of MudColor representing the generated palette.

Exceptions

ArgumentOutOfRangeException

Thrown when numberOfColors is less than or equal to zero.

ArgumentOutOfRangeException

Thrown when tintStep is negative.

ArgumentOutOfRangeException

Thrown when shadeStep is negative.

GetHashCode()

public override int GetHashCode()

Returns

int

HslEquals(MudColor?)

Checks whether the HSL (Hue, Saturation, Lightness) values of this MudColor instance are equal compared to another MudColor instance.

public bool HslEquals(MudColor? other)

Parameters

other MudColor

The MudColor instance to compare HSL values with.

Returns

bool

True if the HSL are equal; otherwise, false.

Lerp(MudColor, MudColor, float)

Linearly interpolates between two MudColor instances.

public static MudColor Lerp(MudColor colorStart, MudColor colorEnd, float t)

Parameters

colorStart MudColor

The starting MudColor instance.

colorEnd MudColor

The ending MudColor instance.

t float

The interpolation factor (0.0 to 1.0).

Returns

MudColor

A new MudColor instance that is the result of the interpolation.

RgbaEquals(MudColor?)

Checks whether the RGBA (Red, Green, Blue, Alpha) values of this MudColor instance are equal compared to another MudColor instance.

public bool RgbaEquals(MudColor? other)

Parameters

other MudColor

The MudColor instance to compare HSL values with.

Returns

bool

True if the RGBA are equal; otherwise, false.

SetAlpha(double)

Creates a new MudColor instance with the specified alpha value while keeping the red, green, blue values unchanged.

public MudColor SetAlpha(double a)

Parameters

a double

The alpha component value (0.0 to 1.0).

Returns

MudColor

A new MudColor instance with the specified alpha component value.

SetAlpha(int)

Creates a new MudColor instance with the specified alpha value while keeping the red, green, blue values unchanged.

public MudColor SetAlpha(int a)

Parameters

a int

The alpha component value (0 to 255).

Returns

MudColor

A new MudColor instance with the specified alpha component value.

SetB(int)

Creates a new MudColor instance with the specified blue component value while keeping the red, green, and alpha values unchanged.

public MudColor SetB(int b)

Parameters

b int

The blue component value (0 to 255).

Returns

MudColor

A new MudColor instance with the specified blue component value.

SetG(int)

Creates a new MudColor instance with the specified green component value while keeping the red, blue, and alpha values unchanged.

public MudColor SetG(int g)

Parameters

g int

The green component value (0 to 255).

Returns

MudColor

A new MudColor instance with the specified green component value.

SetH(double)

Creates a new MudColor instance with the specified hue value while keeping the saturation, lightness, and alpha values unchanged.

public MudColor SetH(double h)

Parameters

h double

The hue component value (0 to 360).

Returns

MudColor

A new MudColor instance with the specified hue value.

SetL(double)

Creates a new MudColor instance with the specified lightness value while keeping the hue, saturation, and alpha values unchanged.

public MudColor SetL(double l)

Parameters

l double

The lightness component value (0.0 to 1.0).

Returns

MudColor

A new MudColor instance with the specified lightness value.

SetR(int)

Creates a new MudColor instance with the specified red component value while keeping the green, blue, and alpha values unchanged.

public MudColor SetR(int r)

Parameters

r int

The red component value (0 to 255).

Returns

MudColor

A new MudColor instance with the specified red component value.

SetS(double)

Creates a new MudColor instance with the specified saturation value while keeping the hue, lightness, and alpha values unchanged.

public MudColor SetS(double s)

Parameters

s double

The saturation component value (0.0 to 1.0).

Returns

MudColor

A new MudColor instance with the specified saturation value.

ToString()

public override string ToString()

Returns

string

ToString(MudColorOutputFormats)

Returns the string representation of the color in the specified format.

public string ToString(MudColorOutputFormats format)

Parameters

format MudColorOutputFormats

The format to represent the color.

Returns

string

A string representing the color.

ToString(string?, IFormatProvider?)

public string ToString(string? format, IFormatProvider? formatProvider)

Parameters

format string
formatProvider IFormatProvider

Returns

string

Remarks

The following formats are available:

  • rgbOutputs the color in the format "rgb(r,g,b)".
  • rgbaOutputs the color in the format "rgba(r,g,b,a)".
  • hexOutputs the color in the hexadecimal format "#rrggbb".
  • hexaOutputs the color in the hexadecimal format with alpha "#rrggbbaa".
  • colorelementsOutputs the color elements without any decorator "r,g,b".

TryParse(string?, out MudColor)

Tries to parse a string representation of a color to a MudColor instance.

public static bool TryParse(string? s, out MudColor result)

Parameters

s string

The string representation of the color.

result MudColor

When this method returns, contains the MudColor instance equivalent to the color contained in s, if the conversion succeeded, or null if the conversion failed.

Returns

bool

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

Remarks

The color can be represented in various formats, including hexadecimal (with or without alpha), RGB, and RGBA. Examples of valid color strings:

  • Hexadecimal format: "#ab2a3d", "#ab2a3dff"
  • RGB format: "rgb(12,15,40)"
  • RGBA format: "rgba(12,15,40,0.42)"

Operators

operator ==(MudColor?, MudColor?)

Determines whether two MudColor instances are equal.

public static bool operator ==(MudColor? lhs, MudColor? rhs)

Parameters

lhs MudColor

The first MudColor instance to compare.

rhs MudColor

The second MudColor instance to compare.

Returns

bool

True if the instances are equal; otherwise, false.

explicit operator string(MudColor?)

Converts a MudColor instance to its string representation.

public static explicit operator string(MudColor? color)

Parameters

color MudColor

The MudColor instance to convert.

Returns

string

The string representation of the color.

explicit operator uint(MudColor)

Converts a MudColor instance to a 32-bit unsigned integer.

public static explicit operator uint(MudColor mudColor)

Parameters

mudColor MudColor

The MudColor instance to convert.

Returns

uint

The 32-bit unsigned integer representation of the color.

implicit operator MudColor(string)

Converts a string representation of a color to a MudColor instance.

public static implicit operator MudColor(string input)

Parameters

input string

The string representation of the color.

Returns

MudColor

A new MudColor instance representing the color.

operator !=(MudColor?, MudColor?)

Determines whether two MudColor instances are not equal.

public static bool operator !=(MudColor? lhs, MudColor? rhs)

Parameters

lhs MudColor

The first MudColor instance to compare.

rhs MudColor

The second MudColor instance to compare.

Returns

bool

True if the instances are not equal; otherwise, false.

Explicit Interface Implementations

Parse(string, IFormatProvider?)

Parses a string representation of a color to a MudColor instance.

static MudColor Parse(string s, IFormatProvider? provider = null)

Parameters

s string

The string representation of the color.

provider IFormatProvider

An optional format provider.

Returns

MudColor

A new MudColor instance representing the color.

Remarks

The color can be represented in various formats, including hexadecimal (with or without alpha), RGB, and RGBA. Examples of valid color strings:

  • Hexadecimal format: "#ab2a3d", "#ab2a3dff"
  • RGB format: "rgb(12,15,40)"
  • RGBA format: "rgba(12,15,40,0.42)"

Exceptions

ArgumentException

Thrown when the input string is null, empty or invalid color format.

TryParse(string?, IFormatProvider?, out MudColor)

Tries to parse a string representation of a color to a MudColor instance.

static bool TryParse(string? s, IFormatProvider? provider, out MudColor result)

Parameters

s string

The string representation of the color.

provider IFormatProvider

An optional format provider.

result MudColor

When this method returns, contains the MudColor instance equivalent to the color contained in s, if the conversion succeeded, or null if the conversion failed.

Returns

bool

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

Remarks

The color can be represented in various formats, including hexadecimal (with or without alpha), RGB, and RGBA. Examples of valid color strings:

  • Hexadecimal format: "#ab2a3d", "#ab2a3dff"
  • RGB format: "rgb(12,15,40)"
  • RGBA format: "rgba(12,15,40,0.42)"