From cac788b6a69939b552f6aebc3a531f6d0ca87f77 Mon Sep 17 00:00:00 2001 From: XorogVEVO Date: Sun, 27 Feb 2022 19:59:31 +0100 Subject: [PATCH] GetClosestColor implemented --- Global.cs | 1 + Internal/Internal.cs | 8 ++++++++ UniversalExtensions.cs | 13 +++++++++++++ 3 files changed, 22 insertions(+) diff --git a/Global.cs b/Global.cs index 8355d2b..dc6c67e 100644 --- a/Global.cs +++ b/Global.cs @@ -6,6 +6,7 @@ global using System.Collections.Generic; global using System.Linq; global using System.Text; global using System.Threading.Tasks; +global using System.Drawing; global using static Xorog.UniversalExtensions.UniversalExtensionsEnums; global using static Xorog.UniversalExtensions.Internal; diff --git a/Internal/Internal.cs b/Internal/Internal.cs index 5004c58..90d2b83 100644 --- a/Internal/Internal.cs +++ b/Internal/Internal.cs @@ -76,6 +76,14 @@ internal static class Internal return _timespan.ToString(); } } + internal static int GetDiff(Color color, Color baseColor) + { + int a = color.A - baseColor.A, + r = color.R - baseColor.R, + g = color.G - baseColor.G, + b = color.B - baseColor.B; + return a * a + r * r + g * g + b * b; + } } public class InternalSheduler diff --git a/UniversalExtensions.cs b/UniversalExtensions.cs index 409f827..2f3f0b6 100644 --- a/UniversalExtensions.cs +++ b/UniversalExtensions.cs @@ -492,6 +492,19 @@ public static class UniversalExtensions return string.Concat(country.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5))); } + + /// + /// Get closest Color to given Color + /// + /// + /// + /// + public static Color GetClosestColor(List colorArray, Color baseColor) + { + var colors = colorArray.Select(x => new { Value = x, Diff = Internal.GetDiff(x, baseColor) }).ToList(); + var min = colors.Min(x => x.Diff); + return colors.Find(x => x.Diff == min).Value; + } } public static class StringExt