mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
Add HSI calculation
This commit is contained in:
@@ -52,6 +52,20 @@ namespace ColorPicker.Helpers
|
|||||||
internal static (double hue, double saturation, double brightness) ConvertToHSBColor(Color color)
|
internal static (double hue, double saturation, double brightness) ConvertToHSBColor(Color color)
|
||||||
=> (color.GetHue(), color.GetSaturation(), color.GetBrightness());
|
=> (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>
|
/// <summary>
|
||||||
/// Convert a given <see cref="Color"/> to a HSL color (hue, saturation, lightness)
|
/// Convert a given <see cref="Color"/> to a HSL color (hue, saturation, lightness)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ namespace ColorPicker.Helpers
|
|||||||
ColorRepresentationType.NCol => ColorToNCol(color),
|
ColorRepresentationType.NCol => ColorToNCol(color),
|
||||||
ColorRepresentationType.HEX => ColorToHex(color),
|
ColorRepresentationType.HEX => ColorToHex(color),
|
||||||
ColorRepresentationType.HSB => ColorToHSB(color),
|
ColorRepresentationType.HSB => ColorToHSB(color),
|
||||||
|
ColorRepresentationType.HSI => ColorToHSI(color),
|
||||||
ColorRepresentationType.HSL => ColorToHSL(color),
|
ColorRepresentationType.HSL => ColorToHSL(color),
|
||||||
ColorRepresentationType.HSV => ColorToHSV(color),
|
ColorRepresentationType.HSV => ColorToHSV(color),
|
||||||
ColorRepresentationType.HWB => ColorToHWB(color),
|
ColorRepresentationType.HWB => ColorToHWB(color),
|
||||||
@@ -101,6 +102,24 @@ namespace ColorPicker.Helpers
|
|||||||
+ $", {brightnes.ToString(CultureInfo.InvariantCulture)}%)";
|
+ $", {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>
|
/// <summary>
|
||||||
/// Return a <see cref="string"/> representation of a HSL color
|
/// Return a <see cref="string"/> representation of a HSL color
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user