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

Merge branch 'coloring' into 'master'

Rework the algorithm that chooses colors for DOT output

See merge request !28
parents ce99af67 def9eddd
No related branches found
No related tags found
1 merge request!28Rework the algorithm that chooses colors for DOT output
......@@ -71,7 +71,7 @@ public class Program
/// <returns>Dot String with coloring information</returns>
private static String Partition2ColoredDot(String MMMatrix, bool textColor = false)
{
return ConversionManager.Partition2ColoredDot(MMMatrix);
return ConversionManager.Partition2ColoredDot(MMMatrix, textColor);
}
/// <summary>
......
......@@ -26,7 +26,7 @@ namespace SparseTransform
/// <param name="MMMatrix">raw MatrixMarket input String</param>
/// <param name="textColor">true if color should additionally be represented as text</param>
/// <returns>Dot String with coloring information</returns>
public static String Partition2ColoredDot(String MMMatrix, bool textColor= false)
public static String Partition2ColoredDot(String MMMatrix, bool textColor = false)
{
MatrixMarketReader reader = new MatrixMarketReader();
IGraph graph = reader.ReadGraph(MMMatrix);
......@@ -40,7 +40,7 @@ namespace SparseTransform
}
else
color.PartialD2Color((BipartiteGraph)graph);
ColoredDotWriter writer = new ColoredDotWriter(textColor);
ColoredDotWriter writer = new ColoredDotWriter(graph, textColor);
return writer.Write(graph);
}
......
using SparseTransform.DataStructures;
using System.Text;
namespace SparseTransform.Convert
{
......@@ -7,27 +8,17 @@ namespace SparseTransform.Convert
/// </summary>
public class ColoredDotWriter : DotWriter
{
private bool textcolor;
private string[] dictColor = new string[65];
private IGraph _coloredGraph;
private bool _textcolor;
/// <summary>
/// Constructor constructing a color-dictionary for coloring purpose.
/// </summary>
/// <param name="textColor">decision whether the labels should be colored or labelled.</param>
public ColoredDotWriter(bool textColor)
public ColoredDotWriter(IGraph graph, bool textColor)
{
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"));
}
}
}
this._coloredGraph = graph;
this._textcolor = textColor;
}
/// <summary>
......@@ -38,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();
}
}
}
......@@ -37,7 +37,7 @@ namespace SparseTransform.DataStructures
/// <summary>
/// Number of colors used for coloring.
/// </summary>
private int ColorsUsed
public int ColorsUsed
{
get
{
......
......@@ -69,7 +69,7 @@ namespace SparseTransform.DataStructures
/// <summary>
/// Number of colors used for coloring. Works if either or both sides are colored
/// </summary>
private int ColorsUsed
public int ColorsUsed
{
get
{
......
......@@ -6,6 +6,8 @@ namespace SparseTransform.DataStructures
/// </summary>
public interface IGraph
{
public int ColorsUsed { get; }
/// <summary>
/// Adds an edge between nodes i and j. If either of the nodes is missing it is created.
/// </summary>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment