Пример #1
0
        public static GradientStopCollection ToGradientStops(System.Xml.XmlNodeList stops)
        {
            int itemCount = stops.Count;
            GradientStopCollection gradientStops = new GradientStopCollection(itemCount);

            double lastOffset = 0;

            for (int i = 0; i < itemCount; i++)
            {
                SvgStopElement stop  = (SvgStopElement)stops.Item(i);
                string         prop  = stop.GetAttribute("stop-color");
                string         style = stop.GetAttribute("style");
                Color          color = Colors.Transparent; // no auto-inherited...
                if (!string.IsNullOrWhiteSpace(prop) || !string.IsNullOrWhiteSpace(style))
                {
                    SvgColor svgColor = new SvgColor(stop.GetComputedStyle(string.Empty).GetPropertyValue("stop-color"));
                    if (svgColor.ColorType == SvgColorType.CurrentColor)
                    {
                        string sCurColor = stop.GetComputedStyle(string.Empty).GetPropertyValue(CssConstants.PropColor);
                        svgColor = new SvgColor(sCurColor);
                    }
                    TryConvertColor(svgColor.RgbColor, out color);
                }
                else
                {
                    color = Colors.Black; // the default color...
                }

                double alpha = 255;
                string opacity;

                opacity = stop.GetAttribute("stop-opacity"); // no auto-inherit
                if (opacity == "inherit")                    // if explicitly defined...
                {
                    opacity = stop.GetPropertyValue("stop-opacity");
                }
                if (!string.IsNullOrWhiteSpace(opacity))
                {
                    alpha *= SvgNumber.ParseNumber(opacity);
                }

                alpha = Math.Min(alpha, 255);
                alpha = Math.Max(alpha, 0);

                color = Color.FromArgb((byte)Convert.ToInt32(alpha),
                                       color.R, color.G, color.B);

                double offset = stop.Offset.AnimVal;

                offset /= 100;
                offset  = Math.Max(lastOffset, offset);

                gradientStops.Add(new GradientStop(color, offset));
                lastOffset = offset;
            }

            return(gradientStops);
        }
Пример #2
0
        private GradientStopCollection GetGradientStops(XmlNodeList stops)
        {
            int itemCount = stops.Count;
            GradientStopCollection gradientStops = new GradientStopCollection(itemCount);

            double lastOffset = 0;

            for (int i = 0; i < itemCount; i++)
            {
                SvgStopElement stop  = (SvgStopElement)stops.Item(i);
                string         prop  = stop.GetAttribute("stop-color");
                string         style = stop.GetAttribute("style");
                Color          color = Colors.Transparent; // no auto-inherited...
                if (!String.IsNullOrEmpty(prop) || !String.IsNullOrEmpty(style))
                {
                    WpfSvgColor svgColor = new WpfSvgColor(stop, "stop-color");
                    color = svgColor.Color;
                }
                else
                {
                    color = Colors.Black; // the default color...
                    double alpha = 255;
                    string opacity;

                    opacity = stop.GetAttribute("stop-opacity"); // no auto-inherit
                    if (opacity == "inherit")                    // if explicitly defined...
                    {
                        opacity = stop.GetPropertyValue("stop-opacity");
                    }
                    if (opacity != null && opacity.Length > 0)
                    {
                        alpha *= SvgNumber.ParseNumber(opacity);
                    }

                    alpha = Math.Min(alpha, 255);
                    alpha = Math.Max(alpha, 0);

                    color = Color.FromArgb((byte)Convert.ToInt32(alpha),
                                           color.R, color.G, color.B);
                }

                double offset = stop.Offset.AnimVal;

                offset /= 100;
                offset  = Math.Max(lastOffset, offset);

                gradientStops.Add(new GradientStop(color, offset));
                lastOffset = offset;
            }

            if (itemCount == 0)
            {
                gradientStops.Add(new GradientStop(Colors.Black, 0));
                gradientStops.Add(new GradientStop(Colors.Black, 1));
            }

            return(gradientStops);
        }
Пример #3
0
        protected List <GdiGradientStop> GetGradientStops(XmlNodeList stops)
        {
            int itemCount = stops.Count;

            if (itemCount == 0)
            {
                return(new List <GdiGradientStop>());
            }
            var gradientStops = new List <GdiGradientStop>(itemCount);

            double lastOffset = 0;

            for (int i = 0; i < itemCount; i++)
            {
                SvgStopElement stop = (SvgStopElement)stops.Item(i);
                if (stop == null)
                {
                    continue;
                }
                string prop  = stop.GetAttribute("stop-color");
                string style = stop.GetAttribute("style");
                Color  color = Color.Transparent; // no auto-inherited...
                if (!string.IsNullOrWhiteSpace(prop) || !string.IsNullOrWhiteSpace(style))
                {
                    GdiSvgColor svgColor = new GdiSvgColor(stop, "stop-color");
                    color = svgColor.Color;
                }
                else
                {
                    color = Color.Black; // the default color...
                    double alpha = 255;
                    string opacity;

                    opacity = stop.GetAttribute("stop-opacity"); // no auto-inherit
                    if (opacity == "inherit")                    // if explicitly defined...
                    {
                        opacity = stop.GetPropertyValue("stop-opacity");
                    }
                    if (!string.IsNullOrWhiteSpace(opacity))
                    {
                        alpha *= SvgNumber.ParseNumber(opacity);
                    }

                    alpha = Math.Min(alpha, 255);
                    alpha = Math.Max(alpha, 0);

                    color = Color.FromArgb((byte)Convert.ToInt32(alpha), color.R, color.G, color.B);
                }

                double offset = (stop.Offset == null) ? 0 : stop.Offset.AnimVal;

                offset /= 100;
                offset  = Math.Max(lastOffset, offset);

                gradientStops.Add(new GdiGradientStop(color, (float)offset));
                lastOffset = offset;
            }

            if (itemCount == 0)
            {
                gradientStops.Add(new GdiGradientStop(Color.Black, 0));
                gradientStops.Add(new GdiGradientStop(Color.Black, 1));
            }

            return(gradientStops);
        }