Class MudColor
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
rbyteThe red component value (0 to 255).
gbyteThe green component value (0 to 255).
bbyteThe blue component value (0 to 255).
colorMudColorThe 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
rbyteThe red component value (0 to 255).
gbyteThe green component value (0 to 255).
bbyteThe blue component value (0 to 255).
abyteThe 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
hdoubleThe hue component value (0 to 360).
sdoubleThe saturation component value (0.0 to 1.0).
ldoubleThe lightness component value (0.0 to 1.0).
adoubleThe 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
hdoubleThe hue component value (0 to 360).
sdoubleThe saturation component value (0.0 to 1.0).
ldoubleThe lightness component value (0.0 to 1.0).
aintThe 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
rintThe red component value (0 to 255).
gintThe green component value (0 to 255).
bintThe blue component value (0 to 255).
alphadoubleThe 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
rintThe red component value (0 to 255).
gintThe green component value (0 to 255).
bintThe blue component value (0 to 255).
alphaintThe alpha component value (0 to 255).
MudColor(SerializationInfo, StreamingContext)
Deserialization constructor for MudColor.
protected MudColor(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoThe SerializationInfo> containing the serialized data.
contextStreamingContextThe StreamingContext>.
MudColor(string)
Initializes a new instance of the MudColor class with the specified string representation of a color.
public MudColor(string value)
Parameters
valuestringThe 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
rgbauintthe 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
APercentage
Gets the alpha component value as a percentage (0.0 to 1.0) of the color.
[JsonIgnore]
public double APercentage { get; }
Property Value
B
Gets the blue component value of the color.
public byte B { get; }
Property Value
G
Gets the green component value of the color.
public byte G { get; }
Property Value
H
Gets the hue component value of the color.
[JsonIgnore]
public double H { get; }
Property Value
L
Gets the lightness component value of the color.
[JsonIgnore]
public double L { get; }
Property Value
R
Gets the red component value of the color.
public byte R { get; }
Property Value
S
Gets the saturation component value of the color.
[JsonIgnore]
public double S { get; }
Property Value
UInt32
Gets the 32-bit unsigned integer representation of the color.
[JsonIgnore]
public uint UInt32 { get; }
Property Value
Value
Gets the hexadecimal representation of the color.
[JsonIgnore]
public string Value { get; }
Property Value
Methods
ChangeLightness(double)
Creates a new MudColor instance by adjusting the lightness component value by the specified amount.
public MudColor ChangeLightness(double amount)
Parameters
amountdoubleThe amount to adjust the lightness by (-1.0 to 1.0).
Returns
ColorDarken(double)
Creates a new MudColor instance by darkening the color.
public MudColor ColorDarken(double amount)
Parameters
amountdoubleThe amount to darken the color by.
Returns
ColorLighten(double)
Creates a new MudColor instance by lightening the color.
public MudColor ColorLighten(double amount)
Parameters
amountdoubleThe amount to lighten the color by.
Returns
ColorRgbDarken()
Creates a new MudColor instance by darkening the color using the RGB algorithm.
public MudColor ColorRgbDarken()
Returns
ColorRgbLighten()
Creates a new MudColor instance by lightening the color using the RGB algorithm.
public MudColor ColorRgbLighten()
Returns
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
rbyteThe red component value (0 to 255).
gbyteThe green component value (0 to 255).
bbyteThe 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
rbyteThe red component value (0 to 255).
gbyteThe green component value (0 to 255).
bbyteThe blue component value (0 to 255).
abyteThe alpha component value (0 to 255).
Equals(MudColor?)
public bool Equals(MudColor? other)
Parameters
Returns
- bool
True if the instances are equal; otherwise, false.
Equals(object?)
public override bool Equals(object? obj)
Parameters
objobject
Returns
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
baseColorMudColorThe base color to generate the analogous palette from.
numberOfColorsintThe total number of colors in the analogous palette.
angledoubleThe angle between each color in the analogous palette.
Returns
- IEnumerable<MudColor>
An enumerable collection of MudColor representing the analogous palette.
Exceptions
- ArgumentOutOfRangeException
Thrown when
numberOfColorsis 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
startColorMudColorThe starting color of the gradient.
endColorMudColorThe ending color of the gradient.
numberOfColorsintThe total number of colors in the gradient palette.
Returns
- IEnumerable<MudColor>
An enumerable collection of MudColor representing the gradient palette.
Exceptions
- ArgumentOutOfRangeException
Thrown when
numberOfColorsis 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
colorsIReadOnlyList<MudColor>The list of colors to generate the multi-gradient palette from. Must contain at least two colors.
numberOfColorsintThe 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
numberOfColorsis less than or equal to zero.- ArgumentException
Thrown when the
colorscollection 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
baseColorMudColorThe base color to generate the palette from.
numberOfColorsintThe total number of colors in the palette.
tintStepdoubleThe step value for lightening the color. If
tintStepis0, no lighter colors will be added to the palette.shadeStepdoubleThe step value for darkening the color. If
shadeStepis0, 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
numberOfColorsis less than or equal to zero.- ArgumentOutOfRangeException
Thrown when
tintStepis negative.- ArgumentOutOfRangeException
Thrown when
shadeStepis negative.
GetHashCode()
public override int GetHashCode()
Returns
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
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
colorStartMudColorThe starting MudColor instance.
colorEndMudColorThe ending MudColor instance.
tfloatThe interpolation factor (0.0 to 1.0).
Returns
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
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
adoubleThe alpha component value (0.0 to 1.0).
Returns
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
aintThe alpha component value (0 to 255).
Returns
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
bintThe blue component value (0 to 255).
Returns
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
gintThe green component value (0 to 255).
Returns
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
hdoubleThe hue component value (0 to 360).
Returns
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
ldoubleThe lightness component value (0.0 to 1.0).
Returns
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
rintThe red component value (0 to 255).
Returns
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
sdoubleThe saturation component value (0.0 to 1.0).
Returns
ToString()
public override string ToString()
Returns
ToString(MudColorOutputFormats)
Returns the string representation of the color in the specified format.
public string ToString(MudColorOutputFormats format)
Parameters
formatMudColorOutputFormatsThe format to represent the color.
Returns
- string
A string representing the color.
ToString(string?, IFormatProvider?)
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringformatProviderIFormatProvider
Returns
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
sstringThe string representation of the color.
resultMudColorWhen this method returns, contains the MudColor instance equivalent to the color contained in
s, if the conversion succeeded, ornullif the conversion failed.
Returns
- bool
trueif 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
lhsMudColorThe first MudColor instance to compare.
rhsMudColorThe 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
colorMudColorThe 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
mudColorMudColorThe 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
inputstringThe string representation of the color.
Returns
operator !=(MudColor?, MudColor?)
Determines whether two MudColor instances are not equal.
public static bool operator !=(MudColor? lhs, MudColor? rhs)
Parameters
lhsMudColorThe first MudColor instance to compare.
rhsMudColorThe 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
sstringThe string representation of the color.
providerIFormatProviderAn optional format provider.
Returns
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
sstringThe string representation of the color.
providerIFormatProviderAn optional format provider.
resultMudColorWhen this method returns, contains the MudColor instance equivalent to the color contained in
s, if the conversion succeeded, ornullif the conversion failed.
Returns
- bool
trueif 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)"