/// <summary> /// Constructs a <see cref="RadialGradientBrush"/> from the specified <paramref name="source"/>. /// </summary> /// <param name="source">A string representation of a <see cref="RadialGradientBrush"/>.</param> /// <returns>The equivalent <see cref="RadialGradientBrush"/>.</returns> /// <remarks>Format: RadialGradient [<center> [<gradient_origin>]] [<radius_x> <radius_y>] (<stop_color>","<stop_offset> ){2..n}</remarks> public static RadialGradientBrush Parse(string source) { Match bodyMatch = Regex.Match(source, RadialGradientParsingPattern, RegexOptions.IgnoreCase); if (!bodyMatch.Success) { throw new ArgumentException("Radial gradient literal is not valid."); } GradientStopCollection gradientStopCollection; if (bodyMatch.Groups.ContainsKey("stops")) { gradientStopCollection = GradientStopCollection.Parse(bodyMatch.Groups["stops"].Value); } else { throw new ArgumentException("Radial gradient literal does not define any gradient stops."); } RadialGradientBrush radialGradientBrush = new RadialGradientBrush(gradientStopCollection); if (bodyMatch.Groups.ContainsKey("center")) { radialGradientBrush.Center = Point.Parse(bodyMatch.Groups["center"].Value); } if (bodyMatch.Groups.ContainsKey("gradient_origin")) { radialGradientBrush.GradientOrigin = Point.Parse(bodyMatch.Groups["gradient_origin"].Value); } if (bodyMatch.Groups.ContainsKey("radius_x")) { double radiusX = Convert.ToDouble(bodyMatch.Groups["radius_x"].Value); radialGradientBrush.RadiusX = radiusX; } if (bodyMatch.Groups.ContainsKey("radius_y")) { double radiusY = Convert.ToDouble(bodyMatch.Groups["radius_y"].Value); radialGradientBrush.RadiusY = radiusY; } return(radialGradientBrush); }
/// <summary> /// Constructs a <see cref="LinearGradientBrush"/> from the specified <paramref name="source"/>. /// </summary> /// <param name="source">A string representation of a <see cref="LinearGradientBrush"/>.</param> /// <returns>The equivalent <see cref="LinearGradientBrush"/>.</returns> /// <remarks>Format: LinearGradient (<start_point> [<end_point>])|(<angle>["deg"]) (<stop_color>","<stop_offset> ){2..n}</remarks> public static LinearGradientBrush Parse(string source) { Match bodyMatch = Regex.Match(source, LinearGradientParsingPattern, RegexOptions.IgnoreCase); if (!bodyMatch.Success) { throw new ArgumentException("Linear gradient literal is not valid."); } GradientStopCollection gradientStopCollection; if (bodyMatch.Groups.ContainsKey("stops")) { gradientStopCollection = GradientStopCollection.Parse(bodyMatch.Groups["stops"].Value); } else { throw new ArgumentException("Linear gradient literal does not define any gradient stops."); } LinearGradientBrush linearGradientBrush; if (bodyMatch.Groups.ContainsKey("angle")) { double angle = Convert.ToDouble(bodyMatch.Groups["angle"].Value); linearGradientBrush = new LinearGradientBrush(gradientStopCollection, angle); } else { Point startPoint = Point.Parse(bodyMatch.Groups["start_point"].Value); Point endPoint = new Point(1, 1); if (bodyMatch.Groups.ContainsKey("end_point")) { endPoint = Point.Parse(bodyMatch.Groups["end_point"].Value); } linearGradientBrush = new LinearGradientBrush(gradientStopCollection, startPoint, endPoint); } return(linearGradientBrush); }