-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
1040 lines (915 loc) · 43 KB
/
Extensions.cs
File metadata and controls
1040 lines (915 loc) · 43 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace TempFileCleaner
{
public static class Extensions
{
public enum LogLevel { DEBUG = 0, INFO = 1, WARNING = 2, ERROR = 3, SUCCESS = 4 }
#region [For .NET 4.8 and lower]
static readonly WeakReference s_random = new WeakReference(null);
public static Random Rnd
{
get
{
var r = (Random)s_random.Target;
if (r == null)
{
s_random.Target = r = new Random();
}
return r;
}
}
#endregion
#region [Logger with automatic duplicate checking]
static HashSet<string> _logCache = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
static DateTime _logCacheUpdated = DateTime.Now;
static int _repeatAllowedSeconds = 15;
public static void WriteToLog(this string message, LogLevel level = LogLevel.INFO, string fileName = "AppLog.txt", bool debugOnly = false)
{
if (string.IsNullOrWhiteSpace(message))
return;
if (_logCache.Add(message))
{
_logCacheUpdated = DateTime.Now;
if (debugOnly)
{
Debug.WriteLine(message);
}
else
{
try { System.IO.File.AppendAllText(fileName, $"[{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff tt")}] [{level}] {message}{Environment.NewLine}"); }
catch (Exception) { }
}
}
else
{
var diff = DateTime.Now - _logCacheUpdated;
if (diff.Seconds > _repeatAllowedSeconds)
_logCache.Clear();
else
{
if (!debugOnly)
Debug.WriteLine($"[WARNING] Duplicate not allowed: {diff.Seconds}secs < {_repeatAllowedSeconds}secs");
}
}
}
#endregion
public const double Epsilon = 0.000000000001;
public static bool IsZeroOrLess(this double value) => value < Epsilon;
public static bool IsZeroOrLess(this float value) => value < (float)Epsilon;
public static bool IsZero(this double value) => Math.Abs(value) < Epsilon;
public static bool IsZero(this float value) => Math.Abs(value) < (float)Epsilon;
public static bool IsInvalid(this double value)
{
if (value == double.NaN || value == double.NegativeInfinity || value == double.PositiveInfinity)
return true;
return false;
}
public static bool IsInvalidOrZero(this double value)
{
if (value == double.NaN || value == double.NegativeInfinity || value == double.PositiveInfinity || value <= 0)
return true;
return false;
}
public static bool IsInvalidOrZero(this System.Windows.Size value)
{
if (value.Width == double.NaN || value.Width == double.NegativeInfinity || value.Width == double.PositiveInfinity || value.Width <= 0)
return true;
if (value.Height == double.NaN || value.Height == double.NegativeInfinity || value.Height == double.PositiveInfinity || value.Height <= 0)
return true;
return false;
}
public static bool IsOne(this double value)
{
return Math.Abs(value) >= 1d - Epsilon && Math.Abs(value) <= 1d + Epsilon;
}
public static bool AreClose(this double left, double right)
{
if (left == right)
return true;
double a = (Math.Abs(left) + Math.Abs(right) + 10.0d) * Epsilon;
double b = left - right;
return (-a < b) && (a > b);
}
public static bool AreClose(this float left, float right)
{
if (left == right)
return true;
float a = (Math.Abs(left) + Math.Abs(right) + 10.0f) * (float)Epsilon;
float b = left - right;
return (-a < b) && (a > b);
}
/// <summary>
/// Clamping function for any value of type <see cref="IComparable{T}"/>.
/// </summary>
/// <param name="val">initial value</param>
/// <param name="min">lowest range</param>
/// <param name="max">highest range</param>
/// <returns>clamped value</returns>
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
{
return val.CompareTo(min) < 0 ? min : (val.CompareTo(max) > 0 ? max : val);
}
/// <summary>
/// If the <paramref name="thing"/> is null then log the stack trace from whence it came.
/// <code>
/// object thing = null;
/// thing.CheckIsNull();
/// thing.CheckIsNull("thing");
/// </code>
/// </summary>
/// <param name="thing">the object to check</param>
/// <remarks>
/// The line numbers and file names will only appear correctly if the .pdb files are
/// available alongside your .dll or .exe files when the code runs. These files contain
/// the mapping between the compiled IL (Intermediate Language) code and your original
/// source code lines. Without the corresponding PDB file, the GetFileLineNumber()
/// method will return 0, and GetFileName() might return null.
/// </remarks>
public static void CheckIsNull(this object thing, string nameOfObject = "")
{
if (thing == null)
{
StringBuilder message = new StringBuilder();
// Pass 'true' to the constructor to capture source file info
StackTrace stackTrace = new StackTrace(true);
if (!string.IsNullOrEmpty(nameOfObject))
message.Append($"[WARNING] CheckIsNull determined the incoming object \"{nameOfObject}\" was null.\n");
else
message.Append($"[WARNING] CheckIsNull determined the incoming object was null.\n");
message.Append("Call Stack:\n");
// Iterate through frames to get detailed info
foreach (StackFrame frame in stackTrace.GetFrames())
{
// Use GetFileLineNumber() and GetFileName()
message.Append($" at {frame.GetMethod().Name} in {frame.GetFileName()}:line {frame.GetFileLineNumber()}\n");
}
Debug.Write($"{message}");
}
//else
//{
// Debug.WriteLine($"[INFO] Is of type {thing.GetType().FullName}");
//}
}
/// <summary>
/// This attempts to solve the issue of having to pass the name of the object along with the object itself
/// just the obtain the name of the object during debug logging.
/// <code>
/// object myObject = null;
/// Extensions.CheckIsNull(() => myObject);
/// </code>
/// </summary>
/// <param name="expression">the object to test</param>
/// <remarks>
/// The line numbers and file names will only appear correctly if the .pdb files are
/// available alongside your .dll or .exe files when the code runs. These files contain
/// the mapping between the compiled IL (Intermediate Language) code and your original
/// source code lines. Without the corresponding PDB file, the GetFileLineNumber()
/// method will return 0, and GetFileName() might return null.
/// </remarks>
public static void CheckIsNull(System.Linq.Expressions.Expression<Func<object>> expression)
{
// Compile time safety (ensure the expression is valid)
if (expression.Body is System.Linq.Expressions.MemberExpression memberExpression)
{
string objectName = memberExpression.Member.Name;
object thing = expression.Compile().Invoke(); // Get the actual value
if (thing == null)
{
StringBuilder message = new StringBuilder();
// Pass 'true' to the constructor to capture source file info
StackTrace stackTrace = new StackTrace(true);
message.Append($"[WARNING] CheckIsNull determined the incoming object \"{objectName}\" was null.\n");
message.Append("Call Stack:\n");
// Iterate through frames to get detailed info
foreach (StackFrame frame in stackTrace.GetFrames())
{
// Use GetFileLineNumber() and GetFileName()
message.Append($" at {frame?.GetMethod()?.Name} in {frame?.GetFileName()}:line {frame?.GetFileLineNumber()}\n");
}
Debug.WriteLine($"{message}");
}
}
}
#region [Color Brush Methods]
public static (byte A, byte R, byte G, byte B) ParseHexColor(string hex)
{
if (string.IsNullOrWhiteSpace(hex))
WriteToLog("Hex color string cannot be null or empty.", LogLevel.WARNING);
// Normalize: remove leading '#'
if (hex.StartsWith("#"))
hex = hex.Substring(1);
if (hex.Length != 6 && hex.Length != 8)
WriteToLog("Hex color string must be 6 (RRGGBB) or 8 (AARRGGBB) characters.", LogLevel.WARNING);
int index = 0;
byte a = 255; // default opaque
if (hex.Length == 8)
{
a = Convert.ToByte(hex.Substring(index, 2), 16);
index += 2;
}
byte r = Convert.ToByte(hex.Substring(index, 2), 16);
byte g = Convert.ToByte(hex.Substring(index + 2, 2), 16);
byte b = Convert.ToByte(hex.Substring(index + 4, 2), 16);
return (a, r, g, b);
}
public static RadialGradientBrush CreateRadialBrush(string hex, double opacity = 0.6)
{
if (string.IsNullOrWhiteSpace(hex))
{
WriteToLog("Hex color string cannot be null or empty.", LogLevel.WARNING);
return null;
}
// Normalize input (strip leading # if present)
if (hex.StartsWith("#"))
hex = hex.Substring(1);
if (hex.Length != 6)
{
WriteToLog("Hex color string must be 6 characters (RRGGBB).", LogLevel.WARNING);
return null;
}
// Parse hex into Color
byte r = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
byte g = byte.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
byte b = byte.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
var baseColor = Color.FromRgb(r, g, b);
// Create lighter/darker variants
Color lighter = Colors.White;
//Color lighter = BrightenGamma(baseColor, 2.0); // 100% lighter
Color darker = DarkenGamma(baseColor, 0.1); // 90% darker
var brush = new RadialGradientBrush
{
Opacity = opacity,
GradientOrigin = new System.Windows.Point(0.75, 0.25),
Center = new System.Windows.Point(0.5, 0.5),
RadiusX = 0.5,
RadiusY = 0.5
};
brush.GradientStops.Add(new GradientStop(lighter, 0.0));
brush.GradientStops.Add(new GradientStop(baseColor, 0.6));
brush.GradientStops.Add(new GradientStop(darker, 1.0));
return brush;
}
/// <summary>
/// Gamma‑corrected brighten (perceptually smoother)
/// <code>
/// var brighter = BrightenGamma(baseColor, 1.5); // 50% brighter
/// </code>
/// </summary>
public static Color BrightenGamma(Color color, double factor = 1.5, double gamma = 2.2)
{
// Convert sRGB ⇨ linear
double r = Math.Pow(color.R / 255.0, gamma);
double g = Math.Pow(color.G / 255.0, gamma);
double b = Math.Pow(color.B / 255.0, gamma);
// Apply brighten factor in linear space
r = Math.Min(1.0, r * factor);
g = Math.Min(1.0, g * factor);
b = Math.Min(1.0, b * factor);
// Convert back linear ⇨ sRGB
byte R = (byte)(Math.Pow(r, 1.0 / gamma) * 255);
byte G = (byte)(Math.Pow(g, 1.0 / gamma) * 255);
byte B = (byte)(Math.Pow(b, 1.0 / gamma) * 255);
return Color.FromArgb(color.A, R, G, B);
}
/// <summary>
/// Gamma‑corrected darken (perceptually smoother)
/// <code>
/// var darker = DarkenGamma(baseColor, 0.7); // Darken to 70% brightness
/// </code>
/// </summary>
public static Color DarkenGamma(Color color, double factor = 0.7, double gamma = 2.2)
{
// factor < 1.0 will darken, factor = 1.0 no change
if (factor > 1.0) factor = 1.0;
if (factor < 0.0) factor = 0.0;
// Convert sRGB ⇨ linear
double r = Math.Pow(color.R / 255.0, gamma);
double g = Math.Pow(color.G / 255.0, gamma);
double b = Math.Pow(color.B / 255.0, gamma);
// Apply darken factor in linear space
r *= factor;
g *= factor;
b *= factor;
// Convert back linear ⇨ sRGB
byte R = (byte)(Math.Pow(r, 1.0 / gamma) * 255);
byte G = (byte)(Math.Pow(g, 1.0 / gamma) * 255);
byte B = (byte)(Math.Pow(b, 1.0 / gamma) * 255);
return Color.FromArgb(color.A, R, G, B);
}
/// <summary>
/// Generates a random <see cref="System.Windows.Media.Color"/>.
/// </summary>
/// <returns><see cref="System.Windows.Media.Color"/> with 255 alpha</returns>
public static System.Windows.Media.Color GenerateRandomColor()
{
return System.Windows.Media.Color.FromRgb((byte)new Random().Next(0, 256), (byte)new Random().Next(0, 256), (byte)new Random().Next(0, 256));
}
/// <summary>
/// Generates a random <see cref="LinearGradientBrush"/> using two <see cref="System.Windows.Media.Color"/>s.
/// </summary>
/// <returns><see cref="LinearGradientBrush"/></returns>
public static LinearGradientBrush CreateGradientBrush(Color c1, Color c2)
{
var gs1 = new GradientStop(c1, 0);
var gs3 = new GradientStop(c2, 1);
var gsc = new GradientStopCollection { gs1, gs3 };
var lgb = new LinearGradientBrush
{
ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation,
StartPoint = new System.Windows.Point(0, 0),
EndPoint = new System.Windows.Point(0, 1),
GradientStops = gsc
};
return lgb;
}
/// <summary>
/// Generates a random <see cref="LinearGradientBrush"/> using three <see cref="System.Windows.Media.Color"/>s.
/// </summary>
/// <returns><see cref="LinearGradientBrush"/></returns>
public static LinearGradientBrush CreateGradientBrush(Color c1, Color c2, Color c3)
{
var gs1 = new GradientStop(c1, 0);
var gs2 = new GradientStop(c2, 0.6);
var gs3 = new GradientStop(c3, 1);
var gsc = new GradientStopCollection { gs1, gs2, gs3 };
var lgb = new LinearGradientBrush
{
ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation,
StartPoint = new System.Windows.Point(0, 0),
EndPoint = new System.Windows.Point(0, 1),
GradientStops = gsc
};
return lgb;
}
/// <summary>
/// Generates a random <see cref="SolidColorBrush"/>.
/// </summary>
/// <returns><see cref="SolidColorBrush"/> with 255 alpha</returns>
public static SolidColorBrush CreateRandomBrush()
{
byte r = (byte)new Random().Next(0, 256);
byte g = (byte)new Random().Next(0, 256);
byte b = (byte)new Random().Next(0, 256);
return new SolidColorBrush(System.Windows.Media.Color.FromRgb(r, g, b));
}
/// <summary>
/// Avoids near-white values by using high saturation ranges prevent desaturation.
/// </summary>
public static SolidColorBrush CreateRandomLightBrush(byte alpha = 255)
{
return CreateRandomHsvBrush(
hue: new Random().NextDouble() * 360.0,
saturation: Lerp(0.65, 1.0, new Random().NextDouble()), // high saturation to avoid gray
value: Lerp(0.85, 1.0, new Random().NextDouble()), // bright
alpha: alpha);
}
/// <summary>
/// Avoids near-black values by using high saturation ranges prevent desaturation.
/// </summary>
public static SolidColorBrush CreateRandomDarkBrush(byte alpha = 255)
{
return CreateRandomHsvBrush(
hue: new Random().NextDouble() * 360.0,
saturation: Lerp(0.65, 1.0, new Random().NextDouble()), // high saturation to avoid gray
value: Lerp(0.2, 0.45, new Random().NextDouble()), // dark
alpha: alpha);
}
public static SolidColorBrush CreateRandomHsvBrush(double hue, double saturation, double value, byte alpha)
{
var (r, g, b) = HsvToRgb(hue, saturation, value);
var brush = new SolidColorBrush(Color.FromArgb(alpha, r, g, b));
if (brush.CanFreeze)
brush.Freeze(); // freeze for performance (if animation is not needed)
return brush;
}
static (byte r, byte g, byte b) HsvToRgb(double h, double s, double v)
{
// h: [0,360), s,v: [0,1]
if (s <= 0.00001)
{
// If saturation is approx zero then return achromatic (grey)
byte grey = (byte)Math.Round(v * 255.0);
return (grey, grey, grey);
}
h = (h % 360 + 360) % 360; // normalize
double c = v * s;
double x = c * (1 - Math.Abs((h / 60.0) % 2 - 1));
double m = v - c;
//double (r1, g1, b1) = h switch
//{
// < 60 => (c, x, 0),
// < 120 => (x, c, 0),
// < 180 => (0, c, x),
// < 240 => (0, x, c),
// < 300 => (x, 0, c),
// _ => (c, 0, x)
//};
double r1, g1, b1;
if (h < 60) { r1 = c; g1 = x; b1 = 0; }
else if (h < 120) { r1 = x; g1 = c; b1 = 0; }
else if (h < 180) { r1 = 0; g1 = c; b1 = x; }
else if (h < 240) { r1 = 0; g1 = x; b1 = c; }
else if (h < 300) { r1 = x; g1 = 0; b1 = c; }
else { r1 = c; g1 = 0; b1 = x; }
byte r = (byte)Math.Round((r1 + m) * 255.0);
byte g = (byte)Math.Round((g1 + m) * 255.0);
byte b = (byte)Math.Round((b1 + m) * 255.0);
return (r, g, b);
}
static void RgbToHsv(byte r, byte g, byte b, out double h, out double s, out double v)
{
double rd = r / 255.0;
double gd = g / 255.0;
double bd = b / 255.0;
double max = Math.Max(rd, Math.Max(gd, bd));
double min = Math.Min(rd, Math.Min(gd, bd));
double delta = max - min;
// Hue
if (delta < 0.00001) { h = 0; }
else if (max == rd) { h = 60 * (((gd - bd) / delta) % 6); }
else if (max == gd) { h = 60 * (((bd - rd) / delta) + 2); }
else { h = 60 * (((rd - gd) / delta) + 4); }
if (h < 0) { h += 360; }
// Saturation
s = (max <= 0) ? 0 : delta / max;
// Value
v = max;
}
static double Lerp(double a, double b, double t) => a + (b - a) * t;
public enum ColorTilt
{
Red,
Orange,
Yellow,
Green,
Blue,
Purple
}
/// <summary>
/// Generates a random <see cref="SolidColorBrush"/> based on a given <see cref="ColorTilt"/>.
/// </summary>
public static SolidColorBrush CreateRandomLightBrush(ColorTilt tilt, double tiltStrength = 30, byte alpha = 255)
{
double hue = GetTiltedHue(tilt, tiltStrength);
double saturation = Lerp(0.65, 1.0, new Random().NextDouble()); // high saturation to avoid gray
double value = Lerp(0.85, 1.0, new Random().NextDouble()); // bright
return CreateBrushFromHsv(hue, saturation, value, alpha);
}
/// <summary>
/// Generates a random <see cref="SolidColorBrush"/> based on a given <see cref="ColorTilt"/>.
/// </summary>
public static SolidColorBrush CreateRandomDarkBrush(ColorTilt tilt, double tiltStrength = 30, byte alpha = 255)
{
double hue = GetTiltedHue(tilt, tiltStrength);
double saturation = Lerp(0.65, 1.0, new Random().NextDouble()); // high saturation to avoid gray
double value = Lerp(0.2, 0.45, new Random().NextDouble()); // dark
return CreateBrushFromHsv(hue, saturation, value, alpha);
}
/// <summary>
/// Generates a random <see cref="SolidColorBrush"/> based on a given dictionary of <see cref="ColorTilt"/>s.
/// </summary>
public static SolidColorBrush CreateRandomLightBrush(Dictionary<ColorTilt, double> tiltWeights, double tiltStrength = 30, byte alpha = 255)
{
double hue = GetBlendedTiltedHue(tiltWeights, tiltStrength);
double saturation = Lerp(0.65, 1.0, new Random().NextDouble()); // high saturation to avoid gray
double value = Lerp(0.85, 1.0, new Random().NextDouble()); // bright
return CreateBrushFromHsv(hue, saturation, value, alpha);
}
/// <summary>
/// Generates a random <see cref="SolidColorBrush"/> based on a given dictionary of <see cref="ColorTilt"/>s.
/// </summary>
public static SolidColorBrush CreateRandomDarkBrush(Dictionary<ColorTilt, double> tiltWeights, double tiltStrength = 30, byte alpha = 255)
{
double hue = GetBlendedTiltedHue(tiltWeights, tiltStrength);
double saturation = Lerp(0.65, 1.0, new Random().NextDouble()); // high saturation to avoid gray
double value = Lerp(0.2, 0.45, new Random().NextDouble()); // dark
return CreateBrushFromHsv(hue, saturation, value, alpha);
}
static SolidColorBrush CreateBrushFromHsv(double hue, double saturation, double value, byte alpha)
{
var (r, g, b) = HsvToRgb(hue, saturation, value);
var brush = new SolidColorBrush(Color.FromArgb(alpha, r, g, b));
if (brush.CanFreeze) { brush.Freeze(); }
return brush;
}
static double GetTiltedHue(ColorTilt tilt, double variance = 30)
{
// Hue centers in degrees for basic colors
double centerHue;
switch (tilt)
{
case ColorTilt.Red:
centerHue = 0.0; // also wraps near 360
break;
case ColorTilt.Orange:
centerHue = 30.0;
break;
case ColorTilt.Yellow:
centerHue = 60.0;
break;
case ColorTilt.Green:
centerHue = 120.0;
break;
case ColorTilt.Blue:
centerHue = 240.0;
break;
case ColorTilt.Purple:
centerHue = 280.0; // between magenta (300) and blue
break;
default:
centerHue = 0.0;
break;
}
// Clamp variance to [0,180]
variance = Math.Max(0, Math.Min(variance, 180));
// Allow ±30° variation for variety
double minHue = centerHue - variance;
double maxHue = centerHue + variance;
double hue = minHue + new Random().NextDouble() * (maxHue - minHue);
// Wrap around 0–360
if (hue < 0) { hue += 360; }
if (hue >= 360) { hue -= 360; }
return hue;
}
static double GetBlendedTiltedHue(Dictionary<ColorTilt, double> tiltWeights, double tiltStrength)
{
if (tiltWeights == null || tiltWeights.Count == 0)
return new Random().NextDouble() * 360.0;
// Normalize weights
double total = tiltWeights.Values.Sum();
if (total <= 0) return new Random().NextDouble() * 360.0;
// Pick a tilt based on weighted random
double roll = new Random().NextDouble() * total;
double cumulative = 0;
ColorTilt chosenTilt = tiltWeights.First().Key;
foreach (var kvp in tiltWeights)
{
cumulative += kvp.Value;
if (roll <= cumulative)
{
chosenTilt = kvp.Key;
break;
}
}
// Get center hue for chosen tilt
double centerHue = GetCenterHue(chosenTilt);
// Clamp tiltStrength
tiltStrength = Math.Max(0, Math.Min(tiltStrength, 180));
// ± tiltStrength variation
double minHue = centerHue - tiltStrength;
double maxHue = centerHue + tiltStrength;
double hue = minHue + new Random().NextDouble() * (maxHue - minHue);
if (hue < 0) hue += 360;
if (hue >= 360) hue -= 360;
return hue;
}
static double GetCenterHue(ColorTilt tilt)
{
switch (tilt)
{
case ColorTilt.Red: return 0.0;
case ColorTilt.Orange: return 30.0;
case ColorTilt.Yellow: return 60.0;
case ColorTilt.Green: return 120.0;
case ColorTilt.Blue: return 240.0;
case ColorTilt.Purple: return 280.0;
default: return 0.0;
}
}
public static SolidColorBrush BrightenBrush(SolidColorBrush brush, double amount)
{
if (brush == null)
throw new ArgumentNullException(nameof(brush));
// Clamp amount to [0, 1]
amount = Math.Max(0, Math.Min(amount, 1));
Color color = brush.Color;
// Convert to HSV
double h, s, v;
RgbToHsv(color.R, color.G, color.B, out h, out s, out v);
// Increase brightness
v = Math.Min(1.0, v + amount);
// Convert back to RGB
var (r, g, b) = HsvToRgb(h, s, v);
var newBrush = new SolidColorBrush(Color.FromArgb(color.A, r, g, b));
if (newBrush.CanFreeze) { newBrush.Freeze(); }
return newBrush;
}
public static SolidColorBrush DarkenBrush(SolidColorBrush brush, double amount)
{
if (brush == null)
throw new ArgumentNullException(nameof(brush));
// Clamp amount to [0, 1]
amount = Math.Max(0, Math.Min(amount, 1));
Color color = brush.Color;
// Convert to HSV
double h, s, v;
RgbToHsv(color.R, color.G, color.B, out h, out s, out v);
// Decrease brightness
v = Math.Max(0.0, v - amount);
// Convert back to RGB
var (r, g, b) = HsvToRgb(h, s, v);
var newBrush = new SolidColorBrush(Color.FromArgb(color.A, r, g, b));
if (newBrush.CanFreeze) newBrush.Freeze();
return newBrush;
}
/// <summary><code>
/// /* Brighten by 20%, no saturation change */
/// var brighter = Extensions.AdjustBrush(baseBrush, brightnessDelta: 0.2);
/// /* Darken by 30%, mute by 20% */
/// var darkerMuted = Extensions.AdjustBrush(baseBrush, brightnessDelta: -0.3, saturationDelta: -0.2);
/// /* Keep brightness, boost saturation */
/// var vivid = Extensions.AdjustBrush(baseBrush, saturationDelta: 0.3);
/// </code></summary>
public static SolidColorBrush AdjustBrush(SolidColorBrush brush, double brightnessDelta = 0.0, double saturationDelta = 0.0)
{
if (brush == null)
throw new ArgumentNullException(nameof(brush));
Color color = brush.Color;
// Convert to HSV
double h, s, v;
RgbToHsv(color.R, color.G, color.B, out h, out s, out v);
// Apply deltas
v = Math.Max(0.0, Math.Min(1.0, v + brightnessDelta));
s = Math.Max(0.0, Math.Min(1.0, s + saturationDelta));
// Convert back to RGB
var (r, g, b) = HsvToRgb(h, s, v);
var adjusted = new SolidColorBrush(Color.FromArgb(color.A, r, g, b));
if (adjusted.CanFreeze) { adjusted.Freeze(); }
return adjusted;
}
public static SolidColorBrush ShiftSaturation(SolidColorBrush brush, double amount)
{
if (brush == null)
throw new ArgumentNullException(nameof(brush));
// amount can be positive (more vivid) or negative (more muted)
// Clamp to [-1, 1] so we don't overshoot
amount = Math.Max(-1, Math.Min(amount, 1));
Color color = brush.Color;
// Convert to HSV
double h, s, v;
RgbToHsv(color.R, color.G, color.B, out h, out s, out v);
// Adjust saturation
s = Math.Max(0.0, Math.Min(1.0, s + amount));
// Convert back to RGB
var (r, g, b) = HsvToRgb(h, s, v);
var newBrush = new SolidColorBrush(Color.FromArgb(color.A, r, g, b));
if (newBrush.CanFreeze)
newBrush.Freeze();
return newBrush;
}
/// <summary>
/// Returns the Euclidean distance between two <see cref="System.Windows.Media.Color"/>s.
/// </summary>
/// <param name="color1">1st <see cref="System.Windows.Media.Color"/></param>
/// <param name="color2">2nd <see cref="System.Windows.Media.Color"/></param>
public static double ColorDistance(System.Windows.Media.Color color1, System.Windows.Media.Color color2)
{
return Math.Sqrt(Math.Pow(color1.R - color2.R, 2) + Math.Pow(color1.G - color2.G, 2) + Math.Pow(color1.B - color2.B, 2));
}
#endregion
/// <summary>
/// Fetch all <see cref="System.Windows.Media.Brushes"/>.
/// </summary>
/// <returns><see cref="List{T}"/></returns>
public static List<Brush> GetAllMediaBrushes()
{
List<Brush> brushes = new List<Brush>();
Type brushesType = typeof(Brushes);
//TypeAttributes ta = typeof(Brushes).Attributes;
//Debug.WriteLine($"[INFO] TypeAttributes: {ta}");
// Iterate through the static properties of the Brushes class type.
foreach (PropertyInfo pi in brushesType.GetProperties(BindingFlags.Static | BindingFlags.Public))
{
// Check if the property type is Brush/SolidColorBrush
if (pi != null && (pi.PropertyType == typeof(Brush) || pi.PropertyType == typeof(SolidColorBrush)))
{
if (pi.Name.Contains("Transparent"))
continue;
Debug.WriteLine($"[INFO] Adding brush '{pi.Name}'");
// Get the brush value from the static property
var br = (Brush)pi?.GetValue(null, null);
if (br != null)
brushes.Add(br);
}
}
return brushes;
}
/// <summary>
/// 'BitmapCacheBrush','DrawingBrush','GradientBrush','ImageBrush',
/// 'LinearGradientBrush','RadialGradientBrush','SolidColorBrush',
/// 'TileBrush','VisualBrush','ImplicitInputBrush'
/// </summary>
/// <returns><see cref="List{T}"/></returns>
public static List<Type> GetAllDerivedBrushClasses()
{
List<Type> derivedBrushes = new List<Type>();
// Get the assembly containing the Brush class
Assembly assembly = typeof(Brush).Assembly;
try
{ // Iterate through all types in the assembly
foreach (Type type in assembly.GetTypes())
{
// Check if the type is a subclass of Brush
if (type.IsSubclassOf(typeof(Brush)))
{
//Debug.WriteLine($"[INFO] Adding type '{type.Name}'");
derivedBrushes.Add(type);
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"[ERROR] GetAllDerivedBrushClasses: {ex.Message}");
}
return derivedBrushes;
}
/// <summary>
/// Fetch all derived types from a super class.
/// </summary>
/// <returns><see cref="List{T}"/></returns>
public static List<Type> GetDerivedSubClasses<T>(T objectClass) where T : class
{
List<Type> derivedClasses = new List<Type>();
// Get the assembly containing the base class
Assembly assembly = typeof(T).Assembly;
try
{ // Iterate through all types in the assembly
foreach (Type type in assembly.GetTypes())
{
// Check if the type is a subclass of T
if (type.IsSubclassOf(typeof(T)))
{
//Debug.WriteLine($"[INFO] Adding subclass type '{type.Name}'");
derivedClasses.Add(type);
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"[ERROR] GetDerivedClasses: {ex.Message}");
}
return derivedClasses;
}
/// <summary>
/// Example of <see cref="UIElement"/> traversal.
/// </summary>
public static void IterateAllUIElements(DockPanel dock)
{
UIElementCollection uic = dock.Children;
foreach (Grid uie in uic)
uie.Background = new SolidColorBrush(Colors.Green);
foreach (Border uie in uic)
uie.Background = new SolidColorBrush(Colors.Orange);
foreach (StackPanel uie in uic)
uie.Background = new SolidColorBrush(Colors.Blue);
foreach (Button uie in uic)
{
uie.Background = new SolidColorBrush(Colors.Yellow);
// Example of restoring default properties
var locallySetProperties = uie.GetLocalValueEnumerator();
while (locallySetProperties.MoveNext())
{
DependencyProperty propertyToClear = locallySetProperties.Current.Property;
if (!propertyToClear.ReadOnly)
uie.ClearValue(propertyToClear);
}
}
}
/// <summary>
/// FindVisualChild element in a control group.
/// <code>
/// /* Getting the ContentPresenter of myListBoxItem */
/// var myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
///
/// /* Getting the currently selected ListBoxItem. Note that the ListBox must have IsSynchronizedWithCurrentItem set to True for this to work */
/// var myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
///
/// /* Finding textBlock from the DataTemplate that is set on that ContentPresenter */
/// var myDataTemplate = myContentPresenter.ContentTemplate;
/// var myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
///
/// /* Do something to the DataTemplate-generated TextBlock */
/// MessageBox.Show($"The text of the TextBlock of the selected list item: {myTextBlock.Text}");
/// </code>
/// </summary>
public static TChildItem FindVisualChild<TChildItem>(DependencyObject obj) where TChildItem : DependencyObject
{
for (var i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
var child = VisualTreeHelper.GetChild(obj, i);
if (child is TChildItem)
return (TChildItem)child;
var childOfChild = FindVisualChild<TChildItem>(child);
if (childOfChild != null)
return childOfChild;
}
return null;
}
/// <summary>
/// Find & return a WPF control based on its resource key name.
/// </summary>
public static T FindControl<T>(this FrameworkElement control, string resourceKey) where T : FrameworkElement
{
return (T)control.FindResource(resourceKey);
}
/// <summary>
/// <code>
/// IEnumerable<DependencyObject> cntrls = this.FindUIElements();
/// </code>
/// If you're struggling to get this working and finding that your Window (for instance)
/// has zero visual children, try running this method in the "_Loaded" event handler.
/// If you call this from a constructor (even after InitializeComponent), the visual
/// children won't be added to the VisualTree yet and it won't work properly.
/// </summary>
/// <param name="parent">some parent control like <see cref="System.Windows.Window"/></param>
/// <returns>list of <see cref="IEnumerable{DependencyObject}"/></returns>
public static IEnumerable<DependencyObject> FindUIElements(this DependencyObject parent)
{
if (parent == null)
yield break;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject o = VisualTreeHelper.GetChild(parent, i);
foreach (DependencyObject obj in FindUIElements(o))
{
if (obj == null)
continue;
if (obj is UIElement ret)
yield return ret;
}
}
yield return parent;
}
/// <summary>
/// Should be called on UI thread only.
/// </summary>
public static void HideAllVisualChildren<T>(this UIElementCollection coll) where T : UIElementCollection
{
// Casting the UIElementCollection into List
List<FrameworkElement> lstElement = coll.Cast<FrameworkElement>().ToList();
var lstControl = lstElement.OfType<Control>();
foreach (Control control in lstControl)
{
if (control == null)
continue;
control.Visibility = System.Windows.Visibility.Hidden;
}
}
public static IEnumerable<Control> GetAllControls<T>(this UIElementCollection coll) where T : UIElementCollection
{
// Casting the UIElementCollection into List
List<FrameworkElement> lstElement = coll.Cast<FrameworkElement>().ToList();
var lstControl = lstElement.OfType<Control>();
foreach (Control control in lstControl)
{
if (control == null)
continue;
yield return control;
}