1
- using System ;
1
+ using Newtonsoft . Json . Linq ;
2
+ using System ;
2
3
using System . Collections . Generic ;
3
4
using System . Drawing ;
4
5
using System . IO ;
5
6
using System . Linq ;
6
- using System . Text ;
7
- using System . Threading . Tasks ;
8
7
using System . Xml . Linq ;
9
- using System . Xml . Xsl ;
10
- using Newtonsoft . Json . Linq ;
11
8
12
9
namespace mdresgen
13
10
{
14
11
class Program
15
12
{
13
+ private const string BaseSnippetLocation = "MaterialColourSwatchesSnippet.xml" ;
14
+
15
+ // Legacy
16
+ private const string OldXamlFileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.xaml" ;
17
+ private const string OldXamlNamedFileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.Named.xaml" ;
18
+
19
+
20
+ private const string XamlPrimaryFileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.Primary.xaml" ;
21
+ private const string XamlAccentFileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.Accent.xaml" ;
22
+ private const string XamlPrimaryNamedFileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.Named.Primary.xaml" ;
23
+ private const string XamlAccentNamedFileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.Named.Accent.xaml" ;
24
+
25
+ private const string RecommendedPrimaryFileFormat = @"..\..\..\Themes\Recommended\Primary\MaterialDesignColor.{0}.xaml" ;
26
+ private const string RecommendedAccentFileFormat = @"..\..\..\Themes\Recommended\Accent\MaterialDesignColor.{0}.xaml" ;
27
+ private const string RecommendedPrimaryTemplateLocation = "RecommendedPrimaryTemplate.xaml" ;
28
+ private const string RecommendedAccentTemplateLocation = "RecommendedAccentTemplate.xaml" ;
29
+
16
30
private static readonly IDictionary < string , Color > ClassNameToForegroundIndex = new Dictionary < string , Color > ( )
17
31
{
18
32
{ "color" , Color . FromArgb ( ( int ) ( 255 * 0.87 ) , 255 , 255 , 255 ) } ,
@@ -25,130 +39,197 @@ class Program
25
39
26
40
static void Main ( string [ ] args )
27
41
{
28
- var xDocument = XDocument . Load ( "MaterialColourSwatchesSnippet.xml" ) ;
29
-
30
- if ( args . Length > 0 && args . Contains ( "json" ) )
31
- GenerateJson ( xDocument ) ;
32
- if ( args . Length > 0 && args . Contains ( "named" ) )
33
- GenerateXamlNamed ( xDocument ) ;
42
+ var xDocument = XDocument . Load ( BaseSnippetLocation ) ;
43
+
44
+ if ( args . Length == 0 )
45
+ GenerateXaml ( xDocument , false ) ;
46
+ else if ( args . Contains ( "json" ) )
47
+ GenerateJson ( xDocument ) ;
48
+ else if ( args . Contains ( "named" ) )
49
+ GenerateXaml ( xDocument , true ) ;
50
+ else if ( args . Contains ( "old-named" ) )
51
+ GenerateOldXaml ( xDocument , true ) ;
52
+ else if ( args . Contains ( "old" ) )
53
+ GenerateOldXaml ( xDocument , false ) ;
34
54
else
35
- GenerateXaml ( xDocument ) ;
55
+ GenerateXaml ( xDocument , false ) ;
56
+
57
+ Console . WriteLine ( ) ;
58
+ Console . WriteLine ( ) ;
59
+ Console . WriteLine ( "FINISHED" ) ;
60
+ Console . ReadKey ( ) ;
36
61
}
37
62
38
- private static void GenerateXaml ( XDocument xDocument )
39
- {
40
- const string fileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.xaml" ;
63
+ private static void GenerateXaml ( XDocument xDocument , bool named = false )
64
+ {
65
+ Console . WriteLine ( "Generating {0} XAMLs & recommended colors" , named ? "named" : "regular" ) ;
66
+ Console . WriteLine ( ) ;
67
+
68
+ var recommendedPrimary = File . ReadAllText ( RecommendedPrimaryTemplateLocation ) ;
69
+ var recommendedAccent = File . ReadAllText ( RecommendedAccentTemplateLocation ) ;
41
70
42
- foreach ( var colour in xDocument . Root . Elements ( "section" ) . Select ( el => ToResourceDictionary ( el ) ) )
43
- {
44
- colour . Item2 . Save ( string . Format ( fileFormat , colour . Item1 . Replace ( " " , "" ) ) ) ;
45
- }
46
- }
71
+ foreach ( var color in xDocument . Root . Elements ( "section" ) )
72
+ {
73
+ bool primaryEmpty ;
74
+ bool accentEmpty ;
75
+
76
+ var primary = ToResourceDictionary ( color , out primaryEmpty , named , ColorMode . PrimaryOnly ) ;
77
+ var accent = ToResourceDictionary ( color , out accentEmpty , named , ColorMode . AccentOnly ) ;
78
+
79
+ var longcolor = primary . Item1 ;
80
+ var shortcolor = longcolor . Replace ( " " , "" ) ;
81
+
82
+ Console . WriteLine ( "{0} \t Primary: {1} \t Accent: {2}" , longcolor . PadRight ( 15 , ' ' ) , ! primaryEmpty , ! accentEmpty ) ;
83
+
84
+ if ( ! primaryEmpty )
85
+ {
86
+ primary . Item2 . Save (
87
+ string . Format (
88
+ named ? XamlPrimaryNamedFileFormat : XamlPrimaryFileFormat ,
89
+ shortcolor
90
+ ) ) ;
91
+
92
+ File . WriteAllText (
93
+ string . Format ( RecommendedPrimaryFileFormat , shortcolor ) ,
94
+ recommendedPrimary . Replace ( "$COLOR" , shortcolor ) . Replace ( "$LONG_COLOR" , longcolor )
95
+ ) ;
96
+ }
97
+
98
+ if ( ! accentEmpty )
99
+ {
100
+ accent . Item2 . Save (
101
+ string . Format (
102
+ named ? XamlAccentNamedFileFormat : XamlAccentFileFormat ,
103
+ shortcolor
104
+ ) ) ;
105
+
106
+ File . WriteAllText (
107
+ string . Format ( RecommendedAccentFileFormat , shortcolor ) ,
108
+ recommendedAccent . Replace ( "$COLOR" , shortcolor ) . Replace ( "$LONG_COLOR" , longcolor )
109
+ ) ;
110
+ }
111
+ }
112
+ }
47
113
48
- private static void GenerateXamlNamed ( XDocument xDocument )
114
+ private static void GenerateOldXaml ( XDocument xDocument , bool named = false )
49
115
{
50
- const string fileFormat = @"..\..\..\Themes\MaterialDesignColor. {0}.Named.xaml" ;
116
+ Console . WriteLine ( "Generating old {0} XAMLs" , named ? "named" : "regular" ) ;
51
117
52
- Console . WriteLine ( "Running named gen" ) ;
118
+ bool dummy ;
53
119
54
- foreach ( var colour in xDocument . Root . Elements ( "section" ) . Select ( el => ToResourceDictionary ( el , true ) ) )
120
+ foreach ( var color in xDocument . Root . Elements ( "section" ) . Select ( el => ToResourceDictionary ( el , out dummy ) ) )
55
121
{
56
- colour . Item2 . Save ( string . Format ( fileFormat , colour . Item1 . Replace ( " " , "" ) ) ) ;
122
+ color . Item2 . Save (
123
+ string . Format (
124
+ named ? OldXamlNamedFileFormat : OldXamlFileFormat ,
125
+ color . Item1 . Replace ( " " , "" )
126
+ ) ) ;
57
127
}
58
128
}
59
129
60
130
private static void GenerateJson ( XDocument xDocument )
61
- {
62
- const string file = @"..\..\..\web\scripts\Swatches.js" ;
63
-
64
-
65
- var json = new JArray (
66
- xDocument . Root . Elements ( "section" )
67
- . Select ( sectionElement => new JObject (
68
- new JProperty ( "name" , GetColourName ( sectionElement ) ) ,
69
- new JProperty ( "colors" ,
70
- new JArray (
71
- sectionElement . Element ( "ul" ) . Elements ( "li" ) . Skip ( 1 ) . Select ( CreateJsonColourPair )
72
- )
73
- )
74
- ) )
75
- ) . ToString ( ) ;
76
-
77
- //var javaScript = $"var swatches={json};";
131
+ {
132
+ const string file = @"..\..\..\web\scripts\Swatches.js" ;
133
+
134
+
135
+ var json = new JArray (
136
+ xDocument . Root . Elements ( "section" )
137
+ . Select ( sectionElement => new JObject (
138
+ new JProperty ( "name" , GetColourName ( sectionElement ) ) ,
139
+ new JProperty ( "colors" ,
140
+ new JArray (
141
+ sectionElement . Element ( "ul" ) . Elements ( "li" ) . Skip ( 1 ) . Select ( CreateJsonColourPair )
142
+ )
143
+ )
144
+ ) )
145
+ ) . ToString ( ) ;
146
+
147
+ //var javaScript = $"var swatches={json};";
78
148
var javaScript = string . Format ( "var swatches={0};" , json ) ;
79
149
80
- File . WriteAllText ( file , javaScript ) ;
150
+ File . WriteAllText ( file , javaScript ) ;
81
151
}
82
152
83
- private static JObject CreateJsonColourPair ( XElement liElement )
84
- {
85
- var name = liElement . Elements ( "span" ) . First ( ) . Value ;
86
- var hex = liElement . Elements ( "span" ) . Last ( ) . Value ;
87
-
88
- var prefix = "Primary" ;
89
- if ( name . StartsWith ( "A" ) )
90
- {
91
- prefix = "Accent" ;
92
- name = name . Skip ( 1 ) . Aggregate ( "" , ( current , next ) => current + next ) ;
93
- }
94
-
95
- var liClass = liElement . Attribute ( "class" ) . Value ;
96
- Color foregroundColour ;
97
- if ( ! ClassNameToForegroundIndex . TryGetValue ( liClass , out foregroundColour ) )
98
- throw new Exception ( "Unable to map foreground color from class " + liClass ) ;
99
-
100
- var foreGroundColorHex = string . Format ( "#{0}{1}{2}" ,
101
- ByteToHex ( foregroundColour . R ) ,
102
- ByteToHex ( foregroundColour . G ) ,
103
- ByteToHex ( foregroundColour . B ) ) ;
104
-
105
- var foregroundOpacity = Math . Round ( ( double ) foregroundColour . A / ( 255.0 ) , 2 ) ;
106
-
107
- return new JObject (
108
- new JProperty ( "backgroundName" , string . Format ( "{0}{1}" , prefix , name ) ) ,
109
- new JProperty ( "backgroundColour" , hex ) ,
110
- new JProperty ( "foregroundName" , string . Format ( "{0}{1}Foreground" , prefix , name ) ) ,
111
- new JProperty ( "foregroundColour" , foreGroundColorHex ) ,
112
- new JProperty ( "foregroundOpacity" , foregroundOpacity )
153
+ private static JObject CreateJsonColourPair ( XElement liElement )
154
+ {
155
+ var name = liElement . Elements ( "span" ) . First ( ) . Value ;
156
+ var hex = liElement . Elements ( "span" ) . Last ( ) . Value ;
157
+
158
+ var prefix = "Primary" ;
159
+ if ( name . StartsWith ( "A" ) )
160
+ {
161
+ prefix = "Accent" ;
162
+ name = name . Skip ( 1 ) . Aggregate ( "" , ( current , next ) => current + next ) ;
163
+ }
164
+
165
+ var liClass = liElement . Attribute ( "class" ) . Value ;
166
+ Color foregroundColour ;
167
+ if ( ! ClassNameToForegroundIndex . TryGetValue ( liClass , out foregroundColour ) )
168
+ throw new Exception ( "Unable to map foreground color from class " + liClass ) ;
169
+
170
+ var foreGroundColorHex = string . Format ( "#{0}{1}{2}" ,
171
+ ByteToHex ( foregroundColour . R ) ,
172
+ ByteToHex ( foregroundColour . G ) ,
173
+ ByteToHex ( foregroundColour . B ) ) ;
174
+
175
+ var foregroundOpacity = Math . Round ( ( double ) foregroundColour . A / ( 255.0 ) , 2 ) ;
176
+
177
+ return new JObject (
178
+ new JProperty ( "backgroundName" , string . Format ( "{0}{1}" , prefix , name ) ) ,
179
+ new JProperty ( "backgroundColour" , hex ) ,
180
+ new JProperty ( "foregroundName" , string . Format ( "{0}{1}Foreground" , prefix , name ) ) ,
181
+ new JProperty ( "foregroundColour" , foreGroundColorHex ) ,
182
+ new JProperty ( "foregroundOpacity" , foregroundOpacity )
113
183
) ;
114
- }
184
+ }
115
185
116
- private static Tuple < string , XDocument > ToResourceDictionary ( XElement sectionElement , bool named = false )
186
+ private static Tuple < string , XDocument > ToResourceDictionary ( XElement sectionElement , out bool empty , bool named = false , ColorMode mode = ColorMode . All )
117
187
{
188
+ empty = true ;
189
+
118
190
var colour = GetColourName ( sectionElement ) ;
119
191
120
192
XNamespace defaultNamespace = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation" ;
121
193
122
194
var xNamespace = XNamespace . Get ( "http://schemas.microsoft.com/winfx/2006/xaml" ) ;
123
195
var doc =
124
196
new XDocument ( new XElement ( defaultNamespace + "ResourceDictionary" ,
125
- new XAttribute ( XNamespace . Xmlns + "x" , xNamespace ) ) ) ;
197
+ new XAttribute ( XNamespace . Xmlns + "x" , xNamespace ) ) ) ;
126
198
127
199
foreach ( var liElement in sectionElement . Element ( "ul" ) . Elements ( "li" ) . Skip ( 1 ) )
128
200
{
129
- AddColour ( liElement , defaultNamespace , xNamespace , doc , named ? colour : "" ) ;
201
+ if ( AddColour ( liElement , defaultNamespace , xNamespace , doc , named ? colour : "" , mode ) )
202
+ empty = false ;
130
203
}
131
-
204
+
132
205
return new Tuple < string , XDocument > ( colour , doc ) ;
133
206
134
207
}
135
208
136
- private static string GetColourName ( XElement sectionElement )
137
- {
138
- return sectionElement . Element ( "ul" ) . Element ( "li" ) . Elements ( "span" ) . First ( ) . Value ;
139
- }
209
+ private static string GetColourName ( XElement sectionElement )
210
+ {
211
+ return sectionElement . Element ( "ul" ) . Element ( "li" ) . Elements ( "span" ) . First ( ) . Value ;
212
+ }
140
213
141
- private static void AddColour ( XElement liElement , XNamespace defaultNamespace , XNamespace xNamespace , XDocument doc , string swatchName = "" )
214
+ private static bool AddColour ( XElement liElement , XNamespace defaultNamespace , XNamespace xNamespace , XDocument doc , string swatchName = "" , ColorMode mode = ColorMode . All )
142
215
{
143
216
var name = liElement . Elements ( "span" ) . First ( ) . Value ;
144
217
var hex = liElement . Elements ( "span" ) . Last ( ) . Value ;
145
218
146
219
var prefix = "Primary" ;
147
- if ( name . StartsWith ( "A" ) )
220
+ if ( name . StartsWith ( "A" , StringComparison . Ordinal ) )
148
221
{
222
+ if ( mode == ColorMode . PrimaryOnly )
223
+ return false ;
224
+
149
225
prefix = "Accent" ;
150
226
name = name . Skip ( 1 ) . Aggregate ( "" , ( current , next ) => current + next ) ;
151
227
}
228
+ else
229
+ {
230
+ if ( mode == ColorMode . AccentOnly )
231
+ return false ;
232
+ }
152
233
153
234
var backgroundColourElement = new XElement ( defaultNamespace + "Color" , hex ) ;
154
235
// new XAttribute()
@@ -161,20 +242,29 @@ private static void AddColour(XElement liElement, XNamespace defaultNamespace, X
161
242
throw new Exception ( "Unable to map foreground color from class " + liClass ) ;
162
243
163
244
var foreGroundColorHex = string . Format ( "#{0}{1}{2}{3}" ,
164
- ByteToHex ( foregroundColour . A ) ,
165
- ByteToHex ( foregroundColour . R ) ,
166
- ByteToHex ( foregroundColour . G ) ,
245
+ ByteToHex ( foregroundColour . A ) ,
246
+ ByteToHex ( foregroundColour . R ) ,
247
+ ByteToHex ( foregroundColour . G ) ,
167
248
ByteToHex ( foregroundColour . B ) ) ;
168
249
169
250
var foregroundColourElement = new XElement ( defaultNamespace + "Color" , foreGroundColorHex ) ;
170
251
foregroundColourElement . Add ( new XAttribute ( xNamespace + "Key" , string . Format ( "{0}{1}{2}Foreground" , swatchName , prefix , name ) ) ) ;
171
252
doc . Root . Add ( foregroundColourElement ) ;
253
+
254
+ return true ;
172
255
}
173
256
174
257
private static string ByteToHex ( byte b )
175
258
{
176
259
var result = b . ToString ( "X" ) ;
177
260
return result . Length == 1 ? "0" + result : result ;
178
261
}
262
+
263
+ enum ColorMode
264
+ {
265
+ All ,
266
+ PrimaryOnly ,
267
+ AccentOnly
268
+ }
179
269
}
180
270
}
0 commit comments