From 71f92994857e57000f4b200f69345e9ea3c366dd Mon Sep 17 00:00:00 2001 From: "Sekan, Tobias" Date: Mon, 26 Oct 2020 13:28:22 +0100 Subject: [PATCH] Add HSI calculation --- .../ColorPickerUI/Helpers/ColorHelper.cs | 14 ++++++++++++++ .../Helpers/ColorRepresentationHelper.cs | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/modules/colorPicker/ColorPickerUI/Helpers/ColorHelper.cs b/src/modules/colorPicker/ColorPickerUI/Helpers/ColorHelper.cs index ac7eddbc85..213d8dbd68 100644 --- a/src/modules/colorPicker/ColorPickerUI/Helpers/ColorHelper.cs +++ b/src/modules/colorPicker/ColorPickerUI/Helpers/ColorHelper.cs @@ -52,6 +52,20 @@ namespace ColorPicker.Helpers internal static (double hue, double saturation, double brightness) ConvertToHSBColor(Color color) => (color.GetHue(), color.GetSaturation(), color.GetBrightness()); + /// + /// Convert a given to a HSI color (hue, saturation, intensity) + /// + /// The to convert + /// The hue [0°..360°], saturation [0..1] and intensity [0..1] of the converted color + 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); + } + /// /// Convert a given to a HSL color (hue, saturation, lightness) /// diff --git a/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs b/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs index cdec717177..7336d0c2a9 100644 --- a/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs +++ b/src/modules/colorPicker/ColorPickerUI/Helpers/ColorRepresentationHelper.cs @@ -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)}%)"; } + /// + /// Return a representation of a HSI color + /// + /// The for the HSI color presentation + /// A representation of a HSI color + 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)}%)"; + } + /// /// Return a representation of a HSL color ///