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
r
byteThe red component value (0 to 255).
g
byteThe green component value (0 to 255).
b
byteThe blue component value (0 to 255).
color
MudColorThe 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
byteThe red component value (0 to 255).
g
byteThe green component value (0 to 255).
b
byteThe blue component value (0 to 255).
a
byteThe 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
doubleThe hue component value (0 to 360).
s
doubleThe saturation component value (0.0 to 1.0).
l
doubleThe lightness component value (0.0 to 1.0).
a
doubleThe 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
doubleThe hue component value (0 to 360).
s
doubleThe saturation component value (0.0 to 1.0).
l
doubleThe lightness component value (0.0 to 1.0).
a
intThe 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
intThe red component value (0 to 255).
g
intThe green component value (0 to 255).
b
intThe blue component value (0 to 255).
alpha
doubleThe 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
intThe red component value (0 to 255).
g
intThe green component value (0 to 255).
b
intThe blue component value (0 to 255).
alpha
intThe alpha component value (0 to 255).
MudColor(SerializationInfo, StreamingContext)
Deserialization constructor for MudColor.
protected MudColor(SerializationInfo info, StreamingContext context)
Parameters
info
SerializationInfoThe SerializationInfo> containing the serialized data.
context
StreamingContextThe 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
stringThe 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
uintthe 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
amount
doubleThe 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
amount
doubleThe amount to darken the color by.
Returns
ColorLighten(double)
Creates a new MudColor instance by lightening the color.
public MudColor ColorLighten(double amount)
Parameters
amount
doubleThe 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
r
byteThe red component value (0 to 255).
g
byteThe green component value (0 to 255).
b
byteThe 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
byteThe red component value (0 to 255).
g
byteThe green component value (0 to 255).
b
byteThe blue component value (0 to 255).
a
byteThe 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
obj
object
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
baseColor
MudColorThe base color to generate the analogous palette from.
numberOfColors
intThe total number of colors in the analogous palette.
angle
doubleThe 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
MudColorThe starting color of the gradient.
endColor
MudColorThe ending color of the gradient.
numberOfColors
intThe 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
intThe 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
MudColorThe base color to generate the palette from.
numberOfColors
intThe total number of colors in the palette.
tintStep
doubleThe step value for lightening the color. If
tintStep
is0
, no lighter colors will be added to the palette.shadeStep
doubleThe step value for darkening the color. If
shadeStep
is0
, 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
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
colorStart
MudColorThe starting MudColor instance.
colorEnd
MudColorThe ending MudColor instance.
t
floatThe 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
a
doubleThe 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
a
intThe 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
b
intThe 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
g
intThe 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
h
doubleThe 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
l
doubleThe 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
r
intThe 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
s
doubleThe 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
format
MudColorOutputFormatsThe format to represent the color.
Returns
- string
A string representing the color.
ToString(string?, IFormatProvider?)
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
format
stringformatProvider
IFormatProvider
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
s
stringThe string representation of the color.
result
MudColorWhen this method returns, contains the MudColor instance equivalent to the color contained in
s
, if the conversion succeeded, ornull
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
MudColorThe first MudColor instance to compare.
rhs
MudColorThe 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
MudColorThe 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
MudColorThe 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
stringThe 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
lhs
MudColorThe first MudColor instance to compare.
rhs
MudColorThe 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
stringThe string representation of the color.
provider
IFormatProviderAn 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
s
stringThe string representation of the color.
provider
IFormatProviderAn optional format provider.
result
MudColorWhen this method returns, contains the MudColor instance equivalent to the color contained in
s
, if the conversion succeeded, ornull
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)"