Inheritance: SvgColor, ISvgPaint
Esempio n. 1
0
        public void TestPaintTypes()
        {
            SvgPaint paint;
            paint = new SvgPaint("url(rr)");
            Assert.AreEqual(SvgPaintType.Uri, paint.PaintType);

            paint = new SvgPaint("url(rr) #34ff23");
            Assert.AreEqual(SvgPaintType.UriRgbColor, paint.PaintType);

            paint = new SvgPaint("url(rr) #34ff23 icc-color(somename)");
            Assert.AreEqual(SvgPaintType.UriRgbColorIccColor, paint.PaintType);

            paint = new SvgPaint("url(rr) none");
            Assert.AreEqual(SvgPaintType.UriNone, paint.PaintType);

            paint = new SvgPaint("url(rr)	 currentColor");
            Assert.AreEqual(SvgPaintType.UriCurrentColor, paint.PaintType);

            paint = new SvgPaint("currentColor");
            Assert.AreEqual(SvgPaintType.CurrentColor, paint.PaintType);

            paint = new SvgPaint("none");
            Assert.AreEqual(SvgPaintType.None, paint.PaintType);

            paint = new SvgPaint("rgb(12%, 23%, 12)");
            Assert.AreEqual(SvgPaintType.RgbColor, paint.PaintType);

            paint = new SvgPaint("rgb(12%, 23%, 45) icc-color(somename)");
            Assert.AreEqual(SvgPaintType.RgbColorIccColor, paint.PaintType);
        }
Esempio n. 2
0
        public void TestSetPaint()
        {
            SvgPaint paint = new SvgPaint("none");

            paint.SetPaint(SvgPaintType.Uri, "someuri", null, null);
            Assert.AreEqual("someuri", paint.Uri);
            Assert.AreEqual("url(someuri)", paint.CssText);
            Assert.AreEqual(SvgPaintType.Uri, paint.PaintType);

            paint.SetPaint(SvgPaintType.UriCurrentColor, "someuri", null, null);
            Assert.AreEqual("someuri", paint.Uri);
            Assert.AreEqual("url(someuri) currentColor", paint.CssText);
            Assert.AreEqual(SvgPaintType.UriCurrentColor, paint.PaintType);

            paint.SetPaint(SvgPaintType.UriNone, "someuri", null, null);
            Assert.AreEqual("someuri", paint.Uri);
            Assert.AreEqual("url(someuri) none", paint.CssText);
            Assert.AreEqual(SvgPaintType.UriNone, paint.PaintType);

            paint.SetPaint(SvgPaintType.None, null, null, null);
            Assert.AreEqual("none", paint.CssText);
            Assert.AreEqual(SvgPaintType.None, paint.PaintType);

            paint.SetPaint(SvgPaintType.CurrentColor, null, null, null);
            Assert.AreEqual("currentColor", paint.CssText);
            Assert.AreEqual(SvgPaintType.CurrentColor, paint.PaintType);

            paint.SetPaint(SvgPaintType.UriRgbColor, "someuri", "#ff00ff", null);
            Assert.AreEqual("url(someuri) rgb(255,0,255)", paint.CssText);
            Assert.AreEqual(SvgPaintType.UriRgbColor, paint.PaintType);
        }
Esempio n. 3
0
        private void ParsePaint(string str)
        {
            bool hasUri          = false;
            bool hasRgb          = false;
            bool hasIcc          = false;
            bool hasNone         = false;
            bool hasCurrentColor = false;

            var comparer = StringComparison.OrdinalIgnoreCase;

            str = str.Trim();

            if (string.IsNullOrWhiteSpace(str) || str.Equals(CssConstants.ValNone, comparer) ||
                str.Equals("transparent", comparer) || str.Equals("null", comparer))
            {
                hasNone = true;
            }
            else if (str.Equals("currentColor", comparer))
            {
                base.ParseColor(str);
                hasCurrentColor = true;
            }
            else if (str.Equals("context-fill", comparer) || str.Equals("contextFill", comparer))
            {
                _paintType = SvgPaintType.ContextFill;
                return;
            }
            else if (str.Equals("context-stroke", comparer) || str.Equals("contextStroke", comparer))
            {
                _paintType = SvgPaintType.ContextStroke;
                return;
            }
            else
            {
                List <string> strList = new List <string>();

                while (!string.IsNullOrWhiteSpace(str))
                {
                    if (str.StartsWith("url(", comparer))
                    {
                        var endUri = str.IndexOf(')', 4);
                        strList.Add(str.Substring(0, endUri + 1));
                        str = str.Substring(endUri + 1).Trim();
                    }
                    else if (str.StartsWith("rgb(", comparer))
                    {
                        var leftParen = str.IndexOf(')', 4);
                        strList.Add(str.Substring(0, leftParen + 1));
                        str = str.Substring(leftParen + 1).Trim();
                    }
                    else if (str.StartsWith("rgba(", comparer))
                    {
                        var leftParen = str.IndexOf(')', 5);
                        strList.Add(str.Substring(0, leftParen + 1));
                        str = str.Substring(leftParen + 1).Trim();
                    }
                    else if (str.StartsWith("hsl(", comparer))
                    {
                        var leftParen = str.IndexOf(')', 4);
                        strList.Add(str.Substring(0, leftParen + 1));
                        str = str.Substring(leftParen + 1).Trim();
                    }
                    else if (str.StartsWith("hsla(", comparer))
                    {
                        var leftParen = str.IndexOf(')', 5);
                        strList.Add(str.Substring(0, leftParen + 1));
                        str = str.Substring(leftParen + 1).Trim();
                    }
                    else if (str.StartsWith("#", comparer)) // Otherwise try and parse as colour
                    {
                        switch (CountHexDigits(str, 1))
                        {
                        // RGB syntax variations
                        case 3:
                            strList.Add(str.Substring(0, 4));
                            str = str.Substring(4).Trim();
                            break;

                        case 6:
                            strList.Add(str.Substring(0, 7));
                            str = str.Substring(7).Trim();
                            break;

                        // RGB transparency variations
                        case 4:
                            strList.Add(str.Substring(0, 5));
                            str = str.Substring(5).Trim();
                            break;

                        case 8:
                            strList.Add(str.Substring(0, 9));
                            str = str.Substring(9).Trim();
                            break;

                        default:
                            strList.Add(str);
                            break;
                        }
                    }
                    else
                    {
                        strList.Add(str.Trim());
                        break;
                    }
                }

                if (strList.Count > 1)
                {
                    _fallback = new SvgPaint(strList[1]);

                    this.ParsePaint(strList[0]);
                    return;
                }
                else
                {
                    str = strList[0];

                    if (str.StartsWith("url(", comparer))
                    {
                        hasUri = true;
                        int endUri = str.IndexOf(")", comparer);
                        _uri = str.Substring(4, endUri - 4);
                        str  = str.Substring(endUri + 1).Trim();
                    }

                    if (str.Length > 0)
                    {
                        base.ParseColor(str);
                        hasRgb = true;
                        hasIcc = (base.ColorType == SvgColorType.RgbColorIccColor);
                    }
                }
            }

            SetPaintType(hasUri, hasRgb, hasIcc, hasNone, hasCurrentColor);
        }
Esempio n. 4
0
 public void TestSetUri()
 {
     SvgPaint paint = new SvgPaint("none");
     paint.SetUri("uri");
     Assert.AreEqual("uri", paint.Uri);
     Assert.AreEqual(SvgPaintType.Uri, paint.PaintType);
 }