public Color(string colorString) { Color color; if (Colors.TryParse(colorString, out color)) { R = color.R; G = color.G; B = color.B; A = color.A; } else { throw new ArgumentException("Bad color string: " + colorString); } }
Color ReadColor(string raw) { if (string.IsNullOrWhiteSpace(raw) || raw.Equals("none", StringComparison.OrdinalIgnoreCase)) { return(Colors.Clear); } var s = raw.Trim(); if (s.Length == 7 && s [0] == '#') { var r = int.Parse(s.Substring(1, 2), NumberStyles.HexNumber, icult); var g = int.Parse(s.Substring(3, 2), NumberStyles.HexNumber, icult); var b = int.Parse(s.Substring(5, 2), NumberStyles.HexNumber, icult); return(new Color(r / 255.0, g / 255.0, b / 255.0, 1)); } var match = rgbRe.Match(s); if (match.Success && match.Groups.Count == 4) { var r = int.Parse(match.Groups[1].Value); var g = int.Parse(match.Groups[2].Value); var b = int.Parse(match.Groups[3].Value); return(new Color(r / 255.0, g / 255.0, b / 255.0, 1)); } Color color; if (Colors.TryParse(s, out color)) { return(color); } throw new NotSupportedException("Color " + s); }
void ApplyStyle(Dictionary <string, string> style, ref Pen pen, out bool hasPen, ref Brush brush, out bool hasBrush) { // // Pen attributes // var strokeWidth = GetString(style, "stroke-width"); if (!string.IsNullOrWhiteSpace(strokeWidth)) { if (pen == null) { pen = new Pen(); } pen.Width = ReadNumber(strokeWidth); } var strokeOpacity = GetString(style, "stroke-opacity"); if (string.IsNullOrWhiteSpace(strokeOpacity)) { strokeOpacity = GetString(style, "opacity"); } if (!string.IsNullOrWhiteSpace(strokeOpacity)) { if (pen == null) { pen = new Pen(); } pen.Color = pen.Color.WithAlpha(ReadNumber(strokeOpacity)); } // // Pen // var stroke = GetString(style, "stroke").Trim(); if (string.IsNullOrEmpty(stroke)) { // No change hasPen = false; } else if (stroke.Equals("none", StringComparison.OrdinalIgnoreCase)) { hasPen = true; pen = null; } else { hasPen = true; if (pen == null) { pen = new Pen(); } Color color; if (Colors.TryParse(stroke, out color)) { if (pen.Color.Alpha == 1) { pen.Color = color; } else { pen.Color = color.WithAlpha(pen.Color.Alpha); } } } // // Brush attributes // var fillOpacity = GetString(style, "fill-opacity"); if (string.IsNullOrWhiteSpace(fillOpacity)) { fillOpacity = GetString(style, "opacity"); } if (!string.IsNullOrWhiteSpace(fillOpacity)) { if (brush == null) { brush = new SolidBrush(); } var sb = brush as SolidBrush; if (sb != null) { sb.Color = sb.Color.WithAlpha(ReadNumber(fillOpacity)); } } // // Brush // var fill = GetString(style, "fill").Trim(); if (string.IsNullOrEmpty(fill)) { // No change hasBrush = false; } else if (fill.Equals("none", StringComparison.OrdinalIgnoreCase)) { hasBrush = true; brush = null; } else { hasBrush = true; Color color; if (Colors.TryParse(fill, out color)) { var sb = brush as SolidBrush; if (sb == null) { brush = new SolidBrush(color); } else { if (sb.Color.Alpha == 1) { sb.Color = color; } else { sb.Color = color.WithAlpha(sb.Color.Alpha); } } } else { var urlM = fillUrlRe.Match(fill); if (urlM.Success) { var id = urlM.Groups [1].Value.Trim(); brush = GetGradientBrush(id, null); } else { throw new NotSupportedException("Fill " + fill); } } } }
void ApplyStyle(Dictionary <string, string> style, ref Pen pen, out bool hasPen, ref Brush brush, out bool hasBrush) { // // Pen attributes // var strokeWidth = GetString(style, "stroke-width"); if (!string.IsNullOrWhiteSpace(strokeWidth)) { if (pen == null) { pen = new Pen(); } pen.Width = ReadNumber(strokeWidth); } var strokeOpacity = GetString(style, "stroke-opacity"); if (!string.IsNullOrWhiteSpace(strokeOpacity)) { if (pen == null) { pen = new Pen(); } pen.Color = pen.Color.WithAlpha(ReadNumber(strokeOpacity)); } var strokeLineCap = GetString(style, "stroke-linecap"); if (!string.IsNullOrWhiteSpace(strokeLineCap)) { if (pen == null) { pen = new Pen(); } strokeLineCap = strokeLineCap.ToLower(); switch (strokeLineCap) { case "butt": pen.Cap = LineCaps.Butt; break; case "square": pen.Cap = LineCaps.Square; break; case "round": pen.Cap = LineCaps.Round; break; } } var strokeLineJoin = GetString(style, "stroke-linejoin"); if (!string.IsNullOrWhiteSpace(strokeLineJoin)) { if (pen == null) { pen = new Pen(); } strokeLineJoin = strokeLineJoin.ToLower(); switch (strokeLineJoin) { case "bevel": throw new NotSupportedException(); case "miter": pen.Join = LineJoin.Miter; break; case "round": pen.Join = LineJoin.Round; break; } } var strokeMiterLimit = GetString(style, "stroke-miterlimit"); if (!string.IsNullOrWhiteSpace(strokeLineJoin)) { if (pen == null) { pen = new Pen(); } pen.MiterLimit = ReadNumber(strokeMiterLimit); } // // Pen // var stroke = GetString(style, "stroke").Trim(); if (string.IsNullOrEmpty(stroke)) { // No change hasPen = false; } else if (stroke.Equals("none", StringComparison.OrdinalIgnoreCase)) { hasPen = true; pen = null; } else { hasPen = true; if (pen == null) { pen = new Pen(); } Color color; if (Colors.TryParse(stroke, out color)) { if (pen.Color.Alpha == 1) { pen.Color = color; } else { pen.Color = color.WithAlpha(pen.Color.Alpha); } } } // // Brush attributes // var fillOpacity = GetString(style, "fill-opacity"); if (!string.IsNullOrWhiteSpace(fillOpacity)) { if (brush == null) { brush = new SolidBrush(); } var sb = brush as SolidBrush; if (sb != null) { sb.Color = sb.Color.WithAlpha(ReadNumber(fillOpacity)); } } var fillMode = GetString(style, "fill-rule"); if (!string.IsNullOrWhiteSpace(fillMode)) { if (brush == null) { brush = new SolidBrush(); } var sb = brush as SolidBrush; if (sb != null) { switch (fillMode) { case "evenodd": brush.FillMode = FillMode.EvenOdd; break; case "nonzero": brush.FillMode = FillMode.NonZero; break; } } } // // Brush // var fill = GetString(style, "fill").Trim(); if (string.IsNullOrEmpty(fill)) { // No change hasBrush = false; } else if (fill.Equals("none", StringComparison.OrdinalIgnoreCase)) { hasBrush = true; brush = null; } else { hasBrush = true; Color color; if (Colors.TryParse(fill, out color)) { var sb = brush as SolidBrush; if (sb == null) { brush = new SolidBrush(color); } else { if (sb.Color.Alpha == 1) { sb.Color = color; } else { sb.Color = color.WithAlpha(sb.Color.Alpha); } } } else { var urlM = fillUrlRe.Match(fill); if (urlM.Success) { var id = urlM.Groups[1].Value.Trim(); brush = GetGradientBrush(id, null); } else { throw new NotSupportedException("Fill " + fill); } } } }