Skip to content

Commit

Permalink
Add utility methods for getting color (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz authored Apr 26, 2023
2 parents e0f1ce9 + cc6a766 commit 0dbb45b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
var x = (item.Location.X - item.Measured.Left).ToString(format);
var y = (item.Location.Y - item.Measured.Top).ToString(format);
var fs = fontSize.ToString(format);
<text transform="translate(@x, @y)" font-size="@fs">@item.Entry.Word</text>
var color = wcg.GetColorHexString(item);
<text transform="translate(@x, @y)" font-size="@fs" fill="@color">@item.Entry.Word</text>
}
</svg>
```
Expand Down
10 changes: 10 additions & 0 deletions src/KnowledgePicker.WordCloud/WordCloudGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,15 @@ public TBitmap Draw()
return engine.ExtractBitmap();
});
}

public string? GetColorHexString(LayoutItem item)
{
return colorizer?.GetColor(item)?.ToHexString();
}

public string GetColorHexStringOrDefault(LayoutItem item)
{
return GetColorHexString(item) ?? wordCloud.TextColor;
}
}
}
51 changes: 51 additions & 0 deletions test/KnowledgePicker.WordCloud.Tests/WordCloudGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using KnowledgePicker.WordCloud.Coloring;
using KnowledgePicker.WordCloud.Drawing;
using KnowledgePicker.WordCloud.Layouts;
using KnowledgePicker.WordCloud.Primitives;
using KnowledgePicker.WordCloud.Sizers;
using KnowledgePicker.WordCloud.Utilities;
using SkiaSharp;
using System.Drawing;

namespace KnowledgePicker.WordCloud.Tests;

Expand Down Expand Up @@ -36,4 +39,52 @@ public void DoesNotShareState()
Assert.Equal(result1.AsEnumerable(), result2);
Assert.Equal(2, result2.Length);
}

[Fact] // https://github.com/knowledgepicker/word-cloud/issues/23
public void CanGetColor()
{
// Arrange.
var wordCloud = new WordCloudInput(new[]
{
new WordCloudEntry("a", 1),
new WordCloudEntry("b", 1),
new WordCloudEntry("c", 1),
})
{
Width = 1024,
Height = 256,
MinFontSize = 8,
MaxFontSize = 32
};
var sizer = new LogSizer(wordCloud);
using var engine = new SkGraphicEngine(sizer, wordCloud);
var layout = new SpiralLayout(wordCloud);
var color1 = Color.FromArgb(0x0f3057);
var color2 = Color.FromArgb(0xe25a5a);
var colorizer = new SpecificColorizer(
new Dictionary<string, Color>
{
["a"] = color1,
["b"] = color2
});
var wcg = new WordCloudGenerator<SKBitmap>(wordCloud, engine, layout, colorizer);

// Act.
foreach (var (item, fontSize) in wcg.Arrange())
{
var actualColor = wcg.GetColorHexString(item);
var actualColorOrDefault = wcg.GetColorHexStringOrDefault(item);

// Assert.
var expectedColor = item.Entry.Word switch
{
"a" => color1.ToHexString(),
"b" => color2.ToHexString(),
_ => null
};
var expectedColorOrDefault = expectedColor ?? WordCloudInput.DefaultTextColor;
Assert.Equal(expectedColor, actualColor);
Assert.Equal(expectedColorOrDefault, actualColorOrDefault);
}
}
}

0 comments on commit 0dbb45b

Please sign in to comment.