Add HSI calculation

This commit is contained in:
Sekan, Tobias 2020-10-26 13:28:22 +01:00
parent 57dd0ce113
commit 71f9299485
2 changed files with 33 additions and 0 deletions

View File

@ -52,6 +52,20 @@ namespace ColorPicker.Helpers
internal static (double hue, double saturation, double brightness) ConvertToHSBColor(Color color)
=> (color.GetHue(), color.GetSaturation(), color.GetBrightness());
/// <summary>
/// Convert a given <see cref="Color"/> to a HSI color (hue, saturation, intensity)
/// </summary>
/// <param name="color">The <see cref="Color"/> to convert</param>
/// <returns>The hue [0°..360°], saturation [0..1] and intensity [0..1] of the converted color</returns>
internal static (double hue, double saturation, double intensity) ConvertToHSIColor(Color color)
{
var min = Math.Min(Math.Min(color.R, color.G), color.B) / 255d;
var intensity = 1d / 3d * (color.R + color.G + color.B);
return (color.GetHue(), intensity == 0d ? 0d : 1d - (min / intensity), intensity);
}
/// <summary>
/// Convert a given <see cref="Color"/> to a HSL color (hue, saturation, lightness)
/// </summary>

View File

@ -27,6 +27,7 @@ namespace ColorPicker.Helpers
ColorRepresentationType.NCol => ColorToNCol(color),
ColorRepresentationType.HEX => ColorToHex(color),
ColorRepresentationType.HSB => ColorToHSB(color),
ColorRepresentationType.HSI => ColorToHSI(color),
ColorRepresentationType.HSL => ColorToHSL(color),
ColorRepresentationType.HSV => ColorToHSV(color),
ColorRepresentationType.HWB => ColorToHWB(color),
@ -101,6 +102,24 @@ namespace ColorPicker.Helpers
+ $", {brightnes.ToString(CultureInfo.InvariantCulture)}%)";
}
/// <summary>
/// Return a <see cref="string"/> representation of a HSI color
/// </summary>
/// <param name="color">The <see cref="Color"/> for the HSI color presentation</param>
/// <returns>A <see cref="string"/> representation of a HSI color</returns>
private static string ColorToHSI(Color color)
{
var (hue, saturation, intensity) = ColorHelper.ConvertToHSIColor(color);
hue = Math.Round(hue);
saturation = Math.Round(saturation * 100);
intensity = Math.Round(intensity * 100);
return $"hsi({hue.ToString(CultureInfo.InvariantCulture)}"
+ $", {saturation.ToString(CultureInfo.InvariantCulture)}%"
+ $", {intensity.ToString(CultureInfo.InvariantCulture)}%)";
}
/// <summary>
/// Return a <see cref="string"/> representation of a HSL color
/// </summary>