static void LoadXamlGradients(string path) { TypeConverter converter = TypeDescriptor.GetConverter(typeof(Color)); foreach (var file in Directory.GetFiles(path, "*.xaml")) { XmlDocument doc = new XmlDocument(); doc.Load(file); foreach (XmlElement g in doc.GetElementsByTagName("LinearGradientBrush")) { List <GradientStop> stops = new List <GradientStop>(); foreach (XmlElement e in g.GetElementsByTagName("GradientStop")) { Color color = (Color)converter.ConvertFrom(e.GetAttribute("Color")); double offset = 0; string os = e.GetAttribute("Offset"); double.TryParse(os, out offset); stops.Add(new GradientStop { Color = color, Offset = offset }); } stops.Sort(delegate(GradientStop a, GradientStop b) { return(a.Offset.CompareTo(b.Offset)); }); SegmentGradient sg = new SegmentGradient(); LinearGradient lg = null; for (int i = 0; i < stops.Count; i++) { var stop = stops[i]; if (lg != null) { lg.UpperColor = stop.Color; sg.Add(new SegmentGradient.Segment(stop.Offset, lg)); lg = null; } if (i < stops.Count - 1 && stop.Offset == stops[i + 1].Offset) { continue; } lg = new LinearGradient(); lg.LowerColor = stop.Color; } List.Add(sg); } } }
/// <summary> /// Loads a GIMP-Gradient .ggr file /// </summary> /// <param name="filename">The path of the file to load.</param> public static SegmentGradient LoadGIMP(string filename) { SegmentGradient sg = new SegmentGradient(); try { StreamReader r = new StreamReader(filename); string s = null; int line = 0, segments = 0; while (line != 3 && ((s = r.ReadLine()) != null)) { line++; } if (s != null) { segments = int.Parse(s); } while (segments > 0 && ((s = r.ReadLine()) != null)) { string[] tokens = s.Split(new char[2] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); LinearGradient g = new LinearGradient(); g.LowerColor = System.Drawing.Color.FromArgb((int)(255 * double.Parse(tokens[6]) + 0.5), (int)(255 * double.Parse(tokens[3]) + 0.5), (int)(255 * double.Parse(tokens[4]) + 0.5), (int)(255 * double.Parse(tokens[5]) + 0.5)); g.UpperColor = System.Drawing.Color.FromArgb((int)(255 * double.Parse(tokens[10]) + 0.5), (int)(255 * double.Parse(tokens[7]) + 0.5), (int)(255 * double.Parse(tokens[8]) + 0.5), (int)(255 * double.Parse(tokens[9]) + 0.5)); sg.Add(new Segment(double.Parse(tokens[2]), g)); } } catch (Exception e0) { ArgumentException e1 = new ArgumentException("Invalid GIMP gradient file format", e0); e1.Data["filename"] = filename; throw e1; } return(sg); }