-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.cs
181 lines (160 loc) · 5.7 KB
/
Helpers.cs
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SupervisorDashboard
{
class Helpers
{
public static class RandomColor
{
private static readonly Random random = new Random();
public static Color GetRandomColor()
{
return Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
}
}
public class ColorGenerator
{
static string[] ColourValues = new string[] {
"FF0000", "00FF00", "0000FF", "9B870C", "FF00FF", "00FFFF", "000000",
"800000", "008000", "000080", "808000", "800080", "008080", "808080",
"C00000", "00C000", "0000C0", "C0C000", "C000C0", "00C0C0", "C0C0C0",
"400000", "004000", "000040", "404000", "400040", "004040", "404040",
"200000", "002000", "000020", "202000", "200020", "002020", "202020",
"600000", "006000", "000060", "606000", "600060", "006060", "606060",
"A00000", "00A000", "0000A0", "A0A000", "A000A0", "00A0A0", "A0A0A0",
"E00000", "00E000", "0000E0", "E0E000", "E000E0", "00E0E0", "E0E0E0",
};
private int index = 0;
private IntensityGenerator intensityGenerator = new IntensityGenerator();
public Color NextColor(bool predefined=true)
{
string colour;
if (predefined)
colour = ColourValues[index];
else
colour = string.Format(PatternGenerator.NextPattern(index),
intensityGenerator.NextIntensity(index));
index++;
return Color.FromArgb(Convert.ToInt32("FF" + colour, 16));
}
}
public class PatternGenerator
{
public static string NextPattern(int index)
{
switch (index % 7)
{
case 0: return "{0}0000";
case 1: return "00{0}00";
case 2: return "0000{0}";
case 3: return "{0}{0}00";
case 4: return "{0}00{0}";
case 5: return "00{0}{0}";
case 6: return "{0}{0}{0}";
default: throw new Exception("Math error");
}
}
}
public class IntensityGenerator
{
private IntensityValueWalker walker;
private int current;
public string NextIntensity(int index)
{
if (index == 0)
{
current = 255;
}
else if (index % 7 == 0)
{
if (walker == null)
{
walker = new IntensityValueWalker();
}
else
{
walker.MoveNext();
}
current = walker.Current.Value;
}
string currentText = current.ToString("X");
if (currentText.Length == 1) currentText = "0" + currentText;
return currentText;
}
}
public class IntensityValue
{
private IntensityValue mChildA;
private IntensityValue mChildB;
public IntensityValue(IntensityValue parent, int value, int level)
{
if (level > 7) throw new Exception("There are no more colours left");
Value = value;
Parent = parent;
Level = level;
}
public int Level { get; set; }
public int Value { get; set; }
public IntensityValue Parent { get; set; }
public IntensityValue ChildA
{
get
{
return mChildA ?? (mChildA = new IntensityValue(this, this.Value - (1 << (7 - Level)), Level + 1));
}
}
public IntensityValue ChildB
{
get
{
return mChildB ?? (mChildB = new IntensityValue(this, Value + (1 << (7 - Level)), Level + 1));
}
}
}
public class IntensityValueWalker
{
public IntensityValueWalker()
{
Current = new IntensityValue(null, 1 << 7, 1);
}
public IntensityValue Current { get; set; }
public void MoveNext()
{
if (Current.Parent == null)
{
Current = Current.ChildA;
}
else if (Current.Parent.ChildA == Current)
{
Current = Current.Parent.ChildB;
}
else
{
int levelsUp = 1;
Current = Current.Parent;
while (Current.Parent != null && Current == Current.Parent.ChildB)
{
Current = Current.Parent;
levelsUp++;
}
if (Current.Parent != null)
{
Current = Current.Parent.ChildB;
}
else
{
levelsUp++;
}
for (int i = 0; i < levelsUp; i++)
{
Current = Current.ChildA;
}
}
}
}
}
}