11 changed files with 416 additions and 144 deletions
@ -0,0 +1,135 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Drawing; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
namespace Google_Play_Music.Utilities |
|||
{ |
|||
public static class ColorMath |
|||
{ |
|||
/// <summary>
|
|||
/// Blends two colors in the specified ratio.
|
|||
/// </summary>
|
|||
/// <param name="color1">First color.</param>
|
|||
/// <param name="color2">Second color.</param>
|
|||
/// <param name="ratio">Ratio between both colors. 0 for first color, 1 for second color.</param>
|
|||
/// <returns></returns>
|
|||
public static Color Blend(Color color1, Color color2, double ratio) |
|||
{ |
|||
int a = (int)Math.Round(color1.A * (1 - ratio) + color2.A * ratio); |
|||
int r = (int)Math.Round(color1.R * (1 - ratio) + color2.R * ratio); |
|||
int g = (int)Math.Round(color1.G * (1 - ratio) + color2.G * ratio); |
|||
int b = (int)Math.Round(color1.B * (1 - ratio) + color2.B * ratio); |
|||
return Color.FromArgb(a, r, g, b); |
|||
} |
|||
|
|||
public static Color Darken(Color color, double ratio) |
|||
{ |
|||
return Blend(color, Color.Black, ratio); |
|||
} |
|||
|
|||
public static Color Lighten(Color color, double ratio) |
|||
{ |
|||
return Blend(color, Color.White, ratio); |
|||
} |
|||
|
|||
public static HslColor RgbToHsl(Color rgb) |
|||
{ |
|||
// Translated from JavaScript, part of coati
|
|||
double h, s, l; |
|||
double r = (double)rgb.R / 255; |
|||
double g = (double)rgb.G / 255; |
|||
double b = (double)rgb.B / 255; |
|||
double min = Math.Min(Math.Min(r, g), b); |
|||
double max = Math.Max(Math.Max(r, g), b); |
|||
|
|||
l = (max + min) / 2; |
|||
|
|||
if (max == min) |
|||
h = 0; |
|||
else if (max == r) |
|||
h = (60 * (g - b) / (max - min)) % 360; |
|||
else if (max == g) |
|||
h = (60 * (b - r) / (max - min) + 120) % 360; |
|||
else //if (max == b)
|
|||
h = (60 * (r - g) / (max - min) + 240) % 360; |
|||
if (h < 0) |
|||
h += 360; |
|||
|
|||
if (max == min) |
|||
s = 0; |
|||
else if (l <= 0.5) |
|||
s = (max - min) / (2 * l); |
|||
else |
|||
s = (max - min) / (2 - 2 * l); |
|||
|
|||
return new HslColor((byte)Math.Round((h / 360 * 256) % 256), (byte)Math.Round(s * 255), (byte)Math.Round(l * 255)); |
|||
} |
|||
|
|||
public static Color HslToRgb(HslColor hsl) |
|||
{ |
|||
// Translated from JavaScript, part of coati
|
|||
double h = (double)hsl.H / 256; |
|||
double s = (double)hsl.S / 255; |
|||
double l = (double)hsl.L / 255; |
|||
double q; |
|||
if (l < 0.5) |
|||
q = l * (1 + s); |
|||
else |
|||
q = l + s - l * s; |
|||
double p = 2 * l - q; |
|||
double[] t = new double[] { h + 1.0 / 3, h, h - 1.0 / 3 }; |
|||
byte[] rgb = new byte[3]; |
|||
for (int i = 0; i < 3; i++) |
|||
{ |
|||
if (t[i] < 0) t[i]++; |
|||
if (t[i] > 1) t[i]--; |
|||
if (t[i] < 1.0 / 6) |
|||
rgb[i] = (byte)Math.Round((p + ((q - p) * 6 * t[i])) * 255); |
|||
else if (t[i] < 1.0 / 2) |
|||
rgb[i] = (byte)Math.Round(q * 255); |
|||
else if (t[i] < 2.0 / 3) |
|||
rgb[i] = (byte)Math.Round((p + ((q - p) * 6 * (2.0 / 3 - t[i]))) * 255); |
|||
else |
|||
rgb[i] = (byte)Math.Round(p * 255); |
|||
} |
|||
return Color.FromArgb(rgb[0], rgb[1], rgb[2]); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Computes the real modulus value, not the division remainder.
|
|||
/// This differs from the % operator only for negative numbers.
|
|||
/// </summary>
|
|||
/// <param name="dividend">Dividend.</param>
|
|||
/// <param name="divisor">Divisor.</param>
|
|||
/// <returns></returns>
|
|||
private static int Mod(int dividend, int divisor) |
|||
{ |
|||
if (divisor <= 0) throw new ArgumentOutOfRangeException("divisor", "The divisor cannot be zero or negative."); |
|||
int i = dividend % divisor; |
|||
if (i < 0) i += divisor; |
|||
return i; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Computes the grey value value of a color.
|
|||
/// </summary>
|
|||
/// <param name="c"></param>
|
|||
/// <returns></returns>
|
|||
public static byte ToGray(Color c) |
|||
{ |
|||
return (byte)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Determines whether the color is dark or light.
|
|||
/// </summary>
|
|||
/// <param name="c"></param>
|
|||
/// <returns></returns>
|
|||
public static bool IsDarkColor(Color c) |
|||
{ |
|||
return ToGray(c) < 0x90; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,49 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
namespace Google_Play_Music.Utilities |
|||
{ |
|||
public struct HslColor |
|||
{ |
|||
private byte h, s, l; |
|||
|
|||
public byte H { get { return h; } set { h = value; } } |
|||
public byte S { get { return s; } set { s = value; } } |
|||
public byte L { get { return l; } set { l = value; } } |
|||
|
|||
public HslColor(byte h, byte s, byte l) |
|||
{ |
|||
this.h = h; |
|||
this.s = s; |
|||
this.l = l; |
|||
} |
|||
|
|||
public override bool Equals(System.Object obj) |
|||
{ |
|||
if (obj is HslColor) |
|||
{ |
|||
HslColor c = (HslColor)obj; |
|||
return (H == c.H) && (S == c.S) && (L == c.L); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
public bool Equals(HslColor c) |
|||
{ |
|||
if ((object)c == null) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
return (H == c.H) && (S == c.S) && (L == c.L); |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return "HslColor(" + H.ToString() + ", " + S.ToString() + ", " + L.ToString() + ") "; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,32 @@ |
|||
using Google_Play_Music.Utilities; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Drawing; |
|||
using System.Linq; |
|||
using System.Text; |
|||
|
|||
|
|||
namespace UnitTests.Test |
|||
{ |
|||
using NUnit.Framework; |
|||
|
|||
[TestFixture] |
|||
public class ColorMathTest |
|||
{ |
|||
[Test] |
|||
public void RGBtoHSL() |
|||
{ |
|||
// Correctly converts RGB to HSL
|
|||
Assert.AreEqual(ColorMath.RgbToHsl(Color.FromArgb(255, 0, 0)), new HslColor(0, 255, 128)); |
|||
|
|||
// Does not return True for no reason :)
|
|||
Assert.AreNotEqual(ColorMath.RgbToHsl(Color.FromArgb(42, 42, 42)), new HslColor(0, 255, 128)); |
|||
} |
|||
|
|||
[Test] |
|||
public void SystemColorToHSL() |
|||
{ |
|||
Assert.AreEqual(ColorMath.RgbToHsl(Color.Yellow), new HslColor(43, 255, 128)); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,36 @@ |
|||
using System.Reflection; |
|||
using System.Runtime.CompilerServices; |
|||
using System.Runtime.InteropServices; |
|||
|
|||
// General Information about an assembly is controlled through the following
|
|||
// set of attributes. Change these attribute values to modify the information
|
|||
// associated with an assembly.
|
|||
[assembly: AssemblyTitle("UnitTests.Test")] |
|||
[assembly: AssemblyDescription("")] |
|||
[assembly: AssemblyConfiguration("")] |
|||
[assembly: AssemblyCompany("")] |
|||
[assembly: AssemblyProduct("UnitTests.Test")] |
|||
[assembly: AssemblyCopyright("Copyright © 2015")] |
|||
[assembly: AssemblyTrademark("")] |
|||
[assembly: AssemblyCulture("")] |
|||
|
|||
// Setting ComVisible to false makes the types in this assembly not visible
|
|||
// to COM components. If you need to access a type in this assembly from
|
|||
// COM, set the ComVisible attribute to true on that type.
|
|||
[assembly: ComVisible(false)] |
|||
|
|||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
|||
[assembly: Guid("68aa8f3d-31e4-474c-a4b5-a82936750d0f")] |
|||
|
|||
// Version information for an assembly consists of the following four values:
|
|||
//
|
|||
// Major Version
|
|||
// Minor Version
|
|||
// Build Number
|
|||
// Revision
|
|||
//
|
|||
// You can specify all the values or you can default the Build and Revision Numbers
|
|||
// by using the '*' as shown below:
|
|||
// [assembly: AssemblyVersion("1.0.*")]
|
|||
[assembly: AssemblyVersion("1.0.0.0")] |
|||
[assembly: AssemblyFileVersion("1.0.0.0")] |
@ -0,0 +1,118 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> |
|||
<PropertyGroup> |
|||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|||
<ProjectGuid>{68AA8F3D-31E4-474C-A4B5-A82936750D0F}</ProjectGuid> |
|||
<OutputType>Library</OutputType> |
|||
<AppDesignerFolder>Properties</AppDesignerFolder> |
|||
<RootNamespace>UnitTests.Test</RootNamespace> |
|||
<AssemblyName>UnitTests.Test</AssemblyName> |
|||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> |
|||
<FileAlignment>512</FileAlignment> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|||
<DebugSymbols>true</DebugSymbols> |
|||
<DebugType>full</DebugType> |
|||
<Optimize>false</Optimize> |
|||
<OutputPath>bin\Debug\</OutputPath> |
|||
<DefineConstants>DEBUG;TRACE</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|||
<DebugType>pdbonly</DebugType> |
|||
<Optimize>true</Optimize> |
|||
<OutputPath>bin\Release\</OutputPath> |
|||
<DefineConstants>TRACE</DefineConstants> |
|||
<ErrorReport>prompt</ErrorReport> |
|||
<WarningLevel>4</WarningLevel> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> |
|||
<PlatformTarget>x86</PlatformTarget> |
|||
<OutputPath>bin\x86\Debug\</OutputPath> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> |
|||
<PlatformTarget>x86</PlatformTarget> |
|||
<OutputPath>bin\x86\Release\</OutputPath> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'"> |
|||
<PlatformTarget>x64</PlatformTarget> |
|||
<OutputPath>bin\x64\Debug\</OutputPath> |
|||
</PropertyGroup> |
|||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> |
|||
<PlatformTarget>x64</PlatformTarget> |
|||
<OutputPath>bin\x64\Release\</OutputPath> |
|||
</PropertyGroup> |
|||
<ItemGroup> |
|||
<Reference Include="Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
|||
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
<Reference Include="Microsoft.Threading.Tasks.Extensions, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
|||
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
<Reference Include="Microsoft.Threading.Tasks.Extensions.Desktop, Version=1.0.168.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
|||
<HintPath>..\packages\Microsoft.Bcl.Async.1.0.168\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
<Reference Include="nunit.framework, Version=3.0.5813.39032, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL"> |
|||
<HintPath>..\packages\NUnit.3.0.1\lib\net40\nunit.framework.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
<Reference Include="System" /> |
|||
<Reference Include="System.Core" /> |
|||
<Reference Include="System.Drawing" /> |
|||
<Reference Include="System.IO, Version=2.6.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
|||
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.IO.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
<Reference Include="System.Net" /> |
|||
<Reference Include="System.Runtime, Version=2.6.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
|||
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Runtime.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
<Reference Include="System.Runtime.Serialization" /> |
|||
<Reference Include="System.Threading.Tasks, Version=2.6.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
|||
<HintPath>..\packages\Microsoft.Bcl.1.1.10\lib\net40\System.Threading.Tasks.dll</HintPath> |
|||
<Private>True</Private> |
|||
</Reference> |
|||
<Reference Include="System.Xml.Linq" /> |
|||
<Reference Include="System.Data.DataSetExtensions" /> |
|||
<Reference Include="Microsoft.CSharp" /> |
|||
<Reference Include="System.Data" /> |
|||
<Reference Include="System.Xml" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<Compile Include="ColorMathTest.cs" /> |
|||
<Compile Include="Properties\AssemblyInfo.cs" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<None Include="app.config" /> |
|||
<None Include="packages.config" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<WCFMetadata Include="Service References\" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Google Play Music\Google Play Music.csproj"> |
|||
<Project>{b2c52d42-4eee-445c-ad6d-357d93397937}</Project> |
|||
<Name>Google Play Music</Name> |
|||
</ProjectReference> |
|||
</ItemGroup> |
|||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
|||
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" /> |
|||
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''"> |
|||
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" /> |
|||
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" /> |
|||
</Target> |
|||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. |
|||
Other similar extension points exist, see Microsoft.Common.targets. |
|||
<Target Name="BeforeBuild"> |
|||
</Target> |
|||
<Target Name="AfterBuild"> |
|||
</Target> |
|||
--> |
|||
</Project> |
@ -0,0 +1,19 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<configuration> |
|||
<runtime> |
|||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> |
|||
<dependentAssembly> |
|||
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> |
|||
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" /> |
|||
</dependentAssembly> |
|||
<dependentAssembly> |
|||
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> |
|||
<bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" /> |
|||
</dependentAssembly> |
|||
<dependentAssembly> |
|||
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> |
|||
<bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" /> |
|||
</dependentAssembly> |
|||
</assemblyBinding> |
|||
</runtime> |
|||
</configuration> |
@ -0,0 +1,9 @@ |
|||
<?xml version="1.0" encoding="utf-8"?> |
|||
<packages> |
|||
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net4" /> |
|||
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net4" /> |
|||
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net4" /> |
|||
<package id="NUnit" version="3.0.1" targetFramework="net4" /> |
|||
<package id="NUnit.Console" version="3.0.1" targetFramework="net4" /> |
|||
<package id="NUnit.Runners" version="3.0.1" targetFramework="net4" /> |
|||
</packages> |
Loading…
Reference in new issue