Graph lines color customization

This commit is contained in:
Paolo Ghibaudo 2024-07-02 15:59:53 +02:00
parent fa625037f0
commit b75e8d5425
4 changed files with 64 additions and 45 deletions

View file

@ -2,55 +2,37 @@
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
namespace SourceGit.Models
{
public class CommitGraph
{
static SolidColorBrush CreateBrushFromHex(string hex)
public static Pen GetPen(int penId)
{
// Rimuove il carattere '#' se presente all'inizio del codice esadecimale
if (hex.StartsWith("#"))
{
hex = hex.Substring(1);
}
// Converte il codice esadecimale in un valore ARGB
uint argb = uint.Parse(hex, System.Globalization.NumberStyles.HexNumber);
// Estrae i componenti del colore
byte a = (byte)((argb >> 24) & 0xFF);
byte r = (byte)((argb >> 16) & 0xFF);
byte g = (byte)((argb >> 8) & 0xFF);
byte b = (byte)(argb & 0xFF);
// Crea un oggetto Color
Color color = Color.FromArgb(a, r, g, b);
// Crea e restituisce un SolidColorBrush usando il colore ottenuto
return new SolidColorBrush(color);
return Pens[penId];
}
public static readonly Pen[] Pens = [
new Pen(CreateBrushFromHex("#ff15a0bf")),
new Pen(CreateBrushFromHex("#ff0669f7")),
new Pen(CreateBrushFromHex("#ff8e00c2")),
new Pen(CreateBrushFromHex("#ffc517b6")),
new Pen(CreateBrushFromHex("#ffd90171")),
new Pen(CreateBrushFromHex("#ffcd0101")),
new Pen(CreateBrushFromHex("#fff25d2e")),
new Pen(CreateBrushFromHex("#fff2ca33")),
new Pen(CreateBrushFromHex("#ff7bd938")),
new Pen(CreateBrushFromHex("#ff2ece9d")),
//new Pen(Brushes.Orange, 2),
//new Pen(Brushes.ForestGreen, 2),
//new Pen(Brushes.Gold, 2),
//new Pen(Brushes.Magenta, 2),
//new Pen(Brushes.Red, 2),
//new Pen(Brushes.Gray, 2),
//new Pen(Brushes.Turquoise, 2),
//new Pen(Brushes.Olive, 2),
public static void FillPen(int id, Pen pens)
{
if (id < Pens.Length)
{
Pens[id] = pens;
}
}
static Pen[] Pens = [
new Pen(Brushes.Orange, 2),
new Pen(Brushes.ForestGreen, 2),
new Pen(Brushes.Gold, 2),
new Pen(Brushes.Magenta, 2),
new Pen(Brushes.Red, 2),
new Pen(Brushes.Gray, 2),
new Pen(Brushes.Turquoise, 2),
new Pen(Brushes.Olive, 2),
new Pen(Brushes.Khaki, 2),
new Pen(Brushes.Lime, 2),
];
public class Path

View file

@ -32,6 +32,16 @@
<Color x:Key="Color.Diff.DeletedBG">#3CFF0000</Color>
<Color x:Key="Color.Diff.AddedHighlight">#5A00FF00</Color>
<Color x:Key="Color.Diff.DeletedHighlight">#50FF0000</Color>
<Color x:Key="Color.Graph.01">#FFFFA500</Color>
<Color x:Key="Color.Graph.02">#FF228B22</Color>
<Color x:Key="Color.Graph.03">#FFFFD700</Color>
<Color x:Key="Color.Graph.04">#FFFF00FF</Color>
<Color x:Key="Color.Graph.05">#FFFF0000</Color>
<Color x:Key="Color.Graph.06">#FF808080</Color>
<Color x:Key="Color.Graph.07">#FF40E0D0</Color>
<Color x:Key="Color.Graph.08">#FF808000</Color>
<Color x:Key="Color.Graph.09">#FFF0E68C</Color>
<Color x:Key="Color.Graph.10">#FF00FF00</Color>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
@ -65,6 +75,16 @@
<Color x:Key="Color.Diff.DeletedBG">#3CFF0000</Color>
<Color x:Key="Color.Diff.AddedHighlight">#5A00FF00</Color>
<Color x:Key="Color.Diff.DeletedHighlight">#50FF0000</Color>
<Color x:Key="Color.Graph.01">#FFFFA500</Color>
<Color x:Key="Color.Graph.02">#FF228B22</Color>
<Color x:Key="Color.Graph.03">#FFFFD700</Color>
<Color x:Key="Color.Graph.04">#FFFF00FF</Color>
<Color x:Key="Color.Graph.05">#FFFF0000</Color>
<Color x:Key="Color.Graph.06">#FF808080</Color>
<Color x:Key="Color.Graph.07">#FF40E0D0</Color>
<Color x:Key="Color.Graph.08">#FF808000</Color>
<Color x:Key="Color.Graph.09">#FFF0E68C</Color>
<Color x:Key="Color.Graph.10">#FF00FF00</Color>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

View file

@ -2,8 +2,9 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Media;
@ -709,8 +710,22 @@ namespace SourceGit.ViewModels
}
var commits = new Commands.QueryCommits(FullPath, limits).Result();
var graph = Models.CommitGraph.Parse(commits, 10);
// hack: color commits
Dispatcher.UIThread.Invoke(() => {
object res;
for (int i = 1; i <= 10; i++)
{
if (Application.Current.TryFindResource($"Color.Graph.{i:00}", out res))
{
var color = (Color)res;
var pen = new Pen(new SolidColorBrush(color));
Models.CommitGraph.FillPen(i - 1, pen);
}
}
});
var graph = Models.CommitGraph.Parse(commits, 10);
Dispatcher.UIThread.Invoke(() =>
{
if (_histories != null)

View file

@ -140,7 +140,7 @@ namespace SourceGit.Views
if (dot.Center.Y > bottom)
break;
context.DrawEllipse(dotFill, Models.CommitGraph.Pens[dot.Color], dot.Center, 3, 3);
context.DrawEllipse(dotFill, Models.CommitGraph.GetPen(dot.Color), dot.Center, 3, 3);
}
}
@ -157,8 +157,10 @@ namespace SourceGit.Views
continue;
var geo = new StreamGeometry();
var pen = Models.CommitGraph.Pens[line.Color];
var pen = Models.CommitGraph.GetPen(line.Color);
pen.Thickness = 2.5;
using (var ctx = geo.Open())
{
var started = false;
@ -228,7 +230,7 @@ namespace SourceGit.Views
ctx.QuadraticBezierTo(link.Control, link.End);
}
context.DrawGeometry(null, Models.CommitGraph.Pens[link.Color], geo);
context.DrawGeometry(null, Models.CommitGraph.GetPen(link.Color), geo);
}
}
}