예제 #1
0
        private static SvgPaintServer CreateLinearGradientPaintServer(SvgElement elementToPaint, SvgLinearGradientElement linear)
        {
            var stops = ConvertStops(linear.Stops);

            if (linear.GradientTransform.Count > 0)
            {
                throw new NotImplementedException($"gradient '{linear.GradientTransform}' transformations");
            }

            var bbox   = elementToPaint.GetBBox();
            var width  = bbox?.Width ?? 0;
            var height = bbox?.Height ?? 0;

            float?GetRelativeValue(SvgLength?length)
            {
                if (length == null)
                {
                    return(null);
                }
                var lengthType = length.Value.LengthType;

                if (lengthType != SvgLengthType.Number && lengthType != SvgLengthType.Percentage)
                {
                    throw new Exception($"Expected linear gradient value to be number or percent but got '{length}'");
                }
                return(length.Value.GetAbsoluteValue(1));
            }

            var x1 = (GetRelativeValue(linear.X1) ?? 0) * width;
            var y1 = (GetRelativeValue(linear.Y1) ?? 0) * height;
            var x2 = (GetRelativeValue(linear.X2) ?? 1) * width;
            var y2 = (GetRelativeValue(linear.Y2) ?? 0) * height;

            if (Math.Abs(x1 - x2) < float.Epsilon && Math.Abs(y1 - y2) < float.Epsilon)
            {
                if (stops.Count < 1)
                {
                    throw new Exception($"Linear gradient has no stops");
                }
                var lastStop = stops[stops.Count - 1];
                return(new SvgSolidColorPaintServer(lastStop.Color, lastStop.Opacity));
            }

            return(new SvgLinearGradientPaintServer(x1, y1, x2, y2, linear.GradientUnits, stops));
        }
예제 #2
0
        private static SvgPaintServer CreateRadialGradientPaintServer(SvgElement elementToPaint, SvgRadialGradientElement radial)
        {
            var stops = ConvertStops(radial.Stops);

            if (radial.GradientTransform.Count > 0)
            {
                throw new NotImplementedException($"gradient '{radial.GradientTransform}' transformations");
            }

            var bbox   = elementToPaint.GetBBox();
            var width  = bbox?.Width ?? 0;
            var height = bbox?.Height ?? 0;

            var cx = radial.CircleX?.GetAbsoluteValue(width) ?? (width * 0.5f);
            var cy = radial.CircleY?.GetAbsoluteValue(height) ?? (height * 0.5f);
            var cr = radial.CircleRadius?.GetAbsoluteValue(width) ?? (width * 0.5f);
            var fx = radial.FocalX?.GetAbsoluteValue(width) ?? cx;
            var fy = radial.FocalY?.GetAbsoluteValue(height) ?? cy;

            return(new SvgRadialGradientPaintServer(cx, cy, cr, fx, fy, radial.GradientUnits, stops));
        }