using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended
{
///
/// Represents a color in the HSL (Hue, Saturation, Lightness) color space.
///
///
///
/// - Hue (H) represents the color, ranging from 0 to 360 degrees on the color wheel.
/// - Saturation (S) represents the intensity of the color, ranging from 0.0 (gray) to 1.0 (full color).
/// - Lightness (L) represents the brightness, ranging from 0.0 (black) to 1.0 (white).
///
///
[StructLayout(LayoutKind.Sequential)]
public struct HslColor : IEquatable, IComparable
{
private float _h;
private float _s;
private float _l;
///
/// The hue component value (in degrees) of the color ranging from 0.0 to 360.0.
///
public readonly float H => _h;
///
/// The saturation component value of the color ranging from 0.0 to 1.0.
///
public readonly float S => _s;
///
/// The lightness component value of the color ranging from 0.0 to 1.0.
///
public readonly float L => _l;
///
/// Normalizes a hue value to be within the range [0, 360).
/// Handles negative values by wrapping them around.
///
/// The hue value to normalize.
/// The normalized hue value.
private static float NormalizeHue(float h)
{
if (h < 0) return h + 360 * ((int)(h / 360) + 1);
return h % 360;
}
///
/// Initializes a new instance of the struct with the specified hue, saturation,
/// and lightness component values.
///
/// The hue component value (in degrees) from 0.0 to 360.0.
/// The saturation component value from 0.0 to 1.0.
/// The lightness component value from 0.0 to 1.0.
public HslColor(float h, float s, float l)
{
_h = Math.Clamp(h, 0.0f, 360.0f);
_s = Math.Clamp(s, 0.0f, 1.0f);
_l = Math.Clamp(l, 0.0f, 1.0f);
}
///
/// Copies the values of this struct to a new instance.
///
/// When this method returns, contains a copy of this .
[Obsolete("Use CopyToRef instead. This will be removed in the next major SemVer release.")]
public readonly void CopyTo(out HslColor destination)
{
destination = new HslColor(H, S, L);
}
///
/// Copies the value of this struct to an existing destination.
///
/// A reference to the destination struct where values will be copied to.
///
/// This method directly modifies the internal components of the destination struct for improved performance.
/// Unlike typical operations on immutable structs, this method does not create a new instance but alters
/// the existing one in-place. It should be used only in scenarios where performance is critical.
///
public readonly void CopyToRef(ref HslColor destination)
{
destination._h = _h;
destination._s = _s;
destination._l = _l;
}
///
/// Deconstructs this into its hue, saturation, and lightness component values.
///
/// When this method returns, contains the hue component value of this .
/// When this method returns, contains the saturation component value of this .
/// When this method returns, contains the lightness component value of this .
[Obsolete("Will be removed in next major SemVer release. Use Deconstruct instead.")]
public readonly void Destructure(out float h, out float s, out float l)
{
h = H;
s = S;
l = L;
}
///
/// Deconstructs this into its hue, saturation, and lightness component values.
///
/// When this method returns, contains the hue component value of this .
/// When this method returns, contains the saturation component value of this .
/// When this method returns, contains the lightness component value of this .
public readonly void Deconstruct(out float h, out float s, out float l)
{
h = H;
s = S;
l = L;
}
///
/// Executes a callback with the components of this .
///
/// The callback to execute.
///
/// Thrown when is .
///
public readonly void Match(Action callback)
{
ArgumentNullException.ThrowIfNull(callback);
callback(H, S, L);
}
///
/// Maps the components of this to a new value using the specified mapping function.
///
/// The type of the result of the mapping function.
/// The mapping function to apply to the components of this .
///
/// The result of applying the mapping function to the components of this .
///
///
/// Thrown when is .
///
public readonly T Map(Func map)
{
ArgumentNullException.ThrowIfNull(map);
return map(H, S, L);
}
///
/// Implicitly converts a string to an .
///
/// The string to convert.
/// The represented by the string.
[Obsolete("Use HslColor.Parse instead to make string parsing explicit and improve code readability. This method will be removed in the next major SemVer release.")]
public static implicit operator HslColor(string value)
{
return Parse(value);
}
///
///
/// This comparison uses a lexicographic approach that establishes a hierarchy among the HSL components:
///
///
/// - Hue is the primary sorting factor
/// - Lightness is the secondary sorting factor
/// - Saturation is the tertiary sorting factor
///
///
/// The comparison returns the result of the first differing component, ensuring that hue differences
/// take precedence over lightness differences, which in turn take precedence over saturation differences.
/// This creates a consistent and predictable ordering for color intervals while prioritizing the most
/// visually significant color properties.
///
public readonly int CompareTo(HslColor other)
{
int result = _h.CompareTo(other._h);
if (result != 0)
{
return result;
}
result = _l.CompareTo(other._l);
if (result != 0)
{
return result;
}
return _s.CompareTo(other._s);
}
///
public override bool Equals([NotNullWhen(true)] object obj)
{
return obj is HslColor other && Equals(other);
}
///
public readonly bool Equals(HslColor value)
{
return H.Equals(value.H) &&
L.Equals(value.L) &&
S.Equals(value.S);
}
///
public override readonly int GetHashCode()
{
return H.GetHashCode() ^
S.GetHashCode() ^
L.GetHashCode();
}
///
public override readonly string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "H:{0:N1}° S:{1:N1} L:{2:N1}",
H, 100 * S, 100 * L);
}
///
/// Parses a string into an .
///
/// The string to parse.
/// The represented by the string.
///
/// The input string should be in the format "hue,saturation,lightness", where hue is in degrees
/// (optionally followed by the '°' symbol), and saturation and lightness are decimal values.
///
public static HslColor Parse(string s)
{
var hsl = s.Split(',');
var hue = float.Parse(hsl[0].TrimEnd('°'), CultureInfo.InvariantCulture.NumberFormat);
var sat = float.Parse(hsl[1], CultureInfo.InvariantCulture.NumberFormat);
var lig = float.Parse(hsl[2], CultureInfo.InvariantCulture.NumberFormat);
return new HslColor(hue, sat, lig);
}
///
/// Determines whether two values are equal.
///
/// The first to compare.
/// The second to compare.
///
/// if the values are equal; otherwise, .
///
public static bool operator ==(HslColor x, HslColor y)
{
return x.Equals(y);
}
///
/// Determines whether two values are not equal.
///
/// The first to compare.
/// The second to compare.
///
/// if the values are not equal; otherwise, .
///
public static bool operator !=(HslColor x, HslColor y)
{
return !x.Equals(y);
}
///
/// Adds two values together.
///
/// The first to add.
/// The second to add.
///
/// A new value where the hue, saturation, and light component values are the sum of
/// the components of the two input colors.
///
public static HslColor operator +(HslColor a, HslColor b)
{
return new HslColor(
a._h + b._h,
a._s + b._s,
a._l + b._l
);
}
///
/// Subtracts one value from another.
///
/// The to subtract from.
/// The to subtract.
///
/// A new value where the hue, saturation, and light component values are the difference
/// of the components of the two input colors.
///
public static HslColor operator -(HslColor a, HslColor b)
{
return new HslColor(
a._h - b._h,
a._s - b._s,
a._l - b._l
);
}
///
/// Linearly interpolates between two values.
///
/// The first .
/// The second .
/// The interpolation factor. A value of 0 returns , a value of 1 returns .
/// The interpolated .
public static HslColor Lerp(HslColor c1, HslColor c2, float t)
{
// loop around if c2.H < c1.HF
var h2 = c2.H >= c1.H ? c2.H : c2.H + 360;
return new HslColor(
c1.H + t * (h2 - c1.H),
c1.S + t * (c2.S - c1.S),
c1.L + t * (c2.L - c1.L));
}
///
/// Convers a value to a value.
///
/// The value to convert.
///
/// A value representing the RGB equivalent of the specified
/// value.
///
public static Color ToRgb(HslColor hsl)
{
float h = hsl._h;
float s = hsl._s;
float l = hsl._l;
if (s < MathExtended.MachineEpsilon)
{
return new Color(l, l, l);
}
if (l <= MathExtended.MachineEpsilon)
{
return Color.Black;
}
h /= 360.0f;
float max = l < 0.5f ?
l * (1 + s) :
l + s - l * s;
float min = 2.0f * l - max;
float r = RgbFromHue(min, max, h + 0.3333333f);
float g = RgbFromHue(min, max, h);
float b = RgbFromHue(min, max, h - 0.3333333f);
return new Color(r, g, b);
}
private static float RgbFromHue(float min, float max, float hue)
{
hue = (hue + 1.0f) % 1.0f;
if (hue * 6.0f < 1.0f)
{
return min + (max - min) * 6.0f * hue;
}
if (hue * 2.0f < 1.0f)
{
return max;
}
if (hue * 3.0f < 2.0f)
{
return min + (max - min) * (2.0f / 3.0f - hue) * 6.0f;
}
return min;
}
///
/// Converts an RGB color to an HSL color.
///
/// The RGB color to convert.
/// The equivalent HSL color.
public static HslColor FromRgb(Color color)
{
float r = color.R / 255f;
float g = color.G / 255f;
float b = color.B / 255f;
float max = MathF.Max(r, MathF.Max(g, b));
float min = MathF.Min(r, MathF.Min(g, b));
float delta = max - min;
float h = 0.0f;
float s = 0.0f;
float l = (max + min) * 0.5f;
if (MathF.Abs(delta) < float.Epsilon)
{
return new HslColor(h, s, l);
}
if (MathF.Abs(r - max) < float.Epsilon)
{
h = (g - b) / delta;
}
else if (MathF.Abs(g - max) < float.Epsilon)
{
h = (b - r) / delta + 2.0f;
}
else if (MathF.Abs(b - max) < float.Epsilon)
{
h = (r - g) / delta + 4.0f;
}
h *= 60.0f;
h = NormalizeHue(h);
if (l <= 0.5f)
{
s = delta / (max + min);
}
else
{
s = delta / (2.0f - max - min);
}
return new HslColor(h, s, l);
}
}
}