-
Notifications
You must be signed in to change notification settings - Fork 2
/
YColor.cs
346 lines (263 loc) · 9.39 KB
/
YColor.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Globalization;
namespace YColors
{
// an improvement over regular .Net Color Classe:
// this one remember if color was defined from RGB
// of HSL color space and can implicitly convert
// colors literal names.
public struct YColor
{
private byte _A, _R, _G, _B, _H, _S, _L;
private bool _fromHSL;
public byte A { get { return _A; } }
public byte R { get { return _R; } }
public byte G { get { return _G; } }
public byte B { get { return _B; } }
public byte H { get { return _H; } }
public byte S { get { return _S; } }
public byte L { get { return _L; } }
public bool isHSL { get { return _fromHSL; } }
public bool isRGB { get { return !_fromHSL; } }
public void setModelToHSL() { _fromHSL = true; }
public void setModelToRGB() { _fromHSL = false; }
// private static IEnumerable<System.Reflection.PropertyInfo> knownColorProperties = typeof(Color)
// .GetProperties(BindingFlags.Public | BindingFlags.Static)
// .Where(p => p.PropertyType == typeof(Color));
private static List<System.Reflection.PropertyInfo> knownColorProperties = (typeof(Color)
.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Where(p => p.PropertyType == typeof(Color))).ToList();
public static YColor FromArgb(byte A, byte R, byte G, byte B)
{
YColor it = new YColor();
it._fromHSL = false;
it._A = A; it._R = R; it._G = G; it._B = B;
float _fR = (R / 255f);
float _fG = (G / 255f);
float _fB = (B / 255f);
float _Min = Math.Min(Math.Min(_fR, _fG), _fB);
float _Max = Math.Max(Math.Max(_fR, _fG), _fB);
float _Delta = _Max - _Min;
float fH = 0;
float fS = 0;
float fL = (float)((_Max + _Min) / 2.0f);
if (_Delta != 0)
{
if (fL < 0.5f)
{
fS = (float)(_Delta / (_Max + _Min));
}
else
{
fS = (float)(_Delta / (2.0f - _Max - _Min));
}
if (_fR == _Max)
{
fH = (_fG - _fB) / _Delta;
}
else if (_fG == _Max)
{
fH = 2f + (_fB - _fR) / _Delta;
}
else if (_fB == _Max)
{
fH = 4f + (_fR - _fG) / _Delta;
}
}
it._H = (byte)(255 * fH / 6);
it._S = (byte)(255 * fS);
it._L = (byte)(255 * fL);
return it;
}
private static double ColorCalc(double c, double t1, double t2)
{
if (c < 0) c += 1d;
if (c > 1) c -= 1d;
if (6.0d * c < 1.0d) return t1 + (t2 - t1) * 6.0d * c;
if (2.0d * c < 1.0d) return t2;
if (3.0d * c < 2.0d) return t1 + (t2 - t1) * (2.0d / 3.0d - c) * 6.0d;
return t1;
}
public static YColor FromAhsl(byte A, byte H, byte S, byte L)
{
YColor it = new YColor();
it._fromHSL = true;
it._A = A;
it._H = H;
it._S = S;
it._L = L;
if (S == 0)
{
it._R = L;
it._G = L;
it._B = L;
}
else
{
double Hue = (6 * (double)H) / 255;
double Saturation = ((double)S / 255);
double Luminosity = ((double)L / 255);
double t1, t2;
double th = Hue / 6.0d;
if (Luminosity < 0.5d)
{
t2 = Luminosity * (1d + Saturation);
}
else
{
t2 = (Luminosity + Saturation) - (Luminosity * Saturation);
}
t1 = 2d * Luminosity - t2;
double tr, tg, tb;
tr = th + (1.0d / 3.0d);
tg = th;
tb = th - (1.0d / 3.0d);
tr = ColorCalc(tr, t1, t2);
tg = ColorCalc(tg, t1, t2);
tb = ColorCalc(tb, t1, t2);
it._R = (byte)Math.Round(tr * 255d);
it._G = (byte)Math.Round(tg * 255d);
it._B = (byte)Math.Round(tb * 255d);
}
return it;
}
public bool isAPredefinedColor()
{
if (_fromHSL) return false;
if (_A != 255) return false;
foreach (var colorProperty in knownColorProperties)
{
var colorPropertyValue = (Color)colorProperty.GetValue(null, null);
if (colorPropertyValue.R == _R
&& colorPropertyValue.G == _G
&& colorPropertyValue.B == _B)
{
return true;
}
}
return false;
}
public override String ToString()
{
if (_fromHSL) return "HSL:" + _A.ToString("X2") + _H.ToString("X2") + _S.ToString("X2") + _L.ToString("X2");
if ((_A == 0) && (_R == 0) && (_G == 0) && (_B == 0)) return "Transparent";
if (_A == 255)
{
if ((_R==255) && (_G == 255) && (_B == 255)) return "White"; // there is a prefined Color matching white but named "transparent";
foreach (var colorProperty in knownColorProperties)
{
var colorPropertyValue = (Color)colorProperty.GetValue(null, null);
if (colorPropertyValue.R == _R
&& colorPropertyValue.G == _G
&& colorPropertyValue.B == _B)
{
return colorPropertyValue.Name;
}
}
}
return "RGB:" + _A.ToString("X2") + _R.ToString("X2") + _G.ToString("X2") + _B.ToString("X2");
}
public Color toColor()
{
return Color.FromArgb(_A, _R, _G, _B);
}
public static YColor fromColor(Color c)
{
return FromArgb(c.A, c.R, c.G, c.B);
}
public static YColor fromString(string s, out bool matchFound)
{
matchFound = false;
s = s.Replace(" ", "");
if (s.StartsWith("#")) s = "RGB:" + s.Substring(1);
if (s.ToUpper() == "LIGHTGREY") s = "RGB:FFD3D3D3"; // sometimes it's LightGrey, sometimes it's LightGray,
if (s.ToUpper() == "TRANSPARENT") s = "RGB:00000000"; //
foreach (var colorProperty in knownColorProperties)
{
var colorPropertyValue = (Color)colorProperty.GetValue(null, null);
if (String.Equals(colorProperty.Name, s, StringComparison.OrdinalIgnoreCase))
{
matchFound = true;
return YColor.FromArgb(255, colorPropertyValue.R, colorPropertyValue.G, colorPropertyValue.B);
}
}
bool fromHSL = false;
if ((s.Length > 4))
{
if (s.Substring(0, 4).ToUpper() == "HSL:") { fromHSL = true; s = s.Substring(4); }
else if (s.Substring(0, 4).ToUpper() == "RGB:") { s = s.Substring(4); }
}
long value;
if (( s.Length>=6) && (long.TryParse(s, System.Globalization.NumberStyles.HexNumber, null, out value)))
{
byte a;
if (s.Length < 7) a = 255; else a = (byte)(value >> 24);
if (fromHSL) { matchFound = true; return YColor.FromAhsl(a, (byte)((value >> 16) & 0xFF), (byte)((value >> 8) & 0xFF), (byte)((value) & 0xFF)); }
else { matchFound = true; return FromArgb(a, (byte)((value >> 16) & 0xFF), (byte)((value >> 8) & 0xFF), (byte)((value) & 0xFF)); }
}
return YColor.FromArgb(255, 0, 0, 0);
}
public static YColor fromString(string s)
{
bool ok;
return fromString( s, out ok);
}
public static bool operator !=(YColor c1, YColor c2)
{
if ((c1._A == c2._A) && (c1._R == c2._R) && (c1._G == c2._G) && (c1._B == c2._B)) return false;
if ((c1._A == c2._A) && (c1._H == c2._H) && (c1._S == c2._S) && (c1._L == c2._L)) return false;
return true;
}
public static bool operator ==(YColor c1, YColor c2)
{
if ((c1._A == c2._A) && (c1._R == c2._R) && (c1._G == c2._G) && (c1._B == c2._B)) return true;
if ((c1._A == c2._A) && (c1._H == c2._H) && (c1._S == c2._S) && (c1._L == c2._L)) return true;
return false;
}
public override bool Equals(Object o)
{
if ( o is YColor)
{
return this == (YColor)o;
}
return false;
}
public override int GetHashCode()
{
return ((int)_A << 24) | ((int)_R << 16) | ((int)_G << 8) | (int)_B;
}
}
public class YColorConverter : StringConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
{
if (destinationType == typeof(YColor)) return true;
return base.CanConvertTo(context, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(String)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is String) return YColor.fromString((string)value);
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
object value, System.Type destinationType)
{
if (destinationType == typeof(YColor) && value is String)
{
YColor.fromString((string)value);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}