11using Gdk ;
22using System ;
3+ using System . Collections . Generic ;
34using System . Runtime . InteropServices ;
45
56public static class Common
67{
8+ static Random random = new Random ( Guid . NewGuid ( ) . GetHashCode ( ) ) ;
9+
10+ public static uint [ ] ColourValues = {
11+ 0xFF0000 , 0x00FF00 , 0x0000FF , 0xFFFF00 , 0xFF00FF , 0x00FFFF , 0x000000 ,
12+ 0x800000 , 0x008000 , 0x000080 , 0x808000 , 0x800080 , 0x008080 , 0x808080 ,
13+ 0xC00000 , 0x00C000 , 0x0000C0 , 0xC0C000 , 0xC000C0 , 0x00C0C0 , 0xC0C0C0 ,
14+ 0x400000 , 0x004000 , 0x000040 , 0x404000 , 0x400040 , 0x004040 , 0x404040 ,
15+ 0x200000 , 0x002000 , 0x000020 , 0x202000 , 0x200020 , 0x002020 , 0x202020 ,
16+ 0x600000 , 0x006000 , 0x000060 , 0x606000 , 0x600060 , 0x006060 , 0x606060 ,
17+ 0xA00000 , 0x00A000 , 0x0000A0 , 0xA0A000 , 0xA000A0 , 0x00A0A0 , 0xA0A0A0 ,
18+ 0xE00000 , 0x00E000 , 0x0000E0 , 0xE0E000 , 0xE000E0 , 0x00E0E0 , 0xE0E0E0 ,
19+ } ;
20+
21+ public static uint [ ] CE2000 = {
22+ 0x00FF00 , 0x0000FF , 0xFF0000 , 0x01FFFE , 0xFFA6FE , 0xFFDB66 , 0x006401 ,
23+ 0x010067 , 0x95003A , 0x007DB5 , 0xFF00F6 , 0xFFEEE8 , 0x774D00 , 0x90FB92 ,
24+ 0x0076FF , 0xD5FF00 , 0xFF937E , 0x6A826C , 0xFF029D , 0xFE8900 , 0x7A4782 ,
25+ 0x7E2DD2 , 0x85A900 , 0xFF0056 , 0xA42400 , 0x00AE7E , 0x683D3B , 0xBDC6FF ,
26+ 0x263400 , 0xBDD393 , 0x00B917 , 0x9E008E , 0x001544 , 0xC28C9F , 0xFF74A3 ,
27+ 0x01D0FF , 0x004754 , 0xE56FFE , 0x788231 , 0x0E4CA1 , 0x91D0CB , 0xBE9970 ,
28+ 0x968AE8 , 0xBB8800 , 0x43002C , 0xDEFF74 , 0x00FFC6 , 0xFFE502 , 0x620E00 ,
29+ 0x008F9C , 0x98FF52 , 0x7544B1 , 0xB500FF , 0x00FF78 , 0xFF6E41 , 0x005F39 ,
30+ 0x6B6882 , 0x5FAD4E , 0xA75740 , 0xA5FFD2 , 0xFFB167 , 0x009BFF , 0xE85EBE
31+ } ;
32+
33+ public static Color [ ] RandomColors ( int count )
34+ {
35+ Color [ ] colors = new Color [ count ] ;
36+
37+ HashSet < Color > hs = new HashSet < Color > ( ) ;
38+
39+ for ( int i = 0 ; i < count ; i ++ )
40+ {
41+ Color color ;
42+
43+ while ( ! hs . Add ( color = new Color ( ( byte ) random . Next ( 70 , 200 ) , ( byte ) random . Next ( 100 , 225 ) , ( byte ) random . Next ( 100 , 230 ) ) ) ) { }
44+
45+ colors [ i ] = color ;
46+ }
47+
48+ return colors ;
49+ }
50+
51+ public static Color [ ] Palette ( )
52+ {
53+ var palette = new Color [ ColourValues . Length ] ;
54+
55+ for ( var i = 0 ; i < ColourValues . Length ; i ++ )
56+ palette [ i ] = I2C ( ColourValues [ i ] ) ;
57+
58+ return palette ;
59+ }
60+
61+ public static Color [ ] Palette2 ( )
62+ {
63+ var palette = new Color [ CE2000 . Length ] ;
64+
65+ for ( var i = 0 ; i < CE2000 . Length ; i ++ )
66+ palette [ i ] = I2C ( CE2000 [ i ] ) ;
67+
68+ return palette ;
69+ }
70+
771 public static uint C2I ( Color color )
872 {
973 return ( uint ) ( ( color . Red << 16 ) | ( color . Green << 8 ) | color . Blue ) ;
@@ -28,6 +92,23 @@ public static Pixbuf Pixbuf(int width, int height, Color c)
2892 return pixbuf ;
2993 }
3094
95+ // Fisher–Yates shuffle algorithm
96+ public static void Shuffle < T > ( this T [ ] list )
97+ {
98+ int n = list . Length ;
99+
100+ for ( int i = list . Length - 1 ; i > 1 ; i -- )
101+ {
102+ int rnd = random . Next ( i + 1 ) ;
103+
104+ T value = list [ rnd ] ;
105+
106+ list [ rnd ] = list [ i ] ;
107+
108+ list [ i ] = value ;
109+ }
110+ }
111+
31112 public static Pixbuf Pixbuf ( int width , int height )
32113 {
33114 var pixbuf = Pixbuf ( width , height , new Color ( 0 , 0 , 0 ) ) ;
@@ -52,22 +133,36 @@ public static void Point(Pixbuf pixbuf, int xp, int yp, Color c)
52133 }
53134 }
54135
55- public static void Circle ( Pixbuf pixbuf , int xc , int yc , int x , int y , Color color )
136+ public static void Circle ( Pixbuf pixbuf , int xc , int yc , int x , int y , Color color , bool filled = false )
56137 {
57- for ( var i = xc - x ; i <= xc + x ; i ++ )
58- Common . Point ( pixbuf , i , yc + y , color ) ;
138+ if ( filled )
139+ {
140+ for ( var i = xc - x ; i <= xc + x ; i ++ )
141+ Point ( pixbuf , i , yc + y , color ) ;
59142
60- for ( var i = xc - x ; i <= xc + x ; i ++ )
61- Common . Point ( pixbuf , i , yc - y , color ) ;
143+ for ( var i = xc - x ; i <= xc + x ; i ++ )
144+ Point ( pixbuf , i , yc - y , color ) ;
62145
63- for ( var i = xc - y ; i <= xc + y ; i ++ )
64- Common . Point ( pixbuf , i , yc + x , color ) ;
146+ for ( var i = xc - y ; i <= xc + y ; i ++ )
147+ Point ( pixbuf , i , yc + x , color ) ;
65148
66- for ( var i = xc - y ; i <= xc + y ; i ++ )
67- Common . Point ( pixbuf , i , yc - x , color ) ;
149+ for ( var i = xc - y ; i <= xc + y ; i ++ )
150+ Point ( pixbuf , i , yc - x , color ) ;
151+ }
152+ else
153+ {
154+ Point ( pixbuf , xc - x , yc + y , color ) ;
155+ Point ( pixbuf , xc + x , yc + y , color ) ;
156+ Point ( pixbuf , xc - x , yc - y , color ) ;
157+ Point ( pixbuf , xc + x , yc - y , color ) ;
158+ Point ( pixbuf , xc - y , yc + x , color ) ;
159+ Point ( pixbuf , xc + y , yc + x , color ) ;
160+ Point ( pixbuf , xc - y , yc - x , color ) ;
161+ Point ( pixbuf , xc + y , yc - x , color ) ;
162+ }
68163 }
69164
70- public static void Circle ( Pixbuf pixbuf , int xc , int yc , int r , Color c )
165+ public static void Circle ( Pixbuf pixbuf , int xc , int yc , int r , Color c , bool filled = false )
71166 {
72167 int x = 0 , y = r ;
73168 int d = 3 - 2 * r ;
@@ -76,7 +171,7 @@ public static void Circle(Pixbuf pixbuf, int xc, int yc, int r, Color c)
76171 {
77172 // for each pixel we will
78173 // draw all eight pixels
79- Circle ( pixbuf , xc , yc , x , y , c ) ;
174+ Circle ( pixbuf , xc , yc , x , y , c , filled ) ;
80175
81176 x ++ ;
82177
@@ -86,24 +181,24 @@ public static void Circle(Pixbuf pixbuf, int xc, int yc, int r, Color c)
86181 if ( d > 0 )
87182 {
88183 y -- ;
184+
89185 d = d + 4 * ( x - y ) + 10 ;
90186 }
91187 else
92188 d = d + 4 * x + 6 ;
93189
94- Circle ( pixbuf , xc , yc , x , y , c ) ;
190+ Circle ( pixbuf , xc , yc , x , y , c , filled ) ;
95191 }
96192 }
97193
98194 public static void Line ( Pixbuf pixbuf , int x0 , int y0 , int x1 , int y1 , Color color )
99195 {
100196 int dx = Math . Abs ( x1 - x0 ) , sx = x0 < x1 ? 1 : - 1 ;
101-
102197 int dy = Math . Abs ( y1 - y0 ) , sy = y0 < y1 ? 1 : - 1 ;
103198
104199 int err = ( dx > dy ? dx : - dy ) / 2 , e2 ;
105200
106- for ( ; ; )
201+ while ( true )
107202 {
108203 Point ( pixbuf , x0 , y0 , color ) ;
109204
0 commit comments