Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ColorConverters.GetCompliment() method #970

Merged
merged 7 commits into from Nov 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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>