Skip to content

Commit

Permalink
Add ColorConverters.GetCompliment() method (#970)
Browse files Browse the repository at this point in the history
* Add ColoConverter.GetCompliment method

+ tests & docs
For #957

* Add ColoConverter.GetCompliment method

+ tests & docs
For #957

* Update index files
  • Loading branch information
mrlacey authored and jamesmontemagno committed Nov 16, 2019
1 parent c629fa5 commit 34110b5
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Tests/Color_Tests.cs
Expand Up @@ -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()
{
Expand Down
18 changes: 18 additions & 0 deletions Xamarin.Essentials/Types/ColorConverters.shared.cs
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-android.xml
Expand Up @@ -117,6 +117,7 @@
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsl(System.Single,System.Single,System.Single)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsla(System.Single,System.Single,System.Single,System.Int32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromUInt(System.UInt32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.GetCompliment(System.Drawing.Color)" />
</Type>
<Type Name="Xamarin.Essentials.ColorExtensions" Id="T:Xamarin.Essentials.ColorExtensions">
<Member Id="M:Xamarin.Essentials.ColorExtensions.AddLuminosity(System.Drawing.Color,System.Single)" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-ios.xml
Expand Up @@ -117,6 +117,7 @@
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsl(System.Single,System.Single,System.Single)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsla(System.Single,System.Single,System.Single,System.Int32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromUInt(System.UInt32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.GetCompliment(System.Drawing.Color)" />
</Type>
<Type Name="Xamarin.Essentials.ColorExtensions" Id="T:Xamarin.Essentials.ColorExtensions">
<Member Id="M:Xamarin.Essentials.ColorExtensions.AddLuminosity(System.Drawing.Color,System.Single)" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-tvos.xml
Expand Up @@ -117,6 +117,7 @@
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsl(System.Single,System.Single,System.Single)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsla(System.Single,System.Single,System.Single,System.Int32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromUInt(System.UInt32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.GetCompliment(System.Drawing.Color)" />
</Type>
<Type Name="Xamarin.Essentials.ColorExtensions" Id="T:Xamarin.Essentials.ColorExtensions">
<Member Id="M:Xamarin.Essentials.ColorExtensions.AddLuminosity(System.Drawing.Color,System.Single)" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-uwp.xml
Expand Up @@ -117,6 +117,7 @@
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsl(System.Single,System.Single,System.Single)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsla(System.Single,System.Single,System.Single,System.Int32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromUInt(System.UInt32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.GetCompliment(System.Drawing.Color)" />
</Type>
<Type Name="Xamarin.Essentials.ColorExtensions" Id="T:Xamarin.Essentials.ColorExtensions">
<Member Id="M:Xamarin.Essentials.ColorExtensions.AddLuminosity(System.Drawing.Color,System.Single)" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials-watchos.xml
Expand Up @@ -117,6 +117,7 @@
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsl(System.Single,System.Single,System.Single)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsla(System.Single,System.Single,System.Single,System.Int32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromUInt(System.UInt32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.GetCompliment(System.Drawing.Color)" />
</Type>
<Type Name="Xamarin.Essentials.ColorExtensions" Id="T:Xamarin.Essentials.ColorExtensions">
<Member Id="M:Xamarin.Essentials.ColorExtensions.AddLuminosity(System.Drawing.Color,System.Single)" />
Expand Down
1 change: 1 addition & 0 deletions docs/en/FrameworksIndex/xamarin-essentials.xml
Expand Up @@ -117,6 +117,7 @@
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsl(System.Single,System.Single,System.Single)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromHsla(System.Single,System.Single,System.Single,System.Int32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.FromUInt(System.UInt32)" />
<Member Id="M:Xamarin.Essentials.ColorConverters.GetCompliment(System.Drawing.Color)" />
</Type>
<Type Name="Xamarin.Essentials.ColorExtensions" Id="T:Xamarin.Essentials.ColorExtensions">
<Member Id="M:Xamarin.Essentials.ColorExtensions.AddLuminosity(System.Drawing.Color,System.Single)" />
Expand Down
24 changes: 24 additions & 0 deletions docs/en/Xamarin.Essentials/ColorConverters.xml
Expand Up @@ -123,5 +123,29 @@
</remarks>
</Docs>
</Member>
<Member MemberName="GetCompliment">
<MemberSignature Language="C#" Value="public static System.Drawing.Color GetCompliment (System.Drawing.Color original);" />
<MemberSignature Language="ILAsm" Value=".method public static hidebysig valuetype System.Drawing.Color GetCompliment(valuetype System.Drawing.Color original) cil managed" />
<MemberSignature Language="DocId" Value="M:Xamarin.Essentials.ColorConverters.GetCompliment(System.Drawing.Color)" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyName>Xamarin.Essentials</AssemblyName>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Drawing.Color</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="original" Type="System.Drawing.Color" />
</Parameters>
<Docs>
<param name="original">A color to obtain the compliment for.</param>
<summary>Returns a new color that is on the opposite side of the color wheel from the original.</summary>
<returns>A color that is the compliment of the value passed in.</returns>
<remarks>
<para />
</remarks>
</Docs>
</Member>
</Members>
</Type>

0 comments on commit 34110b5

Please sign in to comment.