Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ThemeDefinition.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Xml;
5using SkiaSharp;
7using Waher.Script;
10
12{
16 public class ThemeDefinition
17 {
18 private readonly Dictionary<string, string> customProperties = new Dictionary<string, string>();
19 private readonly ThemeImage[] backgroundImages = null;
20 private readonly ThemeImage[] bannerImages = null;
21 private readonly ThemeImage thumbnail;
22 private readonly string id;
23 private readonly string title;
24 private readonly string cssx;
25 private readonly string fontFamily;
26 private readonly SKColor textColor;
27 private readonly SKColor backgroundColor;
28 private readonly SKColor headerColor;
29 private readonly SKColor headerTextColor;
30 private readonly SKColor buttonColor;
31 private readonly SKColor buttonTextColor;
32 private readonly SKColor menuTextColor;
33 private readonly SKColor insertColor;
34 private readonly SKColor deleteColor;
35 private readonly SKColor linkColorUnvisited;
36 private readonly SKColor linkColorVisited;
37 private readonly SKColor linkColorHot;
38 private readonly SKColor graphBgColor;
39 private readonly SKColor graphFgColor;
40
45 internal ThemeDefinition(XmlDocument Xml)
46 {
47 this.id = XML.Attribute(Xml.DocumentElement, "id");
48
49 foreach (XmlNode N in Xml.DocumentElement.ChildNodes)
50 {
51 if (N is XmlElement E)
52 {
53 switch (E.LocalName)
54 {
55 case "Presentation":
56 foreach (XmlNode N2 in E.ChildNodes)
57 {
58 if (N2 is XmlElement E2)
59 {
60 switch (E2.LocalName)
61 {
62 case "Title":
63 this.title = E2.InnerText;
64 break;
65
66 case "Thumbnail":
67 this.thumbnail = this.ParseThemeImage(E2);
68 break;
69 }
70 }
71 }
72 break;
73
74 case "BasicProperties":
75 foreach (XmlNode N2 in E.ChildNodes)
76 {
77 if (N2 is XmlElement E2)
78 {
79 switch (E2.LocalName)
80 {
81 case "CSSX":
82 this.cssx = this.GetResourceName(E2.InnerText);
83 break;
84
85 case "TextColor":
86 if (Color.TryParse(E2.InnerText, out SKColor cl))
87 this.textColor = cl;
88 break;
89
90 case "BackgroundColor":
91 if (Color.TryParse(E2.InnerText, out cl))
92 this.backgroundColor = cl;
93 break;
94
95 case "HeaderColor":
96 if (Color.TryParse(E2.InnerText, out cl))
97 this.headerColor = cl;
98 break;
99
100 case "HeaderTextColor":
101 if (Color.TryParse(E2.InnerText, out cl))
102 this.headerTextColor = cl;
103 break;
104
105 case "ButtonColor":
106 if (Color.TryParse(E2.InnerText, out cl))
107 this.buttonColor = cl;
108 break;
109
110 case "ButtonTextColor":
111 if (Color.TryParse(E2.InnerText, out cl))
112 this.buttonTextColor = cl;
113 break;
114
115 case "MenuTextColor":
116 if (Color.TryParse(E2.InnerText, out cl))
117 this.menuTextColor = cl;
118 break;
119
120 case "InsertColor":
121 if (Color.TryParse(E2.InnerText, out cl))
122 this.insertColor = cl;
123 break;
124
125 case "DeleteColor":
126 if (Color.TryParse(E2.InnerText, out cl))
127 this.deleteColor = cl;
128 break;
129
130 case "LinkColorUnvisited":
131 if (Color.TryParse(E2.InnerText, out cl))
132 this.linkColorUnvisited = cl;
133 break;
134
135 case "LinkColorVisited":
136 if (Color.TryParse(E2.InnerText, out cl))
137 this.linkColorVisited = cl;
138 break;
139
140 case "LinkColorHot":
141 if (Color.TryParse(E2.InnerText, out cl))
142 this.linkColorHot = cl;
143 break;
144
145 case "FontFamily":
146 this.fontFamily = E2.InnerText;
147 break;
148
150 if (Color.TryParse(E2.InnerText, out cl))
151 this.graphBgColor = cl;
152 break;
153
155 if (Color.TryParse(E2.InnerText, out cl))
156 this.graphFgColor = cl;
157 break;
158 }
159 }
160 }
161 break;
162
163 case "CustomProperties":
164 foreach (XmlNode N2 in E.ChildNodes)
165 {
166 if (N2 is XmlElement E2 && E2.LocalName == "Property")
167 {
168 string Name = XML.Attribute(E2, "name");
169 string Value = XML.Attribute(E2, "value");
170
171 this.customProperties[Name] = Value;
172 }
173 }
174 break;
175
176 case "BackgroundImages":
177 List<ThemeImage> Images = new List<ThemeImage>();
178
179 foreach (XmlNode N2 in E.ChildNodes)
180 {
181 if (N2 is XmlElement E2 && E2.LocalName == "BackgroundImage")
182 Images.Add(this.ParseThemeImage(E2));
183 }
184
185 this.backgroundImages = Images.ToArray();
186 break;
187
188 case "BannerImages":
189 Images = new List<ThemeImage>();
190
191 foreach (XmlNode N2 in E.ChildNodes)
192 {
193 if (N2 is XmlElement E2 && E2.LocalName == "BannerImage")
194 Images.Add(this.ParseThemeImage(E2));
195 }
196
197 this.bannerImages = Images.ToArray();
198 break;
199 }
200 }
201 }
202 }
203
204 private ThemeImage ParseThemeImage(XmlElement E)
205 {
206 string FileName = E.InnerText;
207 int Width = XML.Attribute(E, "width", 0);
208 int Height = XML.Attribute(E, "height", 0);
209
210 return new ThemeImage(this.GetResourceName(FileName), Width, Height);
211 }
212
213 private string GetResourceName(string FileName)
214 {
215 return "/Themes/" + this.id + "/" + FileName;
216 }
217
223 public string this[string key] => this.customProperties[key];
224
228 public string Id => this.id;
229
233 public string Title => this.title;
234
238 public ThemeImage Thumbnail => this.thumbnail;
239
243 public string CSSX => this.cssx;
244
248 public SKColor TextColor => this.textColor;
249
253 public SKColor BackgroundColor => this.backgroundColor;
254
258 public SKColor HeaderColor => this.headerColor;
259
263 public SKColor HeaderTextColor => this.headerTextColor;
264
268 public SKColor ButtonColor => this.buttonColor;
269
273 public SKColor ButtonTextColor => this.buttonTextColor;
274
278 public SKColor MenuTextColor => this.menuTextColor;
279
283 public SKColor InsertColor => this.insertColor;
284
288 public SKColor DeleteColor => this.deleteColor;
289
293 public SKColor LinkColorUnvisited => this.linkColorUnvisited;
294
298 public SKColor LinkColorVisited => this.linkColorVisited;
299
303 public SKColor LinkColorHot => this.linkColorHot;
304
308 public string FontFamily => this.fontFamily;
309
313 public SKColor GraphBgColor => this.graphBgColor;
314
318 public SKColor GraphFgColor => this.graphFgColor;
319
323 public ThemeImage[] BackgroundImages => this.backgroundImages;
324
328 public ThemeImage[] BannerImages => this.bannerImages;
329
336 public ThemeImage GetBackgroundImage(int Width, int Height)
337 {
338 return this.GetImage(this.backgroundImages, Width, Height);
339 }
340
347 public ThemeImage GetBannerImage(int Width, int Height)
348 {
349 return this.GetImage(this.bannerImages, Width, Height);
350 }
351
352 private ThemeImage GetImage(ThemeImage[] Images, int Width, int Height)
353 {
354 ThemeImage Best = null;
355 double BestSqrError = double.MaxValue;
356 double SqrError, d;
357
358 foreach (ThemeImage Img in Images)
359 {
360 d = Img.Width - Width;
361 SqrError = d * d;
362
363 d = Img.Height - Height;
364 SqrError += d * d;
365
366 if (Best is null || SqrError < BestSqrError)
367 {
368 Best = Img;
369 BestSqrError = SqrError;
370 }
371 }
372
373 return Best;
374 }
375
380 public KeyValuePair<string, string>[] GetCustomProperties()
381 {
382 KeyValuePair<string, string>[] Result = new KeyValuePair<string, string>[this.customProperties.Count];
383 int i = 0;
384
385 foreach (KeyValuePair<string, string> P in this.customProperties)
386 Result[i++] = P;
387
388 return Result;
389 }
390
396 {
397 Variables["TextColor"] = this.textColor;
398 Variables["BackgroundColor"] = this.backgroundColor;
399 Variables["HeaderColor"] = this.headerColor;
400 Variables["HeaderTextColor"] = this.headerTextColor;
401 Variables["ButtonColor"] = this.buttonColor;
402 Variables["ButtonTextColor"] = this.buttonTextColor;
403 Variables["MenuTextColor"] = this.menuTextColor;
404 Variables["InsertColor"] = this.insertColor;
405 Variables["DeleteColor"] = this.deleteColor;
406 Variables["LinkColorUnvisited"] = this.linkColorUnvisited;
407 Variables["LinkColorVisited"] = this.linkColorVisited;
408 Variables["LinkColorHot"] = this.linkColorHot;
409 Variables["FontFamily"] = this.fontFamily;
410 Variables["BackgroundImages"] = this.backgroundImages;
411 Variables["BannerImages"] = this.bannerImages;
412 Variables[Graph.GraphBgColorVariableName] = this.graphBgColor;
413 Variables[Graph.GraphFgColorVariableName] = this.graphFgColor;
414
415 foreach (KeyValuePair<string, string> P in this.GetCustomProperties())
416 Variables[P.Key] = P.Value;
417 }
418
419 }
420}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Contains properties for a theme.
ThemeImage[] BannerImages
Banner images.
ThemeImage GetBackgroundImage(int Width, int Height)
Gets a background image best matching a given size.
SKColor TextColor
Color for normal text.
void Prepare(Variables Variables)
Prepares a collection of variables for transforming content based on a theme.
ThemeImage[] BackgroundImages
Background images.
SKColor GraphFgColor
Graph foreground color.
SKColor HeaderColor
Color for text headers.
SKColor DeleteColor
Backgound color for deleted text.
SKColor LinkColorUnvisited
Color of unvisited links.
SKColor MenuTextColor
Text color for links in the menu.
string Title
A human readable title for the theme.
SKColor ButtonColor
Background color for controls.
string CSSX
Resource of the CSSX file of the theme
SKColor LinkColorVisited
Color of visited links.
SKColor LinkColorHot
Color of hot links.
ThemeImage Thumbnail
Thumbnail for the theme
SKColor ButtonTextColor
Text color for controls.
SKColor InsertColor
Backgound color for inserted text.
string FontFamily
CSS font-family value.
SKColor GraphBgColor
Graph background color.
SKColor HeaderTextColor
Text color for text headers, if used with backgound HeaderColor.
KeyValuePair< string, string >[] GetCustomProperties()
Gets defined custom properties.
ThemeImage GetBannerImage(int Width, int Height)
Gets a banner image best matching a given size.
SKColor BackgroundColor
Background Color for normal text.
Contains a reference to an image in the theme.
Definition: ThemeImage.cs:11
Returns a color value from a string.
Definition: Color.cs:14
static bool TryParse(string s, out SKColor Color)
Tries to parse a string containing a color name.
Definition: Color.cs:69
Base class for graphs.
Definition: Graph.cs:79
const string GraphFgColorVariableName
Variable name for graph foreground color.
Definition: Graph.cs:98
const string GraphBgColorVariableName
Variable name for graph background color.
Definition: Graph.cs:93
Collection of variables.
Definition: Variables.cs:25