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