Skip to content

Commit

Permalink
Make colorizer optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jjonescz committed Apr 4, 2022
1 parent 06bb048 commit a97b2bb
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
var sizer = new LogSizer(wordCloud);
using var engine = new SkGraphicEngine(sizer, wordCloud);
var layout = new SpiralLayout(wordCloud);
var colorizer = new RandomColorizer();
var colorizer = new RandomColorizer(); // optional
var wcg = new WordCloudGenerator<SKBitmap>(wordCloud, engine, layout, colorizer);
```

Expand Down
2 changes: 1 addition & 1 deletion examples/WordFrequency.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static int Main(string[] args)
var sizer = new LogSizer(wordCloud);
using var engine = new SkGraphicEngine(sizer, wordCloud);
var layout = new SpiralLayout(wordCloud);
var colorizer = new RandomColorizer(); //new DefaultColorizer(); uses default color set in WordCloudInput
var colorizer = new RandomColorizer(); // optional
var wcg = new WordCloudGenerator<SKBitmap>(wordCloud, engine, layout, colorizer);
// Draw the bitmap on white background.
Expand Down
17 changes: 0 additions & 17 deletions src/KnowledgePicker.WordCloud/Coloring/DefaultColorizer.cs

This file was deleted.

4 changes: 4 additions & 0 deletions src/KnowledgePicker.WordCloud/Coloring/IColorizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ namespace KnowledgePicker.WordCloud.Coloring
{
public interface IColorizer
{
/// <summary>
/// Gets the hex string color for text.
/// </summary>
/// <returns>Hex string color in format #RRGGBB.</returns>
string GetColorAsHex();
}
}
28 changes: 14 additions & 14 deletions src/KnowledgePicker.WordCloud/Coloring/RandomColorizer.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;

namespace KnowledgePicker.WordCloud.Coloring
{
/// <summary>
/// Allows random colors for the word cloud text
/// 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);
private readonly Random random;

public RandomColorizer() : this(Environment.TickCount) { }

public RandomColorizer(int seed)
{
random = new Random(seed);
}

/// <summary>
/// Gets a random color.
/// </summary>
/// <returns>Color</returns>
[SuppressMessage("Security", "CA5394:Do not use insecure randomness")]
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
return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}

/// <summary>
/// Converts Color to Hext string
/// Converts Color to hex 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
/// Gets the randon RGB color as a hex string.
/// </summary>
/// <returns>Hexstring</returns>
public string GetColorAsHex()
{
Color c = GetRandomColor();
Expand Down
6 changes: 1 addition & 5 deletions src/KnowledgePicker.WordCloud/Drawing/IGraphicEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ 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>
/// <param name="text"></param>
/// <param name="count"></param>
/// <param name="randomColorHex"></param>
void Draw(PointD location, RectangleD measured, string text, int count, string randomColorHex);
void Draw(PointD location, RectangleD measured, string text, int count, string? colorHex = null);
}

public interface IGraphicEngine<TBitmap> : IGraphicEngine
Expand Down
5 changes: 3 additions & 2 deletions src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ 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, string? randomColorHex)
public void Draw(PointD location, RectangleD measured, string text, int count, string? colorHex = null)
{
// 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);
if (colorHex != null)
textPaint.Color = SKColor.Parse(colorHex);
canvas.DrawText(text, (float)(location.X - measured.Left),
(float)(location.Y - measured.Top), textPaint);
}
Expand Down
5 changes: 3 additions & 2 deletions src/KnowledgePicker.WordCloud/WordCloudGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class WordCloudGenerator<TBitmap>
private readonly IColorizer colorizer;

public WordCloudGenerator(WordCloudInput wordCloud,
IGraphicEngine<TBitmap> engine, ILayout layout, IColorizer colorizer)
IGraphicEngine<TBitmap> engine, ILayout layout,
IColorizer? colorizer = null)
{
this.wordCloud = wordCloud;
this.engine = engine;
Expand Down Expand Up @@ -67,7 +68,7 @@ public TBitmap Draw()
{
// Draw words.
foreach (var item in items)
engine.Draw(item.Location, item.Measured, item.Entry.Word, item.Entry.Count, colorizer.GetColorAsHex());
engine.Draw(item.Location, item.Measured, item.Entry.Word, item.Entry.Count, colorizer?.GetColorAsHex());
return engine.Bitmap;
});
}
Expand Down

0 comments on commit a97b2bb

Please sign in to comment.