diff --git a/src/SparseTransform/Convert/ColoredDotWriter.cs b/src/SparseTransform/Convert/ColoredDotWriter.cs index 48e949a1ffa75f8172983925ca500d048d5da369..f2e99e2ab6a376370089b37ec49ffe15d027b83b 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(); } } }