-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLineColor.cs
328 lines (275 loc) · 9.59 KB
/
LineColor.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
namespace MOTD
{
class LineColor
{
private float _r, _g, _b;
private void set(byte r, byte g, byte b)
{
_r = (1f / 255f) * r;
_g = (1f / 255f) * g;
_b = (1f / 255f) * b;
}
public float get(char channel)
{
switch (channel)
{
case 'r': return _r;
case 'g': return _g;
case 'b': return _b;
default: return 0;
}
}
public LineColor(string color)
{
string oldColor = color; // we need it to throw in Exception message
color = color.ToLower();
color = color.Replace(" ", "");
string colorType = RecognizeColorType(color);
if(colorType == "string") { ConvertString(color); }
if(colorType == "hex") { ConvertHex(color); }
if(colorType == "rgb") { ConvertRGB(color); }
if(colorType == "none") { throw new Exception("Cant define color: " + oldColor); }
}
private string RecognizeColorType(string color)
{
int trueAmount = 0;
string result = "none";
if (ColorIsInString(color))
{
trueAmount++;
result = "string";
}
if (ColorIsInHex(color))
{
trueAmount++;
result = "hex";
}
if (ColorIsInRGB(color))
{
trueAmount++;
result = "rgb";
}
if (trueAmount == 1)
{
return result;
}
return "none";
}
private void ConvertString(string stringColor)
{
if (stringColor == "white") { set(255, 255, 255); }
if (stringColor == "silver") { set(192, 192, 192); }
if (stringColor == "gray") { set(128, 128, 128); }
if (stringColor == "grey") { set(128, 128, 128); }
if (stringColor == "black") { set(0, 0, 0); }
if (stringColor == "red") { set(255, 0, 0); }
if (stringColor == "maroon") { set(128, 0, 0); }
if (stringColor == "yellow") { set(255, 255, 0); }
if (stringColor == "olive") { set(128, 128, 0); }
if (stringColor == "lime") { set(0, 255, 0); }
if (stringColor == "green") { set(0, 128, 0); }
if (stringColor == "aqua") { set(0, 255, 255); }
if (stringColor == "teal") { set(0, 128, 128); }
if (stringColor == "blue") { set(0, 0, 255); }
if (stringColor == "navy") { set(0, 0, 128); }
if (stringColor == "fuchsia") { set(255, 0, 255); }
if (stringColor == "purple") { set(128, 0, 128); }
}
private void ConvertHex(string hexColor)
{
hexColor = hexColor.Remove(0, 1); // remove '#' on first position
if (hexColor.Length == 3) // if hex is like #ABC, convert it to #AABBCC
{
hexColor = hexColor.Insert(1, Convert.ToString(hexColor[0]));
hexColor = hexColor.Insert(3, Convert.ToString(hexColor[2]));
hexColor = hexColor.Insert(5, Convert.ToString(hexColor[4]));
}
byte r, g, b;
// We have B60024
r = ConvertHexPartToRGB(hexColor.Substring(0, 2)); // B6 goes to 182
g = ConvertHexPartToRGB(hexColor.Substring(2, 2)); // 00
b = ConvertHexPartToRGB(hexColor.Substring(4, 2)); // 24
set(r, g, b);
}
private void ConvertRGB(string rgbColor)
{
int commaPos1 = 0;
int commaPos2 = 0;
for (int i = 0; i < rgbColor.Length; i++)
{
if (rgbColor[i] == ',')
{
if (commaPos1 == 0)
{
commaPos1 = i;
}
else
{
commaPos2 = i;
}
}
}
byte r, g, b;
r = Convert.ToByte(rgbColor.Substring(0, commaPos1));
g = Convert.ToByte(rgbColor.Substring(commaPos1 + 1, commaPos2 - commaPos1 - 1));
b = Convert.ToByte(rgbColor.Substring(commaPos2 + 1));
set(r, g, b);
}
private bool ColorIsInString(string color)
{
string[] possibleValues = { "white", "silver", "gray", "grey", "black", "red", "maroon", "yellow", "olive", "lime", "green", "aqua", "teal", "blue", "navy", "fuchsia", "purple" };
if (!StringConsistOnlyOfThisArray(color, possibleValues))
{
return false;
}
return true;
}
private bool ColorIsInHex(string color)
{
if (color[0] == '#')
{
color = color.Remove(0, 1); // remove '#' on first position, if exist
}
else
{
return false;
}
if (color.Length == 3) // if hex is like #ABC, convert it to #AABBCC
{
color = color.Insert(1, Convert.ToString(color[0]));
color = color.Insert(3, Convert.ToString(color[2]));
color = color.Insert(5, Convert.ToString(color[4]));
}
if (color.Length != 6)
{
return false;
}
char[] possibleValues = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
if (!StringConsistOnlyOfThisArray(color, possibleValues))
{
return false;
}
return true; // if everything ok
}
private bool ColorIsInRGB(string color)
{
if (color.Length < 5 || color.Length > 11) // min "0,0,0" max "255,255,255"
{
return false;
}
if (CommasAmount(color) != 2)
{
return false;
}
if (color[0] == ',' || color[color.Length - 1] == ',') // if sting is like ",255,"
{
return false;
}
int commaPos1 = 0;
int commaPos2 = 0;
bool colorR = false; // will check, if string can be convertered to byte
bool colorG = false;
bool colorB = false;
for (int i = 0; i < color.Length; i++)
{
if (color[i] == ',')
{
if (commaPos1 == 0)
{
commaPos1 = i;
}
else
{
commaPos2 = i;
}
}
}
if (commaPos1 == commaPos2 - 1) // if string is like "255,,7"
{
return false;
}
colorR = ConvertStringToByte(color.Substring(0, commaPos1)); // true, if can convert
colorG = ConvertStringToByte(color.Substring(commaPos1 + 1, commaPos2 - commaPos1 - 1));
colorB = ConvertStringToByte(color.Substring(commaPos2 + 1));
if (!colorR || !colorG || !colorB) // if one of color cant be converted to byte (to have size of 0..255)
{
return false;
}
return true;
}
private bool StringConsistOnlyOfThisArray(string str, string[] stringsArray)
{
for (int i = 0; i < stringsArray.Length; i++)
{
if (str == stringsArray[i])
{
return true;
}
}
return false;
}
private bool StringConsistOnlyOfThisArray(string str, char[] charArray)
{
int temp = 0;
for (int i = 0; i < str.Length; i++)
{
for (int k = 0; k < charArray.Length; k++)
{
if (str[i] == charArray[k]) // AB4C 0123456789ABC
{
temp++;
break;
}
}
}
if (temp == str.Length)
{
return true;
}
return false;
}
private bool ConvertStringToByte(string str)
{
try
{
byte byteVal = Convert.ToByte(str);
}
catch
{
return false;
}
return true;
}
private int CommasAmount(string str)
{
int commasAmount = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == ',')
{
commasAmount++;
}
}
return commasAmount;
}
private byte ConvertHexPartToRGB(string str)
{
char[] lettersArray = { 'a', 'b', 'c', 'd', 'e', 'f' };
byte[] digitsArray = { 10, 11, 12, 13, 14, 15 };
byte hex1 = 0;
byte hex2 = 0;
for(byte i = 0; i < 10; i++)
{
if (str[0] == i) { hex1 = i; }
if (str[1] == i) { hex2 = i; }
}
for(byte i = 0; i < 6; i++)
{
if (str[0] == lettersArray[i]) { hex1 = digitsArray[i]; }
if (str[1] == lettersArray[i]) { hex2 = digitsArray[i]; }
}
return Convert.ToByte(hex1 * 16 + hex2 * 1);
}
}
}