Skip to content

Instantly share code, notes, and snippets.

@crozone
Created May 7, 2024 02:55
Show Gist options
  • Save crozone/161f4637274f35ffc23a0821a1a9e719 to your computer and use it in GitHub Desktop.
Save crozone/161f4637274f35ffc23a0821a1a9e719 to your computer and use it in GitHub Desktop.
HSL to RGB Color conversion
internal static class ColorHelpers
{
/// <summary>
/// Converts HSL color value to RGB fractional color value
/// </summary>
/// <param name="hue">Hue angle value between [0,360]</param>
/// <param name="saturation">Saturation value between [0,1]</param>
/// <param name="lightness">Lightness value between [0,1]</param>
/// <returns>RGB color values, with each value between [0,1]</returns>
/// <see href="https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative"/>
public static (double r, double g, double b) HslToRgb(double hue, double saturation, double lightness)
{
// Normalize hue angle
hue %= 360;
if (hue < 0)
{
hue += 360;
}
double a = saturation * Math.Min(lightness, 1 - lightness);
double GetChannel(int n)
{
double k = (n + hue / 30) % 12;
return (lightness - a * Math.Max(-1, Math.Min(Math.Min(k - 3, 9 - k), 1)));
}
return (GetChannel(0), GetChannel(8), GetChannel(4));
}
/// <summary>
/// Converts HSL color value to RGB integer color value
/// </summary>
/// <param name="hue">Hue angle value between [0,360]</param>
/// <param name="saturation">Saturation value between [0,1]</param>
/// <param name="lightness">Lightness value between [0,1]</param>
/// <returns>The <see cref="Color"/> equivalent of the input HSL values</returns>
public static Color HslToRgbColor(double hue, double saturation, double lightness)
{
(double r, double g, double b) = HslToRgb(hue, saturation, lightness);
return Color.FromArgb(
(byte)Math.Min(255, r * 256),
(byte)Math.Min(255, g * 256),
(byte)Math.Min(255, b * 256)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment