Skip to content

Commit

Permalink
fix: Fix color models serialization
Browse files Browse the repository at this point in the history
- Bump packages version to 1.0.14
- RGBWWWColor model included
  • Loading branch information
vicfergar committed Feb 25, 2022
1 parent 5ca63af commit 79acb70
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/HassClient.Core.Tests/Converters/ColorConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static IEnumerable<TestCaseData> WriteReadJsonTestCases()

yield return createData(new RGBColor(10, 20, 30));
yield return createData(new RGBWColor(10, 20, 30, 255));
yield return createData(new RGBWWColor(10, 20, 30, 128, 255));
yield return createData(new HSColor(10, 20));
yield return createData(new XYColor(0.2f, 0.6f));
yield return createData(new NameColor("test_color"));
Expand All @@ -51,7 +52,7 @@ public void WriteJson(Color color)
var serializer = JsonSerializer.Create();
converter.WriteJson(jsonWriter, color, serializer);

Assert.AreEqual($"\"{color}\"", textWriter.ToString());
Assert.AreEqual(GetJsonRepresentation(color), textWriter.ToString());
}

[Test]
Expand All @@ -74,6 +75,7 @@ public static IEnumerable<TestCaseData> ReadJsonWithExisingValueTestCases()

yield return createData(new RGBColor(10, 20, 30), new RGBColor(40, 50, 60));
yield return createData(new RGBWColor(10, 20, 30, 255), new RGBWColor(40, 50, 60, 128));
yield return createData(new RGBWWColor(10, 20, 30, 128, 255), new RGBWWColor(40, 50, 60, 64, 128));
yield return createData(new HSColor(10, 20), new HSColor(30, 40));
yield return createData(new XYColor(0.2f, 0.6f), new XYColor(0.4f, 0.8f));
yield return createData(new NameColor("test_color"), new NameColor("new_color"));
Expand Down Expand Up @@ -111,7 +113,7 @@ private string GetJsonRepresentation(Color color)
}
else
{
return color.ToString();
return color.ToString().Replace(" ", string.Empty);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/HassClient.Core.Tests/Models/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ public void FromRGBW()
Assert.AreEqual(white, color.W);
}

[Test]
public void FromRGBWW()
{
byte red = 10;
byte green = 20;
byte blue = 30;
byte coldWhite = 128;
byte warmWhite = 255;

var color = Color.FromRGBWW(red, green, blue, coldWhite, warmWhite);

Assert.AreEqual(red, color.R);
Assert.AreEqual(green, color.G);
Assert.AreEqual(blue, color.B);
Assert.AreEqual(coldWhite, color.CW);
Assert.AreEqual(warmWhite, color.WW);
}

[Test]
public void FromHS()
{
Expand Down
2 changes: 1 addition & 1 deletion src/HassClient.Core/HassClient.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.13</Version>
<Version>1.0.14</Version>
<Authors>VFerrer</Authors>
<Description>A shared package used by HassClient. Do not install this package manually, it will be added as a prerequisite by other packages that require it.</Description>
<RepositoryUrl>https://github.com/vicfergar/HassClient</RepositoryUrl>
Expand Down
14 changes: 14 additions & 0 deletions src/HassClient.Core/Models/Color/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public static RGBWColor FromRGBW(byte red, byte green, byte blue, byte white)
return new RGBWColor(red, green, blue, white);
}

/// <summary>
/// Creates a <see cref="RGBWWColor"/> with the given values.
/// </summary>
/// <param name="red">The red color component value.</param>
/// <param name="green">The green color component value.</param>
/// <param name="blue">The blue color component value.</param>
/// <param name="coldWhite">The cold white color component value.</param>
/// <param name="warmWhite">The warm white color component value.</param>
/// <returns>A <see cref="RGBWWColor"/> with the given values.</returns>
public static RGBWWColor FromRGBWW(byte red, byte green, byte blue, byte coldWhite, byte warmWhite)
{
return new RGBWWColor(red, green, blue, coldWhite, warmWhite);
}

/// <summary>
/// Creates a <see cref="HSColor"/> with the given values.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions src/HassClient.Core/Models/Color/KelvinTemperatureColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public KelvinTemperatureColor(uint kelvins)
{
this.Kelvins = Math.Min(Math.Max(kelvins, 1000), 40000);
}

/// <inheritdoc />
public override string ToString() => this.Kelvins.ToString();
}
}
3 changes: 3 additions & 0 deletions src/HassClient.Core/Models/Color/MiredsTemperatureColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public MiredsTemperatureColor(uint mireds)
{
this.Mireds = Math.Min(Math.Max(mireds, 153), 500);
}

/// <inheritdoc />
public override string ToString() => this.Mireds.ToString();
}
}
47 changes: 47 additions & 0 deletions src/HassClient.Core/Models/Color/RGBWWColor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace HassClient.Models
{
/// <summary>
/// Represents an RGBWW (red, green, blue, cold white, warm white) color.
/// </summary>
public class RGBWWColor : RGBColor
{
/// <summary>
/// Gets the cold white color component value.
/// </summary>
public byte CW { get; internal set; }

/// <summary>
/// Gets the warm white color component value.
/// </summary>
public byte WW { get; internal set; }

/// <summary>
/// Initializes a new instance of the <see cref="RGBWWColor"/> class.
/// </summary>
/// <param name="red">The red color component value.</param>
/// <param name="green">The green color component value.</param>
/// <param name="blue">The blue color component value.</param>
/// <param name="coldWhite">The cold white color component value.</param>
/// <param name="warmWhite">The warm white color component value.</param>
public RGBWWColor(byte red, byte green, byte blue, byte coldWhite, byte warmWhite)
: base(red, green, blue)
{
this.CW = coldWhite;
this.WW = warmWhite;
}

/// <summary>
/// Initializes a new instance of the <see cref="RGBWWColor"/> class.
/// </summary>
/// <param name="color">A <see cref="System.Drawing.Color"/> color.</param>
public RGBWWColor(System.Drawing.Color color)
: this(color.R, color.G, color.B, color.A, color.A)
{
}

public static implicit operator RGBWWColor(System.Drawing.Color x) => new RGBWWColor(x);

/// <inheritdoc/>
public override string ToString() => $"[{this.R}, {this.G}, {this.B}, {this.CW}, {this.WW}]";
}
}
33 changes: 32 additions & 1 deletion src/HassClient.Core/Serialization/Converters/ColorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@ public class ColorConverter : JsonConverter<Color>
/// <inheritdoc />
public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer)
{
serializer.Serialize(writer, value.ToString());
if (value is NameColor)
{
serializer.Serialize(writer, value.ToString());
}
else if (value is KelvinTemperatureColor kelvinColor)
{
serializer.Serialize(writer, kelvinColor.Kelvins);
}
else if (value is MiredsTemperatureColor miredsColor)
{
serializer.Serialize(writer, miredsColor.Mireds);
}
else
{
serializer.Serialize(writer, JArray.Parse(value.ToString()));
}
}

/// <inheritdoc />
Expand All @@ -34,6 +49,22 @@ public override Color ReadJson(JsonReader reader, Type objectType, Color existin

return Color.FromRGBW((byte)values[0], (byte)values[1], (byte)values[2], (byte)values[3]);
}
else if (objectType == typeof(RGBWWColor))
{
var values = serializer.Deserialize<JArray>(reader);
if (hasExistingValue)
{
var rgbwwColor = existingValue as RGBWWColor;
rgbwwColor.R = (byte)values[0];
rgbwwColor.G = (byte)values[1];
rgbwwColor.B = (byte)values[2];
rgbwwColor.CW = (byte)values[3];
rgbwwColor.WW = (byte)values[4];
return rgbwwColor;
}

return Color.FromRGBWW((byte)values[0], (byte)values[1], (byte)values[2], (byte)values[3], (byte)values[4]);
}
else if (objectType == typeof(RGBColor))
{
var values = serializer.Deserialize<JArray>(reader);
Expand Down
2 changes: 1 addition & 1 deletion src/HassClient.WS/HassClient.WS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.13</Version>
<Version>1.0.14</Version>
<Authors>VFerrer</Authors>
<Description>A Home Assistant WebSocket client to communicate with Home Assistant instances.</Description>
<RepositoryUrl>https://github.com/vicfergar/HassClient</RepositoryUrl>
Expand Down

0 comments on commit 79acb70

Please sign in to comment.