Ejemplo n.º 1
0
        private static RgbColor ParseColorFunc(string color)
        {
            var ptr = new StringPtr(color);

            ptr.AdvanceWhiteSpace();

            var s1 = ptr.Index;

            ptr.AdvanceInteger();
            if (ptr.Index == s1)
            {
                throw new ArgumentException(nameof(color));
            }
            var r = byte.Parse(color.Substring(s1, ptr.Index - s1));

            var percentage = false;

            if (ptr.Char == '%')
            {
                percentage = true;
                ++ptr;
            }

            ptr.AdvanceWhiteSpace();
            if (ptr.Char != ',')
            {
                throw new ArgumentException(nameof(color));
            }
            ++ptr;
            ptr.AdvanceWhiteSpace();

            var s2 = ptr.Index;

            ptr.AdvanceInteger();
            if (ptr.Index == s2)
            {
                throw new ArgumentException(nameof(color));
            }
            var g = byte.Parse(color.Substring(s2, ptr.Index - s2));

            if (percentage)
            {
                if (ptr.Char != '%')
                {
                    throw new ArgumentException(nameof(color));
                }
                ++ptr;
            }

            ptr.AdvanceWhiteSpace();
            if (ptr.Char != ',')
            {
                throw new ArgumentException(nameof(color));
            }
            ++ptr;
            ptr.AdvanceWhiteSpace();

            var s3 = ptr.Index;

            ptr.AdvanceInteger();
            if (ptr.Index == s3)
            {
                throw new ArgumentException(nameof(color));
            }
            var b = byte.Parse(color.Substring(s3, ptr.Index - s3));

            if (percentage)
            {
                if (ptr.Char != '%')
                {
                    throw new ArgumentException(nameof(color));
                }
                ++ptr;
            }

            ptr.AdvanceWhiteSpace();

            if (ptr.Char != ')')
            {
                throw new ArgumentException(nameof(color));
            }

            return(percentage
                                ? new RgbColor((byte)(255.0F * r / 100.0F), (byte)(255.0F * g / 100.0F), (byte)(255.0F * b / 100.0F))
                                : new RgbColor(r, g, b));
        }
Ejemplo n.º 2
0
        private static RgbColor ParseHslColorFunc(string color)
        {
            var ptr = new StringPtr(color);

            ptr.AdvanceWhiteSpace();

            var s1 = ptr.Index;
            var h  = SvgAngle.Parse(ptr);

            if (ptr.Index == s1)
            {
                throw new ArgumentException(nameof(color));
            }

            ptr.AdvanceWhiteSpace();
            if (ptr.Char != ',')
            {
                throw new ArgumentException(nameof(color));
            }
            ++ptr;
            ptr.AdvanceWhiteSpace();

            var s2 = ptr.Index;

            ptr.AdvanceInteger();
            if (ptr.Index == s2)
            {
                throw new ArgumentException(nameof(color));
            }
            var s = byte.Parse(color.Substring(s2, ptr.Index - s2));

            if (ptr.Char != '%')
            {
                throw new ArgumentException(nameof(color));
            }
            ++ptr;

            ptr.AdvanceWhiteSpace();
            if (ptr.Char != ',')
            {
                throw new ArgumentException(nameof(color));
            }
            ++ptr;
            ptr.AdvanceWhiteSpace();

            var s3 = ptr.Index;

            ptr.AdvanceInteger();
            if (ptr.Index == s3)
            {
                throw new ArgumentException(nameof(color));
            }
            var l = byte.Parse(color.Substring(s3, ptr.Index - s3));

            if (ptr.Char != '%')
            {
                throw new ArgumentException(nameof(color));
            }
            ++ptr;

            ptr.AdvanceWhiteSpace();

            if (ptr.Char != ')')
            {
                throw new ArgumentException(nameof(color));
            }

            return(HlsToRgb(h.ValueAsDegree, s / 100.0, l / 100.0));
        }