Skip to content

Commit 7a21733

Browse files
committed
enhance: graph color allocation
1 parent 0391ec9 commit 7a21733

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/Models/CommitGraph.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
7171
var unsolved = new List<PathHelper>();
7272
var ended = new List<PathHelper>();
7373
var offsetY = -halfHeight;
74-
var colorIdx = 0;
74+
var colorPicker = new ColorPicker();
7575

7676
foreach (var commit in commits)
7777
{
@@ -121,7 +121,10 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
121121

122122
// Remove ended curves from unsolved
123123
foreach (var l in ended)
124+
{
125+
colorPicker.Recycle(l.Path.Color);
124126
unsolved.Remove(l);
127+
}
125128
ended.Clear();
126129

127130
// If no path found, create new curve for branch head
@@ -132,12 +135,10 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
132135

133136
if (commit.Parents.Count > 0)
134137
{
135-
major = new PathHelper(commit.Parents[0], isMerged, colorIdx, new Point(offsetX, offsetY));
138+
major = new PathHelper(commit.Parents[0], isMerged, colorPicker.Next(), new Point(offsetX, offsetY));
136139
unsolved.Add(major);
137140
temp.Paths.Add(major.Path);
138141
}
139-
140-
colorIdx = (colorIdx + 1) % s_penCount;
141142
}
142143
else if (isMerged && !major.IsMerged && commit.Parents.Count > 0)
143144
{
@@ -187,10 +188,9 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
187188
offsetX += unitWidth;
188189

189190
// Create new curve for parent commit that not includes before
190-
var l = new PathHelper(parentHash, isMerged, colorIdx, position, new Point(offsetX, position.Y + halfHeight));
191+
var l = new PathHelper(parentHash, isMerged, colorPicker.Next(), position, new Point(offsetX, position.Y + halfHeight));
191192
unsolved.Add(l);
192193
temp.Paths.Add(l.Path);
193-
colorIdx = (colorIdx + 1) % s_penCount;
194194
}
195195
}
196196
}
@@ -217,6 +217,28 @@ public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnable
217217
return temp;
218218
}
219219

220+
private class ColorPicker
221+
{
222+
public int Next()
223+
{
224+
if (_colorsQueue.Count == 0)
225+
{
226+
for (var i = 0; i < s_penCount; i++)
227+
_colorsQueue.Enqueue(i);
228+
}
229+
230+
return _colorsQueue.Dequeue();
231+
}
232+
233+
public void Recycle(int idx)
234+
{
235+
if (!_colorsQueue.Contains(idx))
236+
_colorsQueue.Enqueue(idx);
237+
}
238+
239+
private Queue<int> _colorsQueue = new Queue<int>();
240+
}
241+
220242
private class PathHelper
221243
{
222244
public Path Path { get; private set; }

0 commit comments

Comments
 (0)