Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colourisation #4

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
var sizer = new LogSizer(wordCloud);
using var engine = new SkGraphicEngine(sizer, wordCloud);
var layout = new SpiralLayout(wordCloud);
var wcg = new WordCloudGenerator<SKBitmap>(wordCloud, engine, layout);
var colorizer = new RandomColorizer();
var wcg = new WordCloudGenerator<SKBitmap>(wordCloud, engine, layout, colorizer);
```

5. Now we can *arrange* the topic cloud:
Expand Down
4 changes: 3 additions & 1 deletion examples/WordFrequency.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static int Main(string[] args)
// Process words on input.
var freqs = new Dictionary<string, int>();
var whitespaces = new Regex(@"\s+");

while (Console.ReadLine() is string line)
{
foreach (var word in whitespaces.Split(line))
Expand All @@ -55,7 +56,8 @@ static int Main(string[] args)
var sizer = new LogSizer(wordCloud);
using var engine = new SkGraphicEngine(sizer, wordCloud);
var layout = new SpiralLayout(wordCloud);
var wcg = new WordCloudGenerator<SKBitmap>(wordCloud, engine, layout);
var colorizer = new RandomColorizer(); //new DefaultColorizer(); uses default color set in WordCloudInput
var wcg = new WordCloudGenerator<SKBitmap>(wordCloud, engine, layout, colorizer);

// Draw the bitmap on white background.
using var final = new SKBitmap(wordCloud.Width, wordCloud.Height);
Expand Down
17 changes: 17 additions & 0 deletions src/KnowledgePicker.WordCloud/Drawing/DefaultColorizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace KnowledgePicker.WordCloud.Drawing
{
/// <summary>
/// Gets the default color set in the WordCloudInput
/// </summary>
public class DefaultColorizer : IColorizer
{
/// <summary>
/// Gets the hex string color for text
/// </summary>
/// <returns>Hex string color</returns>
public string GetColorAsHex()
{
return WordCloudInput.DefaultTextColor;
}
}
}
7 changes: 7 additions & 0 deletions src/KnowledgePicker.WordCloud/Drawing/IColorizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace KnowledgePicker.WordCloud.Drawing
{
public interface IColorizer
{
string GetColorAsHex();
}
}
6 changes: 5 additions & 1 deletion src/KnowledgePicker.WordCloud/Drawing/IGraphicEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ public interface IGraphicEngine : IDisposable
/// Draws <paramref name="text"/> with weight proportional to
/// <paramref name="count"/>.
/// </summary>
/// <param name="location"></param>
/// <param name="measured">
/// Result of <see cref="Measure(string, int)"/>.
/// </param>
void Draw(PointD location, RectangleD measured, string text, int count);
/// <param name="text"></param>
/// <param name="count"></param>
/// <param name="randomColorHex"></param>
void Draw(PointD location, RectangleD measured, string text, int count, string randomColorHex);
}

public interface IGraphicEngine<TBitmap> : IGraphicEngine
Expand Down
48 changes: 48 additions & 0 deletions src/KnowledgePicker.WordCloud/Drawing/RandomColorizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Drawing;

namespace KnowledgePicker.WordCloud.Drawing
{
/// <summary>
/// Allows random colors for the word cloud text
/// </summary>
public class RandomColorizer : IColorizer
{
/// <summary>
/// Used to select random colors.
/// </summary>
private Random Random { get; set; } = new Random(Environment.TickCount);

/// <summary>
/// Gets a random color.
/// </summary>
/// <returns>Color</returns>
private Color GetRandomColor()
{
#pragma warning disable CA5394 // Do not use insecure randomness
return Color.FromArgb(Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255));
#pragma warning restore CA5394 // Do not use insecure randomness
}

/// <summary>
/// Converts Color to Hext string
/// </summary>
/// <returns>Color</returns>
private static string ConvertToHexString(Color c)
{
return $"#{c.R:X2}{c.G:X2}{c.B:X2}";
}

/// <summary>
/// Gets the randon RGB color and
/// returns a hex string
/// </summary>
/// <returns>Hexstring</returns>
public string GetColorAsHex()
{
Color c = GetRandomColor();
return ConvertToHexString(c);
}

}
}
3 changes: 2 additions & 1 deletion src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ public RectangleD Measure(string text, int count)
return new RectangleD(rect.Left + m, rect.Top + m, rect.Width + 2 * m, rect.Height + 2 * m);
}

public void Draw(PointD location, RectangleD measured, string text, int count)
public void Draw(PointD location, RectangleD measured, string text, int count, string? randomColorHex)
{
// For computation explanation, see
// https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/basics/text.
textPaint.TextSize = (float)Sizer.GetFontSize(count);
textPaint.Color = SKColor.Parse(randomColorHex);
canvas.DrawText(text, (float)(location.X - measured.Left),
(float)(location.Y - measured.Top), textPaint);
}
Expand Down
8 changes: 5 additions & 3 deletions src/KnowledgePicker.WordCloud/WordCloudGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public class WordCloudGenerator<TBitmap>
private readonly WordCloudInput wordCloud;
private readonly IGraphicEngine<TBitmap> engine;
private readonly ILayout layout;
private readonly IColorizer colorizer;

public WordCloudGenerator(WordCloudInput wordCloud,
IGraphicEngine<TBitmap> engine, ILayout layout)
IGraphicEngine<TBitmap> engine, ILayout layout, IColorizer colorizer)
{
this.wordCloud = wordCloud;
this.engine = engine;
this.layout = layout;
this.colorizer = colorizer;
}

private T Process<T>(
Expand Down Expand Up @@ -59,12 +61,12 @@ private T Process<T>(
/// cref="IGraphicEngine{TBitmap}.Bitmap"/>.
/// </summary>
public TBitmap Draw()
{
{
return Process((engine, items) =>
{
// Draw words.
foreach (var item in items)
engine.Draw(item.Location, item.Measured, item.Entry.Word, item.Entry.Count);
engine.Draw(item.Location, item.Measured, item.Entry.Word, item.Entry.Count, colorizer.GetColorAsHex());
return engine.Bitmap;
});
}
Expand Down