Skip to content
Snippets Groups Projects
Commit db44ed9a authored by Paul Nitzke's avatar Paul Nitzke
Browse files

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
parent c5b5344e
Branches
No related tags found
1 merge request!28Rework the algorithm that chooses colors for DOT output
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();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment