From db44ed9a7feac40380ce53c1b4c97d1ed8aaced8 Mon Sep 17 00:00:00 2001 From: Paul Nitzke <14367-paulenit@users.noreply.git.rwth-aachen.de> Date: Wed, 22 Jun 2022 17:58:31 +0200 Subject: [PATCH] Replace color finding algorithm for writer * Replace the algorithm that finds the most distinct colors for n used colors. This can currently color up to 255 nodes using hue spread. After that the program automatically adopts text coloring --- .../Convert/ColoredDotWriter.cs | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/SparseTransform/Convert/ColoredDotWriter.cs b/src/SparseTransform/Convert/ColoredDotWriter.cs index 48e949a..f2e99e2 100644 --- a/src/SparseTransform/Convert/ColoredDotWriter.cs +++ b/src/SparseTransform/Convert/ColoredDotWriter.cs @@ -1,4 +1,5 @@ using SparseTransform.DataStructures; +using System.Text; namespace SparseTransform.Convert { @@ -9,7 +10,6 @@ namespace SparseTransform.Convert { private IGraph _coloredGraph; private bool _textcolor; - private string[] _dictColor = new string[65]; /// <summary> /// Constructor constructing a color-dictionary for coloring purpose. @@ -19,17 +19,6 @@ namespace SparseTransform.Convert { this._coloredGraph = graph; this._textcolor = textColor; - - for (int i = 0; i <= 3; i++) - { - for (int j = 0; j <= 3; j++) - { - for (int k = 0; k <= 4; k++) - { - _dictColor[i * 16 + j * 4 + k] = ("#" + (255 - i * 60).ToString("X2") + (255 - j * 60).ToString("X2") + (255 - k * 60).ToString("X2")); - } - } - } } /// <summary> @@ -40,14 +29,32 @@ namespace SparseTransform.Convert /// <returns></returns> public override String NodeLabel(GraphNode node, String prefix) { - if (_textcolor || node.Color > 64) - { - return "\t" + prefix + node.Index + " [shape=circle, style=filled, label=\"" + prefix + node.Index + "\\n Farbe " + node.Color + "\"]"; - } - else + // This is the offset. Less colors = more distance between + int hueFactor = 255 / _coloredGraph.ColorsUsed; + // Calculate the hue. We modulo with 255 to not exceed spectrum. Then convert to float for DOT + double hue = Math.Round((double)((node.Color * hueFactor) % 255) / 255, 3); + + StringBuilder label = new StringBuilder(); + // Append label name and general layout info + label.Append($"\t{prefix}{node.Index} [shape=circle, style=filled"); + + // If current node is colored, apply either text label with color or set fill color + if (node.Colored) { - return "\t" + prefix + node.Index + " [shape=circle, style=filled, fillcolor=\"" + _dictColor[node.Color] + "\"]"; + if (_textcolor || _coloredGraph.ColorsUsed > 255) + { + label.Append($", label=\"{prefix}{node.Index}\\n color:{node.Color}\""); + } + else + { + // Set saturation + value (brightness) to max for vibrant colors + label.Append($", fillcolor=\"{hue} {1} {1}\""); + } } + + // Close attribute line + label.Append("]"); + return label.ToString(); } } } -- GitLab