Skip to content

Commit

Permalink
Added option to convert XY to Hex/RGB for issue #320
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Nov 25, 2024
1 parent 95a1124 commit 6add85d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HueApi.ColorConverters\HueApi.ColorConverters.csproj" />
<ProjectReference Include="..\HueApi\HueApi.csproj" />
</ItemGroup>

</Project>
22 changes: 22 additions & 0 deletions src/HueApi.ColorConverters.Tests/XyPositionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using HueApi.ColorConverters.Original.Extensions;
using HueApi.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace HueApi.ColorConverters.Tests
{
[TestClass]
public sealed class XyPositionExtensionsTests
{
[TestMethod]
public void TestHexRgb()
{
var position = new XyPosition { X = 1, Y = 1 };
var hex = position.ToHex();
var rgb = position.ToRGBColor();

Assert.IsNotNull(hex);
Assert.IsNotNull(rgb);
}
}
}
2 changes: 1 addition & 1 deletion src/HueApi.ColorConverters/HueApi.ColorConverters.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WarningsAsErrors>nullable</WarningsAsErrors>
<Version>1.7.0</Version>
<Version>1.7.1</Version>
<Authors>Michiel Post</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>For Clip v2 API. Open source library for interaction with the Philips Hue Bridge. Allows you to control your lights from C#.</Description>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using HueApi.Models;

namespace HueApi.ColorConverters.Original.Extensions
{
public static class XyPositionExtensions
{
public static string ToHex(this XyPosition position)
{
return HueColorConverter.HexFromXy(position.X, position.Y);
}

public static RGBColor ToRGBColor(this XyPosition position)
{
return new RGBColor(position.ToHex());
}

public static RGBColor ToRGBColor(this XyPosition position, string model)
{
return HueColorConverter.ColorFromXY(new CGPoint(position.X, position.Y), model );
}
}
}
4 changes: 2 additions & 2 deletions src/HueApi.ColorConverters/Original/HueColorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static string HexFromState(Light state)
/// <param name="xNumber"></param>
/// <param name="yNumber"></param>
/// <returns></returns>
private static string HexFromXy(double xNumber, double yNumber)
internal static string HexFromXy(double xNumber, double yNumber)
{
if (xNumber == 0 && yNumber == 0)
{
Expand Down Expand Up @@ -514,7 +514,7 @@ public static RGBColor RGBColorFromState(Light state, string model)
return new RGBColor("FFFFFF"); //White
}

private static RGBColor ColorFromXY(CGPoint xy, string model)
internal static RGBColor ColorFromXY(CGPoint xy, string model)
{
List<CGPoint> colorPoints = ColorPointsForModel(model);
bool inReachOfLamps = CheckPointInLampsReach(xy, colorPoints);
Expand Down
6 changes: 6 additions & 0 deletions src/HueApi.ConsoleSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ void EventStreamMessage(string bridgeIp, List<EventStreamResponse> events)
foreach(var data in hueEvent.Data)
{
Console.WriteLine($"Bridge IP: {bridgeIp} | Data: {data.Metadata?.Name} / {data.IdV1}");

foreach(var jsonData in data.ExtensionData)
{
Console.WriteLine(jsonData);
}
Console.WriteLine();
}
}
}
Expand Down
21 changes: 20 additions & 1 deletion src/Q42.HueApi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HueApi.Entertainment.Consol
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HueDiagnostics", "HueDiagnostics\HueDiagnostics.csproj", "{E3B37251-3EAD-46F6-A6FF-FA1CC648076B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HueApi.ColorConverters.Tests", "HueApi.ColorConverters.Tests\HueApi.ColorConverters.Tests.csproj", "{A3864C6B-C904-47F2-9794-3F0724E9F7F0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -295,6 +297,22 @@ Global
{E3B37251-3EAD-46F6-A6FF-FA1CC648076B}.Release|x64.Build.0 = Release|Any CPU
{E3B37251-3EAD-46F6-A6FF-FA1CC648076B}.Release|x86.ActiveCfg = Release|Any CPU
{E3B37251-3EAD-46F6-A6FF-FA1CC648076B}.Release|x86.Build.0 = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|ARM.ActiveCfg = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|ARM.Build.0 = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|x64.ActiveCfg = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|x64.Build.0 = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|x86.ActiveCfg = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Debug|x86.Build.0 = Debug|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|Any CPU.Build.0 = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|ARM.ActiveCfg = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|ARM.Build.0 = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|x64.ActiveCfg = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|x64.Build.0 = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|x86.ActiveCfg = Release|Any CPU
{A3864C6B-C904-47F2-9794-3F0724E9F7F0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -309,9 +327,10 @@ Global
{D2F2AA7D-D327-4D2A-9F9D-8E01FEBE4AFB} = {DFB9BA31-9C63-4F2E-A1A9-22F5772A38FC}
{3B99EB35-BC48-4AA1-89BD-752E3E3AEAD7} = {DFB9BA31-9C63-4F2E-A1A9-22F5772A38FC}
{E3B37251-3EAD-46F6-A6FF-FA1CC648076B} = {DFB9BA31-9C63-4F2E-A1A9-22F5772A38FC}
{A3864C6B-C904-47F2-9794-3F0724E9F7F0} = {4D056BD7-E0A1-4B24-A0CD-DEFB571F1089}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.2\lib\NET35
SolutionGuid = {74861D31-CA14-40A4-837D-B32580C33790}
EnterpriseLibraryConfigurationToolBinariesPath = packages\Unity.2.1.505.2\lib\NET35
EndGlobalSection
EndGlobal

0 comments on commit 6add85d

Please sign in to comment.