diff --git a/README.md b/README.md index 4153a4f..f86aeca 100644 --- a/README.md +++ b/README.md @@ -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(wordCloud, engine, layout, colorizer); ``` diff --git a/examples/WordFrequency.ConsoleApp/Program.cs b/examples/WordFrequency.ConsoleApp/Program.cs index fcde1b7..f3da7a2 100644 --- a/examples/WordFrequency.ConsoleApp/Program.cs +++ b/examples/WordFrequency.ConsoleApp/Program.cs @@ -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(wordCloud, engine, layout, colorizer); // Draw the bitmap on white background. diff --git a/src/KnowledgePicker.WordCloud/Coloring/DefaultColorizer.cs b/src/KnowledgePicker.WordCloud/Coloring/DefaultColorizer.cs deleted file mode 100644 index 3ce1499..0000000 --- a/src/KnowledgePicker.WordCloud/Coloring/DefaultColorizer.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace KnowledgePicker.WordCloud.Coloring -{ - /// - /// Gets the default color set in the WordCloudInput - /// - public class DefaultColorizer : IColorizer - { - /// - /// Gets the hex string color for text - /// - /// Hex string color - public string GetColorAsHex() - { - return WordCloudInput.DefaultTextColor; - } - } -} diff --git a/src/KnowledgePicker.WordCloud/Coloring/IColorizer.cs b/src/KnowledgePicker.WordCloud/Coloring/IColorizer.cs index 1b74cb0..0cd4b30 100644 --- a/src/KnowledgePicker.WordCloud/Coloring/IColorizer.cs +++ b/src/KnowledgePicker.WordCloud/Coloring/IColorizer.cs @@ -2,6 +2,10 @@ namespace KnowledgePicker.WordCloud.Coloring { public interface IColorizer { + /// + /// Gets the hex string color for text. + /// + /// Hex string color in format #RRGGBB. string GetColorAsHex(); } } diff --git a/src/KnowledgePicker.WordCloud/Coloring/RandomColorizer.cs b/src/KnowledgePicker.WordCloud/Coloring/RandomColorizer.cs index c3fb7cc..4b225e0 100644 --- a/src/KnowledgePicker.WordCloud/Coloring/RandomColorizer.cs +++ b/src/KnowledgePicker.WordCloud/Coloring/RandomColorizer.cs @@ -1,43 +1,43 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Drawing; namespace KnowledgePicker.WordCloud.Coloring { /// - /// Allows random colors for the word cloud text + /// Allows random colors for the word cloud text. /// public class RandomColorizer : IColorizer { - /// - /// Used to select random colors. - /// - 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); + } /// /// Gets a random color. /// - /// Color + [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)); } /// - /// Converts Color to Hext string + /// Converts Color to hex string. /// - /// Color private static string ConvertToHexString(Color c) { return $"#{c.R:X2}{c.G:X2}{c.B:X2}"; } /// - /// Gets the randon RGB color and - /// returns a hex string + /// Gets the randon RGB color as a hex string. /// - /// Hexstring public string GetColorAsHex() { Color c = GetRandomColor(); diff --git a/src/KnowledgePicker.WordCloud/Drawing/IGraphicEngine.cs b/src/KnowledgePicker.WordCloud/Drawing/IGraphicEngine.cs index 41b7e14..8791d6c 100644 --- a/src/KnowledgePicker.WordCloud/Drawing/IGraphicEngine.cs +++ b/src/KnowledgePicker.WordCloud/Drawing/IGraphicEngine.cs @@ -26,14 +26,10 @@ public interface IGraphicEngine : IDisposable /// Draws with weight proportional to /// . /// - /// /// /// Result of . /// - /// - /// - /// - 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 : IGraphicEngine diff --git a/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs b/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs index e839cb6..22fd465 100644 --- a/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs +++ b/src/KnowledgePicker.WordCloud/Drawing/SkGraphicEngine.cs @@ -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); } diff --git a/src/KnowledgePicker.WordCloud/WordCloudGenerator.cs b/src/KnowledgePicker.WordCloud/WordCloudGenerator.cs index 415c05c..0bc256e 100644 --- a/src/KnowledgePicker.WordCloud/WordCloudGenerator.cs +++ b/src/KnowledgePicker.WordCloud/WordCloudGenerator.cs @@ -27,7 +27,8 @@ public class WordCloudGenerator private readonly IColorizer colorizer; public WordCloudGenerator(WordCloudInput wordCloud, - IGraphicEngine engine, ILayout layout, IColorizer colorizer) + IGraphicEngine engine, ILayout layout, + IColorizer? colorizer = null) { this.wordCloud = wordCloud; this.engine = engine; @@ -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; }); }