TryParse() public static method

public static TryParse ( string hexString, SKColor &color ) : bool
hexString string
color SKColor
return bool
示例#1
0
        private SortedDictionary <float, SKColor> ReadStops(XElement e)
        {
            var stops = new SortedDictionary <float, SKColor>();

            var ns = e.Name.Namespace;

            foreach (var se in e.Elements(ns + "stop"))
            {
                var style = ReadStyle(se);

                var  offset = ReadNumber(style["offset"]);
                var  color  = SKColors.Black;
                byte alpha  = 255;

                string stopColor;
                if (style.TryGetValue("stop-color", out stopColor))
                {
                    // preserve alpha
                    if (SKColor.TryParse(stopColor, out color) && color.Alpha == 255)
                    {
                        alpha = color.Alpha;
                    }
                }

                string stopOpacity;
                if (style.TryGetValue("stop-opacity", out stopOpacity))
                {
                    alpha = (byte)(ReadNumber(stopOpacity) * 255);
                }

                color         = color.WithAlpha(alpha);
                stops[offset] = color;
            }

            return(stops);
        }
示例#2
0
        private void ReadPaints(Dictionary <string, string> style, ref SKPaint strokePaint, ref SKPaint fillPaint)
        {
            // stroke
            var stroke = GetString(style, "stroke").Trim();

            if (stroke.Equals("none", StringComparison.OrdinalIgnoreCase))
            {
                strokePaint = null;
            }
            else
            {
                if (string.IsNullOrEmpty(stroke))
                {
                    // no change
                }
                else
                {
                    if (strokePaint == null)
                    {
                        strokePaint = CreatePaint(true);
                    }

                    SKColor color;
                    if (SKColor.TryParse(stroke, out color))
                    {
                        // preserve alpha
                        if (color.Alpha == 255)
                        {
                            strokePaint.Color = color.WithAlpha(strokePaint.Color.Alpha);
                        }
                        else
                        {
                            strokePaint.Color = color;
                        }
                    }
                }

                // stroke attributes
                var strokeWidth = GetString(style, "stroke-width");
                if (!string.IsNullOrWhiteSpace(strokeWidth))
                {
                    if (strokePaint == null)
                    {
                        strokePaint = CreatePaint(true);
                    }
                    strokePaint.StrokeWidth = ReadNumber(strokeWidth);
                }

                var strokeOpacity = GetString(style, "stroke-opacity");
                if (!string.IsNullOrWhiteSpace(strokeOpacity))
                {
                    if (strokePaint == null)
                    {
                        strokePaint = CreatePaint(true);
                    }
                    strokePaint.Color = strokePaint.Color.WithAlpha((byte)(ReadNumber(strokeOpacity) * 255));
                }
            }

            // fill
            var fill = GetString(style, "fill").Trim();

            if (fill.Equals("none", StringComparison.OrdinalIgnoreCase))
            {
                fillPaint = null;
            }
            else
            {
                if (string.IsNullOrEmpty(fill))
                {
                    // no change
                }
                else
                {
                    if (fillPaint == null)
                    {
                        fillPaint = CreatePaint();
                    }

                    SKColor color;
                    if (SKColor.TryParse(fill, out color))
                    {
                        // preserve alpha
                        if (color.Alpha == 255)
                        {
                            fillPaint.Color = color.WithAlpha(fillPaint.Color.Alpha);
                        }
                        else
                        {
                            fillPaint.Color = color;
                        }
                    }
                    else
                    {
                        var read = false;
                        var urlM = fillUrlRe.Match(fill);
                        if (urlM.Success)
                        {
                            var id = urlM.Groups[1].Value.Trim();

                            XElement defE;
                            if (defs.TryGetValue(id, out defE))
                            {
                                var gradientShader = ReadGradient(defE);
                                if (gradientShader != null)
                                {
                                    // TODO: multiple shaders

                                    fillPaint.Shader = gradientShader;
                                    read             = true;
                                }
                                // else try another type (eg: image)
                            }
                            else
                            {
                                LogOrThrow($"Invalid fill url reference: {id}");
                            }
                        }

                        if (!read)
                        {
                            LogOrThrow($"Unsupported fill: {fill}");
                        }
                    }
                }

                // fill attributes
                var fillOpacity = GetString(style, "fill-opacity");
                if (!string.IsNullOrWhiteSpace(fillOpacity))
                {
                    if (fillPaint == null)
                    {
                        fillPaint = CreatePaint();
                    }

                    fillPaint.Color = fillPaint.Color.WithAlpha((byte)(ReadNumber(fillOpacity) * 255));
                }
            }
        }