internal static ColorScheme LoadFrom (Stream stream)
{
var result = new ColorScheme ();
byte [] bytes;
using (var sr = Core.Text.TextFileUtility.OpenStream (stream)) {
bytes = System.Text.Encoding.UTF8.GetBytes (sr.ReadToEnd ());
}
var reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader (bytes, new System.Xml.XmlDictionaryReaderQuotas ());
var root = XElement.Load(reader);
// The fields we'd like to extract
result.Name = root.XPathSelectElement("name").Value;
if (result.Name != "Default")
result.CopyValues (SyntaxModeService.DefaultColorStyle);
var version = Version.Parse (root.XPathSelectElement("version").Value);
if (version.Major != 1)
return null;
var el = root.XPathSelectElement ("description");
if (el != null)
result.Description = el.Value;
el = root.XPathSelectElement ("originator");
if (el != null)
result.Originator = el.Value;
el = root.XPathSelectElement ("baseScheme");
if (el != null)
result.BaseScheme = el.Value;
if (result.BaseScheme != null) {
var baseScheme = SyntaxModeService.GetColorStyle (result.BaseScheme);
if (baseScheme != null)
result.CopyValues (baseScheme);
}
var palette = new Dictionary<string, HslColor> ();
foreach (var color in root.XPathSelectElements("palette/*")) {
var name = color.XPathSelectElement ("name").Value;
if (palette.ContainsKey (name))
throw new InvalidDataException ("Duplicate palette color definition for: " + name);
palette.Add (
name,
ParseColor (color.XPathSelectElement ("value").Value)
);
}
foreach (var colorElement in root.XPathSelectElements("//colors/*")) {
var color = AmbientColor.Create (colorElement, palette);
PropertyDecsription info;
if (!ambientColors.TryGetValue (color.Name, out info)) {
Console.WriteLine ("Ambient color:" + color.Name + " not found.");
continue;
}
info.Info.SetValue (result, color, null);
}
foreach (var textColorElement in root.XPathSelectElements("//text/*")) {
var color = ChunkStyle.Create (textColorElement, palette);
PropertyDecsription info;
if (!textColors.TryGetValue (color.Name, out info)) {
Console.WriteLine ("Text color:" + color.Name + " not found.");
continue;
}
info.Info.SetValue (result, color, null);
}
// Check scheme
bool valid = true;
foreach (var color in textColors.Values) {
if (color.Info.GetValue (result, null) == null) {
Console.WriteLine (color.Attribute.Name + " == null");
valid = false;
}
}
foreach (var color in ambientColors.Values) {
if (color.Info.GetValue (result, null) == null) {
Console.WriteLine (color.Attribute.Name + " == null");
valid = false;
}
}
if (!valid)
throw new InvalidDataException ("Scheme " + result.Name + " is not valid.");
return result;
}