Skip to content

Commit c92deb6

Browse files
committed
Change mdresgen to new format
1 parent 124e7f6 commit c92deb6

File tree

4 files changed

+215
-87
lines changed

4 files changed

+215
-87
lines changed

mdresgen/Program.cs

Lines changed: 177 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
using System;
1+
using Newtonsoft.Json.Linq;
2+
using System;
23
using System.Collections.Generic;
34
using System.Drawing;
45
using System.IO;
56
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
87
using System.Xml.Linq;
9-
using System.Xml.Xsl;
10-
using Newtonsoft.Json.Linq;
118

129
namespace mdresgen
1310
{
1411
class Program
1512
{
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+
1630
private static readonly IDictionary<string, Color> ClassNameToForegroundIndex = new Dictionary<string, Color>()
1731
{
1832
{"color", Color.FromArgb((int) (255*0.87), 255, 255, 255)},
@@ -25,130 +39,197 @@ class Program
2539

2640
static void Main(string[] args)
2741
{
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);
3454
else
35-
GenerateXaml(xDocument);
55+
GenerateXaml(xDocument, false);
56+
57+
Console.WriteLine();
58+
Console.WriteLine();
59+
Console.WriteLine("FINISHED");
60+
Console.ReadKey();
3661
}
3762

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);
4170

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+
}
47113

48-
private static void GenerateXamlNamed(XDocument xDocument)
114+
private static void GenerateOldXaml(XDocument xDocument, bool named = false)
49115
{
50-
const string fileFormat = @"..\..\..\Themes\MaterialDesignColor.{0}.Named.xaml";
116+
Console.WriteLine("Generating old {0} XAMLs", named ? "named" : "regular");
51117

52-
Console.WriteLine("Running named gen");
118+
bool dummy;
53119

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)))
55121
{
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+
));
57127
}
58128
}
59129

60130
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};";
78148
var javaScript = string.Format("var swatches={0};", json);
79149

80-
File.WriteAllText(file, javaScript);
150+
File.WriteAllText(file, javaScript);
81151
}
82152

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)
113183
);
114-
}
184+
}
115185

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)
117187
{
188+
empty = true;
189+
118190
var colour = GetColourName(sectionElement);
119191

120192
XNamespace defaultNamespace = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation";
121193

122194
var xNamespace = XNamespace.Get("http://schemas.microsoft.com/winfx/2006/xaml");
123195
var doc =
124196
new XDocument(new XElement(defaultNamespace + "ResourceDictionary",
125-
new XAttribute(XNamespace.Xmlns + "x", xNamespace)));
197+
new XAttribute(XNamespace.Xmlns + "x", xNamespace)));
126198

127199
foreach (var liElement in sectionElement.Element("ul").Elements("li").Skip(1))
128200
{
129-
AddColour(liElement, defaultNamespace, xNamespace, doc, named ? colour : "");
201+
if (AddColour(liElement, defaultNamespace, xNamespace, doc, named ? colour : "", mode))
202+
empty = false;
130203
}
131-
204+
132205
return new Tuple<string, XDocument>(colour, doc);
133206

134207
}
135208

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+
}
140213

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)
142215
{
143216
var name = liElement.Elements("span").First().Value;
144217
var hex = liElement.Elements("span").Last().Value;
145218

146219
var prefix = "Primary";
147-
if (name.StartsWith("A"))
220+
if (name.StartsWith("A", StringComparison.Ordinal))
148221
{
222+
if (mode == ColorMode.PrimaryOnly)
223+
return false;
224+
149225
prefix = "Accent";
150226
name = name.Skip(1).Aggregate("", (current, next) => current + next);
151227
}
228+
else
229+
{
230+
if (mode == ColorMode.AccentOnly)
231+
return false;
232+
}
152233

153234
var backgroundColourElement = new XElement(defaultNamespace + "Color", hex);
154235
// new XAttribute()
@@ -161,20 +242,29 @@ private static void AddColour(XElement liElement, XNamespace defaultNamespace, X
161242
throw new Exception("Unable to map foreground color from class " + liClass);
162243

163244
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),
167248
ByteToHex(foregroundColour.B));
168249

169250
var foregroundColourElement = new XElement(defaultNamespace + "Color", foreGroundColorHex);
170251
foregroundColourElement.Add(new XAttribute(xNamespace + "Key", string.Format("{0}{1}{2}Foreground", swatchName, prefix, name)));
171252
doc.Root.Add(foregroundColourElement);
253+
254+
return true;
172255
}
173256

174257
private static string ByteToHex(byte b)
175258
{
176259
var result = b.ToString("X");
177260
return result.Length == 1 ? "0" + result : result;
178261
}
262+
263+
enum ColorMode
264+
{
265+
All,
266+
PrimaryOnly,
267+
AccentOnly
268+
}
179269
}
180270
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3+
4+
<ResourceDictionary.MergedDictionaries>
5+
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.$COLOR.Accent.xaml" />
6+
</ResourceDictionary.MergedDictionaries>
7+
8+
<SolidColorBrush x:Key="SecondaryAccentBrush" Color="{StaticResource Accent700}"/>
9+
<SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="{StaticResource Accent700Foreground}"/>
10+
11+
</ResourceDictionary>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3+
4+
<ResourceDictionary.MergedDictionaries>
5+
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.$COLOR.Primary.xaml" />
6+
</ResourceDictionary.MergedDictionaries>
7+
8+
<SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary200}"/>
9+
<SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary200Foreground}"/>
10+
<SolidColorBrush x:Key="PrimaryHueMidBrush" Color="{StaticResource Primary500}"/>
11+
<SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/>
12+
<SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary700}"/>
13+
<SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary700Foreground}"/>
14+
15+
</ResourceDictionary>

mdresgen/mdresgen.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@
7070
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7171
</Content>
7272
</ItemGroup>
73+
<ItemGroup>
74+
<None Include="RecommendedAccentTemplate.xaml">
75+
<Generator>MSBuild:Compile</Generator>
76+
<SubType>Designer</SubType>
77+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
78+
</None>
79+
<None Include="RecommendedPrimaryTemplate.xaml">
80+
<Generator>MSBuild:Compile</Generator>
81+
<SubType>Designer</SubType>
82+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
83+
</None>
84+
</ItemGroup>
7385
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7486
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
7587
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

0 commit comments

Comments
 (0)