Skip to content

Commit

Permalink
Replaced System.Drawing.Color by custom DotColor
Browse files Browse the repository at this point in the history
  • Loading branch information
vfrz committed Aug 10, 2023
1 parent c10222e commit 3ca09f7
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using DotNetGraph.Attributes;
using DotNetGraph.Compilation;
using DotNetGraph.Core;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand All @@ -27,7 +27,7 @@ public async Task CompileFromString()
[TestMethod]
public async Task CompileFromColor()
{
var attribute = new DotColorAttribute(Color.Red);
var attribute = new DotColorAttribute(DotColor.Red);

await using var writer = new StringWriter();
var context = new CompilationContext(writer, new CompilationOptions());
Expand All @@ -40,7 +40,7 @@ public async Task CompileFromColor()
[TestMethod]
public void ImplicitConversionFromColor()
{
DotColorAttribute attribute = Color.Red;
DotColorAttribute attribute = DotColor.Red;
attribute.Value.Should().Be("#FF0000");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using DotNetGraph.Attributes;
Expand Down
42 changes: 42 additions & 0 deletions Sources/DotNetGraph.Tests/Core/DotColorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using DotNetGraph.Core;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DotNetGraph.Tests.Core;

[TestClass]
public class DotColorTests
{
[DataTestMethod]
[DataRow(255, 0, 0, 255, 0, 0, 255, 255, false)]
[DataRow(128, 15, 255, 255, 128, 15, 255, 255, true)]
public void EqualsWithAnother(int r1, int g1, int b1, int a1, int r2, int g2, int b2, int a2, bool expectedEqual)
{
var color1 = new DotColor((byte) r1, (byte) g1, (byte) b1, (byte) a1);
var color2 = new DotColor((byte) r2, (byte) g2, (byte) b2, (byte) a2);

var equal = color1.Equals(color2);

equal.Should().Be(expectedEqual);
}

[TestMethod]
public void ToHexStringRgb()
{
var color = new DotColor(123, 123, 255);

var hex = color.ToHexString();

hex.Should().Be("#7B7BFF");
}

[TestMethod]
public void ToHexStringRgba()
{
var color = new DotColor(123, 123, 255, 123);

var hex = color.ToHexString();

hex.Should().Be("#7B7BFF7B");
}
}
3 changes: 1 addition & 2 deletions Sources/DotNetGraph.Tests/Core/DotNodeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using DotNetGraph.Compilation;
Expand Down Expand Up @@ -31,7 +30,7 @@ public async Task CompileNodeWithColor()
{
var node = new DotNode()
.WithIdentifier("Test")
.WithColor(Color.Red);
.WithColor(DotColor.Red);

await using var writer = new StringWriter();
var context = new CompilationContext(writer, new CompilationOptions());
Expand Down
30 changes: 0 additions & 30 deletions Sources/DotNetGraph.Tests/Extensions/ColorExtensionsTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using DotNetGraph.Core;
using DotNetGraph.Extensions;
using FluentAssertions;
Expand Down Expand Up @@ -114,7 +113,7 @@ public void WithColorString()
public void WithColor()
{
var edge = new DotEdge()
.WithColor(Color.Red);
.WithColor(DotColor.Red);

edge.Color.Value.Should().Be("#FF0000");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using DotNetGraph.Attributes;
using DotNetGraph.Core;
using DotNetGraph.Extensions;
Expand Down Expand Up @@ -53,7 +52,7 @@ public void WithFontColorString()
public void WithFontColor()
{
var node = new DotNode()
.WithFontColor(Color.Red);
.WithFontColor(DotColor.Red);

node.FontColor.Value.Should().Be("#FF0000");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using DotNetGraph.Core;
using DotNetGraph.Extensions;
using FluentAssertions;
Expand Down Expand Up @@ -42,7 +41,7 @@ public void WithColorString()
public void WithColor()
{
var node = new DotNode()
.WithColor(Color.Red);
.WithColor(DotColor.Red);

node.Color.Value.Should().Be("#FF0000");
}
Expand All @@ -60,7 +59,7 @@ public void WithFillColorString()
public void WithFillColor()
{
var node = new DotNode()
.WithFillColor(Color.Red);
.WithFillColor(DotColor.Red);

node.FillColor.Value.Should().Be("#FF0000");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Drawing;
using DotNetGraph.Core;
using DotNetGraph.Extensions;
using FluentAssertions;
Expand All @@ -22,7 +21,7 @@ public void WithColorString()
public void WithColor()
{
var subgraph = new DotSubgraph()
.WithColor(Color.Red);
.WithColor(DotColor.Red);

subgraph.Color.Value.Should().Be("#FF0000");
}
Expand Down
7 changes: 3 additions & 4 deletions Sources/DotNetGraph/Attributes/DotColorAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Drawing;
using System.Threading.Tasks;
using DotNetGraph.Compilation;
using DotNetGraph.Extensions;
using DotNetGraph.Core;

namespace DotNetGraph.Attributes
{
public class DotColorAttribute : IDotAttribute
{
public string Value { get; set; }

public DotColorAttribute(Color color)
public DotColorAttribute(DotColor color)
{
Value = color.ToHexString();
}
Expand All @@ -24,7 +23,7 @@ public async Task CompileAsync(CompilationContext context)
await context.WriteAsync($"\"{Value}\"");
}

public static implicit operator DotColorAttribute(Color value) => new DotColorAttribute(value);
public static implicit operator DotColorAttribute(DotColor value) => new DotColorAttribute(value);
public static implicit operator DotColorAttribute(string value) => new DotColorAttribute(value);
}
}
Loading

0 comments on commit 3ca09f7

Please sign in to comment.