Repository navigation
Expand file tree
/
Copy pathChartColors.cs
More file actions
135 lines (116 loc) · 5.68 KB
/
Copy pathChartColors.cs
File metadata and controls
135 lines (116 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System.Windows;
using System.Windows.Media;
using LiveChartsCore.Painting;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using LiveChartsCore.SkiaSharpView.Painting.Effects;
using NETworkManager.Settings;
using SkiaSharp;
namespace NETworkManager;
/// <summary>
/// Colors of the charts and maps (Ping Monitor, bandwidth of the Network Interface, speed test, WiFi channels,
/// cities of the traceroute map) for the current theme: the same hues in both themes, a bit brighter on the dark and
/// darker on the light background for contrast. Read when a chart is created and set again on
/// <see cref="AppearanceManager.ThemeResourcesChanged"/> (the paints of an open chart don't follow the theme).
/// </summary>
public static class ChartColors
{
// Dark theme: brighter than the former fixed colors (#1ba1e2, #7fba00); light theme: darker for the white
// background
private static readonly SKColor PrimaryDark = SKColor.Parse("#3aaee8");
private static readonly SKColor PrimaryLight = SKColor.Parse("#0a76b5");
private static readonly SKColor SecondaryDark = SKColor.Parse("#8cc63f");
private static readonly SKColor SecondaryLight = SKColor.Parse("#4a8a00");
// One color per network (WiFi channels), distinct hues
private static readonly SKColor[] PaletteDark =
[
SKColor.Parse("#3aaee8"), SKColor.Parse("#b5d335"), SKColor.Parse("#f5ad2a"),
SKColor.Parse("#ff5c4d"), SKColor.Parse("#8f5cff"), SKColor.Parse("#1ec2bf"),
SKColor.Parse("#ff4fa3"), SKColor.Parse("#7ccf3a"), SKColor.Parse("#ff8a3d"),
SKColor.Parse("#5a8cff"), SKColor.Parse("#c25cff"), SKColor.Parse("#c08a52")
];
private static readonly SKColor[] PaletteLight =
[
SKColor.Parse("#0a76b5"), SKColor.Parse("#6f8a00"), SKColor.Parse("#b8740a"),
SKColor.Parse("#c41200"), SKColor.Parse("#5a00d6"), SKColor.Parse("#008a88"),
SKColor.Parse("#b8005f"), SKColor.Parse("#3f8a10"), SKColor.Parse("#c95300"),
SKColor.Parse("#0042c4"), SKColor.Parse("#8a00cc"), SKColor.Parse("#6b4824")
];
/// <summary>
/// <see langword="true"/> if the current theme has a dark background.
/// </summary>
private static bool IsDark =>
Application.Current?.TryFindResource("MahApps.Colors.ThemeBackground") is not Color background ||
ThemeVariants.IsDark(background);
/// <summary>
/// Main series (ping time, download, cities of the traceroute map).
/// </summary>
public static SKColor Primary => IsDark ? PrimaryDark : PrimaryLight;
/// <summary>
/// Second series (upload).
/// </summary>
public static SKColor Secondary => IsDark ? SecondaryDark : SecondaryLight;
/// <summary>
/// Color of the series with the index (e.g. a WiFi network), repeats after 12 colors.
/// </summary>
public static SKColor GetPaletteColor(int index)
{
var palette = IsDark ? PaletteDark : PaletteLight;
return palette[index % palette.Length];
}
/// <summary>
/// Color of a brush of the application (e.g. <c>MahApps.Brushes.Gray5</c> for labels, <c>Brushes.Surface.Border</c>
/// for separators) for a chart, incl. the opacity of the brush (the surface brushes keep the color opaque and get
/// their low opacity from <see cref="Brush.Opacity" />).
/// </summary>
/// <param name="resourceKey">Resource key of a <see cref="SolidColorBrush" />.</param>
/// <param name="fallback">Color if the resource is not found.</param>
public static SKColor FromResource(string resourceKey, SKColor fallback) =>
Application.Current?.TryFindResource(resourceKey) is SolidColorBrush brush
? new SKColor(brush.Color.R, brush.Color.G, brush.Color.B,
(byte)System.Math.Round(brush.Color.A * brush.Opacity))
: fallback;
/// <summary>
/// Labels and names of the axes, text of a legend (<c>MahApps.Brushes.Gray5</c>).
/// </summary>
public static SKColor LabelColor => FromResource("MahApps.Brushes.Gray5", new SKColor(0x68, 0x68, 0x68));
/// <summary>
/// Labels and names of the axes.
/// </summary>
public static SolidColorPaint LabelPaint() => new(LabelColor);
/// <summary>
/// Dashed grid lines (<c>Brushes.Surface.Border</c>, incl. the opacity of the brush).
/// </summary>
public static SolidColorPaint SeparatorPaint() =>
new(FromResource("Brushes.Surface.Border", new SKColor(0x80, 0x80, 0x80, 0x40)))
{
StrokeThickness = 1,
PathEffect = new DashEffect([10f, 10f])
};
/// <summary>
/// Line of a series.
/// </summary>
public static SolidColorPaint LineStroke(SKColor color) => new(color) { StrokeThickness = 1.5f };
/// <summary>
/// Sets the paints of an axis for the current theme: labels, name (if shown) and dashed grid lines (if shown).
/// </summary>
/// <param name="axis">Axis of a chart.</param>
public static void ApplyAxisColors(Axis axis)
{
axis.LabelsPaint = LabelPaint();
if (axis.NamePaint != null)
axis.NamePaint = LabelPaint();
if (axis.SeparatorsPaint != null)
axis.SeparatorsPaint = SeparatorPaint();
}
/// <summary>
/// <see cref="Primary"/> as WPF color (e.g. the traceroute map).
/// </summary>
public static Color PrimaryWpf => Color.FromRgb(Primary.Red, Primary.Green, Primary.Blue);
/// <summary>
/// Fill below a line: from 33 % of the line color at the top to transparent at the bottom.
/// </summary>
public static Paint AreaFill(SKColor color) =>
new LinearGradientPaint([color.WithAlpha(0x55), color.WithAlpha(0x00)], new SKPoint(0.5f, 0),
new SKPoint(0.5f, 1));
}