diff --git a/Tests/Color_Tests.cs b/Tests/Color_Tests.cs index 47aefa896..ca11de5dc 100644 --- a/Tests/Color_Tests.cs +++ b/Tests/Color_Tests.cs @@ -60,6 +60,19 @@ public void FromHex(string hex, int a, int r, int g, int b) Assert.Equal(b, color.B); } + [Theory] + [InlineData("#FF0000", "#00FFFF")] // Red & Cyan + [InlineData("#00FF00", "#FF00FF")] // Green & Fuschia + [InlineData("#0000FF", "#FFFF00")] // Blue & Yellow + [InlineData("#0AF56C", "#F50A93")] // Lime green & bright purple (but with no limit values) + public void GetCompliment(string original, string expected) + { + var orig = ColorConverters.FromHex(original); + var expectedCompliment = ColorConverters.FromHex(expected); + + Assert.Equal(expectedCompliment, ColorConverters.GetCompliment(orig)); + } + [Fact] public void FromHsla() { diff --git a/Xamarin.Essentials/Types/ColorConverters.shared.cs b/Xamarin.Essentials/Types/ColorConverters.shared.cs index 42c8a256d..aa7ea3cd2 100644 --- a/Xamarin.Essentials/Types/ColorConverters.shared.cs +++ b/Xamarin.Essentials/Types/ColorConverters.shared.cs @@ -71,6 +71,24 @@ public static Color FromUInt(uint argb) return Color.FromArgb(a, r, g, b); } + public static Color GetCompliment(Color original) + { + // Divide RGB by 255 as ConvertToHsl expects a value between 0 & 1. + ConvertToHsl(original.R / 255f, original.G / 255f, original.B / 255f, out var h, out var s, out var l); + + // Multiply by 360 as `ConvertToHsl` specifies it as a value between 0 and 1. + h *= 360; + + // Add 180 (degrees) to get to the other side of the circle. + h += 180; + + // Ensure still within the bounds of a circle. + h %= 360; + + // multiply by 100 as `ConvertToHsl` specifies them as values between 0 and 1. + return FromHsl(h, s * 100, l * 100); + } + internal static void ConvertToRgb(float hue, float saturation, float luminosity, out int r, out int g, out int b) { if (luminosity == 0) diff --git a/docs/en/FrameworksIndex/xamarin-essentials-android.xml b/docs/en/FrameworksIndex/xamarin-essentials-android.xml index 5a2ba4ce1..0c27d335a 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-android.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-android.xml @@ -117,6 +117,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials-ios.xml b/docs/en/FrameworksIndex/xamarin-essentials-ios.xml index d4031d0ff..c68d47a07 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-ios.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-ios.xml @@ -117,6 +117,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials-tvos.xml b/docs/en/FrameworksIndex/xamarin-essentials-tvos.xml index 87ac8caec..a4d9bbdeb 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-tvos.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-tvos.xml @@ -117,6 +117,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml b/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml index 03a1ad443..6d64e4868 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-uwp.xml @@ -117,6 +117,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials-watchos.xml b/docs/en/FrameworksIndex/xamarin-essentials-watchos.xml index 3998f5e9f..cf498c649 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials-watchos.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials-watchos.xml @@ -117,6 +117,7 @@ + diff --git a/docs/en/FrameworksIndex/xamarin-essentials.xml b/docs/en/FrameworksIndex/xamarin-essentials.xml index 31cd40e98..781af10c1 100644 --- a/docs/en/FrameworksIndex/xamarin-essentials.xml +++ b/docs/en/FrameworksIndex/xamarin-essentials.xml @@ -117,6 +117,7 @@ + diff --git a/docs/en/Xamarin.Essentials/ColorConverters.xml b/docs/en/Xamarin.Essentials/ColorConverters.xml index 6bb3c427a..39fe1008c 100644 --- a/docs/en/Xamarin.Essentials/ColorConverters.xml +++ b/docs/en/Xamarin.Essentials/ColorConverters.xml @@ -123,5 +123,29 @@ + + + + + Method + + Xamarin.Essentials + 1.0.0.0 + + + System.Drawing.Color + + + + + + A color to obtain the compliment for. + Returns a new color that is on the opposite side of the color wheel from the original. + A color that is the compliment of the value passed in. + + + + +