Remove readonly keyword from FromHexString in Color (whoops!), add docs to Color.

This commit is contained in:
2025-06-24 23:11:12 +02:00
parent 255dea138b
commit b2f3e1c351

View File

@@ -147,11 +147,28 @@ namespace Voile
public static readonly Color Yellow = new(0xFFFF00);
public static readonly Color YellowGreen = new(0x9ACD32);
/// <summary>
/// Red component of this <see cref="Color"/>.
/// </summary>
public byte R { get; set; }
/// <summary>
/// Green component of this <see cref="Color"/>.
/// </summary>
public byte G { get; set; }
/// <summary>
/// Blue component of this <see cref="Color"/>.
/// </summary>
public byte B { get; set; }
/// <summary>
/// Alpha component of this <see cref="Color"/>.
/// </summary>/// <summary>
/// Gets the color as a 32-bit ARGB integer in the format 0xAARRGGBB.
/// </summary>
public byte A { get; set; } = 255;
/// <summary>
/// Gets the color as a 32-bit ARGB integer in the format 0xAARRGGBB.
/// </summary>
public int Argb
{
get
@@ -165,6 +182,13 @@ namespace Voile
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using float RGB(A) values between 0 and 1.
/// </summary>
/// <param name="r">The red component (0.0 to 1.0).</param>
/// <param name="g">The green component (0.0 to 1.0).</param>
/// <param name="b">The blue component (0.0 to 1.0).</param>
/// <param name="a">The alpha component (0.0 to 1.0), default is 1.0 (fully opaque).</param>
public Color(float r, float g, float b, float a = 1.0f)
{
R = (byte)Math.Clamp(r * 255, 0, 255);
@@ -173,6 +197,13 @@ namespace Voile
A = (byte)Math.Clamp(a * 255, 0, 255);
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using byte RGB(A) values.
/// </summary>
/// <param name="r">The red component (0 to 255).</param>
/// <param name="g">The green component (0 to 255).</param>
/// <param name="b">The blue component (0 to 255).</param>
/// <param name="a">The alpha component (0 to 255), default is 255 (fully opaque).</param>
public Color(byte r, byte g, byte b, byte a = 255)
{
R = r;
@@ -181,6 +212,13 @@ namespace Voile
A = a;
}
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct using a hexadecimal value.
/// </summary>
/// <param name="hex">
/// A 24-bit (RRGGBB) or 32-bit (AARRGGBB) integer representing the color.
/// Alpha is assumed to be 255 if not included.
/// </param>
public Color(int hex)
{
A = 255; // Default alpha to 255 if not provided
@@ -193,7 +231,13 @@ namespace Voile
}
}
public static readonly Color FromHexString(string hex)
/// <summary>
/// Parses a color from a hexadecimal string in the format "#RRGGBB" or "#AARRGGBB".
/// </summary>
/// <param name="hex">The hex string representing the color.</param>
/// <returns>A <see cref="Color"/> instance parsed from the string.</returns>
/// <exception cref="ArgumentException">Thrown if the format is invalid.</exception>
public static Color FromHexString(string hex)
{
if (hex.StartsWith("#"))
{
@@ -216,6 +260,11 @@ namespace Voile
}
}
/// <summary>
/// Returns a lightened version of the color by interpolating toward white.
/// </summary>
/// <param name="amount">A value from 0.0 (no change) to 1.0 (fully white).</param>
/// <returns>A lighter <see cref="Color"/>.</returns>
public Color Lightened(float amount)
{
var result = this;
@@ -225,6 +274,11 @@ namespace Voile
return result;
}
/// <summary>
/// Returns a darkened version of the color by interpolating toward black.
/// </summary>
/// <param name="amount">A value from 0.0 (no change) to 1.0 (fully black).</param>
/// <returns>A darker <see cref="Color"/>.</returns>
public Color Darkened(float amount)
{
var result = this;
@@ -234,6 +288,10 @@ namespace Voile
return result;
}
/// <summary>
/// Converts this color to a <see cref="System.Drawing.Color"/>.
/// </summary>
/// <returns>A <see cref="System.Drawing.Color"/> with equivalent ARGB values.</returns>
public System.Drawing.Color ToSystemColor()
{
var result = System.Drawing.Color.FromArgb(Argb);