1 // =================================
  2 // Copyright (c) 2024 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Collections;
  8 
  9 namespace System.Windows
 10 {
 11     public class Color
 12     {
 13         public Color() : alpha(255u)red(0u)green(0u)blue(0u)
 14         {
 15         }
 16         public Color(byte alpha_byte red_byte green_byte blue_) : alpha(alpha_)red(red_)green(green_)blue(blue_)
 17         {
 18         }
 19         public Color(byte red_byte green_byte blue_) : alpha(255u)red(red_)green(green_)blue(blue_)
 20         {
 21         }
 22         public Color(uint value) : alpha(cast<byte>(value >> 24u))red(cast<byte>(value >> 16u))green(cast<byte>(value >> 8u))blue(cast<byte>(value))
 23         {
 24         }
 25         public uint ToUInt() const
 26         {
 27             return (cast<uint>(alpha) << 24u) | (cast<uint>(red) << 16u) | (cast<uint>(green) << 8u) | (cast<uint>(blue));
 28         }
 29         public Color ToGray(const Color& transparentColor) const
 30         {
 31             if (*this == transparentColor) return *this;
 32             byte g = cast<byte>((cast<int>(red) + cast<int>(green) + cast<int>(blue) + cast<int>(2) * 255u) / 5);
 33             return Color(alphaggg);
 34         }
 35         public Color ToGray() const
 36         {
 37             return ToGray(Color(0u255u255u255u));
 38         }
 39         public string ToString() const
 40         {
 41             string s = System.ToString(alpha);
 42             s.Append('.').Append(System.ToString(red)).Append('.').Append(System.ToString(green)).Append('.').Append(System.ToString(blue));
 43             return s;
 44         }
 45         public static Color AliceBlue()
 46         {
 47             return Color(240u 248u 255u);
 48         }
 49         public static Color AntiqueWhite()
 50         {
 51             return Color(250u 235u 215u);
 52         }
 53         public static Color Aqua()
 54         {
 55             return Color(0u   255u 255u);
 56         }
 57         public static Color Aquamarine()
 58         {
 59             return Color(127u 255u 212u);
 60         }
 61         public static Color Azure()
 62         {
 63             return Color(240u 255u 255u);
 64         }
 65         public static Color Beige()
 66         {
 67             return Color(245u 245u 220u);
 68         }
 69         public static Color Bisque()
 70         {
 71             return Color(255u 228u 196u);
 72         }
 73         public static Color Black()
 74         {
 75             return Color(0u   0u   0u);
 76         }
 77         public static Color BlanchedAlmond()
 78         {
 79             return Color(255u 235u 205u);
 80         }
 81         public static Color Blue()
 82         {
 83             return Color(0u   0u   255u);
 84         }
 85         public static Color BlueViolet()
 86         {
 87             return Color(138u 43u  226u);
 88         }
 89         public static Color Brown()
 90         {
 91             return Color(165u 42u  42u);
 92         }
 93         public static Color BurlyWood()
 94         {
 95             return Color(222u 184u 135u);
 96         }
 97         public static Color CadetBlue()
 98         {
 99             return Color(95u  158u 160u);
100         }
101         public static Color Chartreuse()
102         {
103             return Color(127u 255u 0u);
104         }
105         public static Color Chocolate()
106         {
107             return Color(210u 105u 30u);
108         }
109         public static Color Coral()
110         {
111             return Color(255u 127u 80u);
112         }
113         public static Color CornflowerBlue()
114         {
115             return Color(100u 149u 237u);
116         }
117         public static Color Cornsilk()
118         {
119             return Color(255u 248u 220u);
120         }
121         public static Color Crimson()
122         {
123             return Color(220u 20u  60u);
124         }
125         public static Color Cyan()
126         {
127             return Color(0u   255u 255u);
128         }
129         public static Color DarkBlue()
130         {
131             return Color(0u   0u   139u);
132         }
133         public static Color DarkCyan()
134         {
135             return Color(0u   139u 139u);
136         }
137         public static Color DarkGoldenrod()
138         {
139             return Color(184u 134u 11u);
140         }
141         public static Color DarkGray()
142         {
143             return Color(169u 169u 169u);
144         }
145         public static Color DarkGreen()
146         {
147             return Color(0u   100u 0u);
148         }
149         public static Color DarkKhaki()
150         {
151             return Color(189u 183u 107u);
152         }
153         public static Color DarkMagenta()
154         {
155             return Color(139u 0u   139u);
156         }
157         public static Color DarkOliveGreen()
158         {
159             return Color(85u  107u 47u);
160         }
161         public static Color DarkOrange()
162         {
163             return Color(255u 140u 0u);
164         }
165         public static Color DarkOrchid()
166         {
167             return Color(153u 50u  204u);
168         }
169         public static Color DarkRed()
170         {
171             return Color(139u 0u   0u);
172         }
173         public static Color DarkSalmon()
174         {
175             return Color(233u 150u 122u);
176         }
177         public static Color DarkSeaGreen()
178         {
179             return Color(143u 188u 139u);
180         }
181         public static Color DarkSlateBlue()
182         {
183             return Color(72u  61u  139u);
184         }
185         public static Color DarkSlateGray()
186         {
187             return Color(47u  79u  79u);
188         }
189         public static Color DarkTurquoise()
190         {
191             return Color(0u   206u 209u);
192         }
193         public static Color DarkViolet()
194         {
195             return Color(148u 0u   211u);
196         }
197         public static Color DeepPink()
198         {
199             return Color(255u 20u  147u);
200         }
201         public static Color DeepSkyBlue()
202         {
203             return Color(0u   191u 255u);
204         }
205         public static Color DimGray()
206         {
207             return Color(105u 105u 105u);
208         }
209         public static Color DodgerBlue()
210         {
211             return Color(30u  144u 255u);
212         }
213         public static Color Firebrick()
214         {
215             return Color(178u 34u  34u);
216         }
217         public static Color FloralWhite()
218         {
219             return Color(255u 250u 240u);
220         }
221         public static Color ForestGreen()
222         {
223             return Color(34u  139u 34u);
224         }
225         public static Color Fuchsia()
226         {
227             return Color(255u 0u   255u);
228         }
229         public static Color Gainsboro()
230         {
231             return Color(220u 220u 220u);
232         }
233         public static Color GhostWhite()
234         {
235             return Color(248u 248u 255u);
236         }
237         public static Color Gold()
238         {
239             return Color(255u 215u 0u);
240         }
241         public static Color Goldenrod()
242         {
243             return Color(218u 165u 32u);
244         }
245         public static Color Gray()
246         {
247             return Color(128u 128u 128u);
248         }
249         public static Color Green()
250         {
251             return Color(0u   128u 0u);
252         }
253         public static Color GreenYellow()
254         {
255             return Color(173u 255u 47u);
256         }
257         public static Color Honeydew()
258         {
259             return Color(240u 255u 240u);
260         }
261         public static Color HotPink()
262         {
263             return Color(255u 105u 180u);
264         }
265         public static Color IndianRed()
266         {
267             return Color(205u 92u  92u);
268         }
269         public static Color Indigo()
270         {
271             return Color(75u  0u   130u);
272         }
273         public static Color Ivory()
274         {
275             return Color(255u 255u 240u);
276         }
277         public static Color Khaki()
278         {
279             return Color(240u 230u 140u);
280         }
281         public static Color Lavender()
282         {
283             return Color(230u 230u 250u);
284         }
285         public static Color LavenderBlush()
286         {
287             return Color(255u 240u 245u);
288         }
289         public static Color LawnGreen()
290         {
291             return Color(124u 252u 0u);
292         }
293         public static Color LemonChiffon()
294         {
295             return Color(255u 250u 205u);
296         }
297         public static Color LightBlue()
298         {
299             return Color(173u 216u 230u);
300         }
301         public static Color LightCoral()
302         {
303             return Color(240u 128u 128u);
304         }
305         public static Color LightCyan()
306         {
307             return Color(224u 255u 255u);
308         }
309         public static Color LightGoldenrodYellow()
310         {
311             return Color(250u 250u 210u);
312         }
313         public static Color LightGray()
314         {
315             return Color(211u 211u 211u);
316         }
317         public static Color LightGreen()
318         {
319             return Color(144u 238u 144u);
320         }
321         public static Color LightPink()
322         {
323             return Color(255u 182u 193u);
324         }
325         public static Color LightSalmon()
326         {
327             return Color(255u 160u 122u);
328         }
329         public static Color LightSeaGreen()
330         {
331             return Color(32u  178u 170u);
332         }
333         public static Color LightSkyBlue()
334         {
335             return Color(135u 206u 250u);
336         }
337         public static Color LightSlateGray()
338         {
339             return Color(119u 136u 153u);
340         }
341         public static Color LightSteelBlue()
342         {
343             return Color(176u 196u 222u);
344         }
345         public static Color LightYellow()
346         {
347             return Color(255u 255u 224u);
348         }
349         public static Color Lime()
350         {
351             return Color(0u   255u 0u);
352         }
353         public static Color LimeGreen()
354         {
355             return Color(50u  205u 50u);
356         }
357         public static Color Linen()
358         {
359             return Color(250u 240u 230u);
360         }
361         public static Color Magenta()
362         {
363             return Color(255u 0u   255u);
364         }
365         public static Color Maroon()
366         {
367             return Color(128u 0u   0u);
368         }
369         public static Color MediumAquamarine()
370         {
371             return Color(102u 205u 170u);
372         }
373         public static Color MediumBlue()
374         {
375             return Color(0u   0u   205u);
376         }
377         public static Color MediumOrchid()
378         {
379             return Color(186u 85u  211u);
380         }
381         public static Color MediumPurple()
382         {
383             return Color(147u 112u 219u);
384         }
385         public static Color MediumSeaGreen()
386         {
387             return Color(60u  179u 113u);
388         }
389         public static Color MediumSlateBlue()
390         {
391             return Color(123u 104u 238u);
392         }
393         public static Color MediumSpringGreen()
394         {
395             return Color(0u   250u 154u);
396         }
397         public static Color MediumTurquoise()
398         {
399             return Color(72u  209u 204u);
400         }
401         public static Color MediumVioletRed()
402         {
403             return Color(199u 21u  133u);
404         }
405         public static Color MidnightBlue()
406         {
407             return Color(25u  25u  112u);
408         }
409         public static Color MintCream()
410         {
411             return Color(245u 255u 250u);
412         }
413         public static Color MistyRose()
414         {
415             return Color(255u 228u 225u);
416         }
417         public static Color Moccasin()
418         {
419             return Color(255u 228u 181u);
420         }
421         public static Color NavajoWhite()
422         {
423             return Color(255u 222u 173u);
424         }
425         public static Color Navy()
426         {
427             return Color(0u   0u   128u);
428         }
429         public static Color OldLace()
430         {
431             return Color(253u 245u 230u);
432         }
433         public static Color Olive()
434         {
435             return Color(128u 128u 0u);
436         }
437         public static Color OliveDrab()
438         {
439             return Color(107u 142u 35u);
440         }
441         public static Color Orange()
442         {
443             return Color(255u 165u 0u);
444         }
445         public static Color OrangeRed()
446         {
447             return Color(255u 69u  0u);
448         }
449         public static Color Orchid()
450         {
451             return Color(218u 112u 214u);
452         }
453         public static Color PaleGoldenrod()
454         {
455             return Color(238u 232u 170u);
456         }
457         public static Color PaleGreen()
458         {
459             return Color(152u 251u 152u);
460         }
461         public static Color PaleTurquoise()
462         {
463             return Color(175u 238u 238u);
464         }
465         public static Color PaleVioletRed()
466         {
467             return Color(219u 112u 147u);
468         }
469         public static Color PapayaWhip()
470         {
471             return Color(255u 239u 213u);
472         }
473         public static Color PeachPuff()
474         {
475             return Color(255u 218u 185u);
476         }
477         public static Color Peru()
478         {
479             return Color(205u 133u 63u);
480         }
481         public static Color Pink()
482         {
483             return Color(255u 192u 203u);
484         }
485         public static Color Plum()
486         {
487             return Color(221u 160u 221u);
488         }
489         public static Color PowderBlue()
490         {
491             return Color(176u 224u 230u);
492         }
493         public static Color Purple()
494         {
495             return Color(128u 0u   128u);
496         }
497         public static Color Red()
498         {
499             return Color(255u 0u   0u);
500         }
501         public static Color RosyBrown()
502         {
503             return Color(188u 143u 143u);
504         }
505         public static Color RoyalBlue()
506         {
507             return Color(65u  105u 225u);
508         }
509         public static Color SaddleBrown()
510         {
511             return Color(139u 69u  19u);
512         }
513         public static Color Salmon()
514         {
515             return Color(250u 128u 114u);
516         }
517         public static Color SandyBrown()
518         {
519             return Color(244u 164u 96u);
520         }
521         public static Color SeaGreen()
522         {
523             return Color(46u  139u 87u);
524         }
525         public static Color SeaShell()
526         {
527             return Color(255u 245u 238u);
528         }
529         public static Color Sienna()
530         {
531             return Color(160u 82u  45u);
532         }
533         public static Color Silver()
534         {
535             return Color(192u 192u 192u);
536         }
537         public static Color SkyBlue()
538         {
539             return Color(135u 206u 235u);
540         }
541         public static Color SlateBlue()
542         {
543             return Color(106u 90u  205u);
544         }
545         public static Color SlateGray()
546         {
547             return Color(112u 128u 144u);
548         }
549         public static Color Snow()
550         {
551             return Color(255u 250u 250u);
552         }
553         public static Color SpringGreen()
554         {
555             return Color(0u   255u 127u);
556         }
557         public static Color SteelBlue()
558         {
559             return Color(70u  130u 180u);
560         }
561         public static Color Tan()
562         {
563             return Color(210u 180u 140u);
564         }
565         public static Color Teal()
566         {
567             return Color(0u   128u 128u);
568         }
569         public static Color Thistle()
570         {
571             return Color(216u 191u 216u);
572         }
573         public static Color Tomato()
574         {
575             return Color(255u 99u  71u);
576         }
577         public static Color Transparent()
578         {
579             return Color(0u   255u 255u 255u);
580         }
581         public static Color Turquoise()
582         {
583             return Color(64u  224u 208u);
584         }
585         public static Color Violet()
586         {
587             return Color(238u 130u 238u);
588         }
589         public static Color Wheat()
590         {
591             return Color(245u 222u 179u);
592         }
593         public static Color White()
594         {
595             return Color(255u 255u 255u);
596         }
597         public static Color WhiteSmoke()
598         {
599             return Color(245u 245u 245u);
600         }
601         public static Color Yellow()
602         {
603             return Color(255u 255u 0u);
604         }
605         public static Color YellowGreen()
606         {
607             return Color(154u 205u 50u);
608         }
609         public static Color DefaultBitmapTransparent()
610         {
611             return Color(0u128u128u);
612         }
613         public byte alpha;
614         public byte red;
615         public byte green;
616         public byte blue;
617     }
618 
619     public bool operator==(const Color& leftconst Color& right)
620     {
621         return left.alpha == right.alpha && left.red == right.red && left.green == right.green && left.blue == right.blue;
622     }
623 
624     public ulong GetHashCode(const Color& color)
625     {
626         return (cast<ulong>(color.alpha) << (8u * 3u)) | (cast<ulong>(color.red) << (8u * 2u)) | (cast<ulong>(color.green) << (8u * 1u)) | cast<ulong>(color.blue);
627     }
628 
629     [nodiscard]
630     public Result<Color> ParseColor(const string& s)
631     {
632         Color color;
633         List<string> components = s.Split('.');
634         if (components.Count() == 4)
635         {
636             auto alphaResult = ParseInt(components[0]);
637             if (alphaResult.Error())
638             {
639                 return Result<Color>(ErrorId(alphaResult.GetErrorId()));
640             }
641             color.alpha = cast<byte>(alphaResult.Value());
642             auto redResult = ParseInt(components[1]);
643             if (redResult.Error())
644             {
645                 return Result<Color>(ErrorId(redResult.GetErrorId()));
646             }
647             color.red = cast<byte>(redResult.Value());
648             auto greenResult = ParseInt(components[2]);
649             if (greenResult.Error())
650             {
651                 return Result<Color>(ErrorId(greenResult.GetErrorId()));
652             }
653             color.green = cast<byte>(greenResult.Value());
654             auto blueResult = ParseInt(components[3]);
655             if (blueResult.Error())
656             {
657                 return Result<Color>(ErrorId(blueResult.GetErrorId()));
658             }
659             color.blue = cast<byte>(blueResult.Value());
660         }
661         else
662         {
663             int errorId = AllocateError("could not parse color from string \'" + s + "\'");
664             return Result<Color>(ErrorId(errorId));
665         }
666         return Result<Color>(color);
667     }
668 
669     public Color GetSystemColor(SystemColor color)
670     {
671         byte red;
672         byte green;
673         byte blue;
674         WinGetSysColor(cast<int>(color)redgreenblue);
675         return Color(redgreenblue);
676     }
677 
678     public System.Windows.Color GetColor(System.Color.Constant consoleColor)
679     {
680         switch (consoleColor)
681         {
682             case System.Color.Constant.black:
683             {
684                 return System.Windows.Color(12u12u12u);
685             }
686             case System.Color.Constant.darkRed:
687             {
688                 return System.Windows.Color(197u15u31u);
689             }
690             case System.Color.Constant.darkGreen:
691             {
692                 return System.Windows.Color(19u161u14u);
693             }
694             case System.Color.Constant.darkYellow:
695             {
696                 return System.Windows.Color(193u156u0u);
697             }
698             case System.Color.Constant.darkBlue:
699             {
700                 return System.Windows.Color(0u55u218u);
701             }
702             case System.Color.Constant.darkGray:
703             {
704                 return System.Windows.Color(169u169u169u);
705             }
706             case System.Color.Constant.darkCyan:
707             {
708                 return System.Windows.Color(58u150u221u);
709             }
710             case System.Color.Constant.gray:
711             {
712                 return System.Windows.Color(204u204u204u);
713             }
714             case System.Color.Constant.red:
715             {
716                 return System.Windows.Color(231u72u86u);
717             }
718             case System.Color.Constant.green:
719             {
720                 return System.Windows.Color(22u198u12u);
721             }
722             case System.Color.Constant.yellow:
723             {
724                 return System.Windows.Color(249u241u165u);
725             }
726             case System.Color.Constant.blue:
727             {
728                 return System.Windows.Color(59u120u255u);
729             }
730             case System.Color.Constant.magenta:
731             {
732                 return System.Windows.Color(180u0u158u);
733             }
734             case System.Color.Constant.cyan:
735             {
736                 return System.Windows.Color(97u214u214u);
737             }
738             case System.Color.Constant.white:
739             {
740                 return System.Windows.Color(242u242u242u);
741             }
742         }
743         return System.Windows.Color.Black();
744     }