private Brush GetRadialGradientBrush(SvgRadialGradientElement res)
        {
            double centerX = res.Cx.AnimVal.Value;
            double centerY = res.Cy.AnimVal.Value;
            double focusX  = res.Fx.AnimVal.Value;
            double focusY  = res.Fy.AnimVal.Value;
            double radius  = res.R.AnimVal.Value;

            GradientStopCollection gradientStops = GetGradientStops(res.Stops);

            if (gradientStops == null || gradientStops.Count == 0)
            {
                return(null);
            }

            RadialGradientBrush brush = new RadialGradientBrush(gradientStops);

            brush.RadiusX        = radius;
            brush.RadiusY        = radius;
            brush.Center         = new Point(centerX, centerY);
            brush.GradientOrigin = new Point(focusX, focusY);

            if (res.SpreadMethod != null)
            {
                SvgSpreadMethod spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;

                if (spreadMethod != SvgSpreadMethod.None)
                {
                    brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
                }
            }
            if (res.GradientUnits != null)
            {
                SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;

                    if (res.Fx.AnimVal.UnitType == SvgLengthType.Percentage)
                    {
                        brush.GradientOrigin = brush.Center;
                    }
                }
            }

            MatrixTransform transform = GetTransformMatrix(res);

            if (transform != null && !transform.Matrix.IsIdentity)
            {
                brush.Transform = transform;
            }

            string colorInterpolation = res.GetPropertyValue("color-interpolation");

            if (!string.IsNullOrWhiteSpace(colorInterpolation))
            {
                if (colorInterpolation == "linearRGB")
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
            }

            return(brush);
        }
Beispiel #2
0
        public static RadialGradientBrush ConstructBrush(SvgRadialGradientElement gradient, Rect bounds, Matrix transform)
        {
            if (gradient.Stops.Count == 0)
            {
                return(null);
            }

            double centerX = gradient.Cx.AnimVal.Value;
            double centerY = gradient.Cy.AnimVal.Value;
            double focusX  = gradient.Fx.AnimVal.Value;
            double focusY  = gradient.Fy.AnimVal.Value;
            double radius  = gradient.R.AnimVal.Value;

            GradientStopCollection gradientStops = ToGradientStops(gradient.Stops);

            RadialGradientBrush brush = new RadialGradientBrush(gradientStops);

            brush.RadiusX        = radius;
            brush.RadiusY        = radius;
            brush.Center         = new Point(centerX, centerY);
            brush.GradientOrigin = new Point(focusX, focusY);

            if (gradient.SpreadMethod != null)
            {
                SvgSpreadMethod      spreadMethod = (SvgSpreadMethod)gradient.SpreadMethod.AnimVal;
                GradientSpreadMethod sm;
                if (TryGetSpreadMethod(spreadMethod, out sm))
                {
                    brush.SpreadMethod = sm;
                }
            }

            SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox;

            if (gradient.GradientUnits != null)
            {
                mappingMode = (SvgUnitType)gradient.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;
                    brush.Center.Offset(bounds.X, bounds.Y);
                    brush.GradientOrigin.Offset(bounds.X, bounds.Y);
                }
            }

            Matrix brushTransform = ToWpfMatrix(((SvgTransformList)gradient.GradientTransform.AnimVal).TotalMatrix);

            if (mappingMode == SvgUnitType.UserSpaceOnUse)
            {
                brushTransform *= transform;
            }
            if (!brushTransform.IsIdentity)
            {
                brush.Transform = new MatrixTransform(brushTransform);
            }

            string colorInterpolation = gradient.GetPropertyValue("color-interpolation");

            if (!string.IsNullOrWhiteSpace(colorInterpolation))
            {
                if (colorInterpolation == CssConstants.ValLinearRgb)
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
            }

            return(brush);
        }
Beispiel #3
0
        private PathGradientBrush GetRadialGradientBrush(SvgRadialGradientElement res, RectangleF bounds)
        {
            float fCenterX = (float)res.Cx.AnimVal.Value;
            float fCenterY = (float)res.Cy.AnimVal.Value;
            float fFocusX  = (float)res.Fx.AnimVal.Value;
            float fFocusY  = (float)res.Fy.AnimVal.Value;
            float fRadius  = (float)res.R.AnimVal.Value;

            float fEffectiveCX      = fCenterX;
            float fEffectiveCY      = fCenterY;
            float fEffectiveFX      = fFocusX;
            float fEffectiveFY      = fFocusY;
            float fEffectiveRadiusX = fRadius;
            float fEffectiveRadiusY = fRadius;

            if (res.GradientUnits.AnimVal.Equals(SvgUnitType.ObjectBoundingBox))
            {
                fEffectiveCX      = bounds.Left + fCenterX * (bounds.Width);
                fEffectiveCY      = bounds.Top + fCenterY * (bounds.Height);
                fEffectiveFX      = bounds.Left + fFocusX * (bounds.Width);
                fEffectiveFY      = bounds.Top + fFocusY * (bounds.Height);
                fEffectiveRadiusX = fRadius * bounds.Width;
                fEffectiveRadiusY = fRadius * bounds.Height;
            }

            GraphicsPath gp = new GraphicsPath();

            gp.AddEllipse(fEffectiveCX - fEffectiveRadiusX,
                          fEffectiveCY - fEffectiveRadiusY, 2 * fEffectiveRadiusX, 2 * fEffectiveRadiusY);

            PathGradientBrush brush = new PathGradientBrush(gp);

            brush.CenterPoint = new PointF(fEffectiveFX, fEffectiveFY);

            XmlNodeList stops = res.Stops;

            ColorBlend cb = new ColorBlend();

            List <Color> adjcolors    = new List <Color>();
            List <float> adjpositions = new List <float>();

            GetColorsAndPositions(stops, adjpositions, adjcolors);

            // Need to invert the colors for some bizarre reason
            adjcolors.Reverse();
            adjpositions.Reverse();
            for (int i = 0; i < adjpositions.Count; i++)
            {
                adjpositions[i] = 1 - adjpositions[i];
            }

            cb.Colors    = adjcolors.ToArray();
            cb.Positions = adjpositions.ToArray();

            brush.InterpolationColors = cb;

            //			ISvgTransformable transElm = (ISvgTransformable)res;
            //			SvgTransformList svgTList = (SvgTransformList)transElm.transform.AnimVal;
            //			brush.Transform = svgTList.matrix.matrix;

            if (res.GetPropertyValue("color-interpolation") == "linearRGB")
            {
                //GdipSetPathGradientGammaCorrection(brush, true);
            }
            else
            {
                //GdipSetPathGradientGammaCorrection(brush, false);
            }

            /*
             * How to do brush.GammaCorrection = true on a PathGradientBrush? / nikgus
             * */

            return(brush);
        }
Beispiel #4
0
        private Brush GetRadialGradientBrush(SvgRadialGradientElement res)
        {
            var refElem = res.ReferencedElement;

            double centerX = res.Cx.AnimVal.Value;
            double centerY = res.Cy.AnimVal.Value;

            // 'fx', 'fy', and 'fr' define the start circle for the radial gradient.
            double focusX = res.Fx.AnimVal.Value;
            double focusY = res.Fy.AnimVal.Value;
            double radius = res.R.AnimVal.Value;

            var lengthUnit = res.Cx.AnimVal.UnitType;

            // If attribute 'fx' is not specified, 'fx' will coincide with the presentational
            // value of 'cx' for the element whether the value for 'cx' was inherited or not.
            if (lengthUnit == SvgLengthType.Percentage)
            {
                if (!res.HasAttribute("fx") && (refElem == null || !refElem.HasAttribute("fx")))
                {
                    focusX = centerX;
                }
                else if (focusX.Equals(0.0))
                {
                    focusX = centerX;
                    if (focusX > 0 && focusX >= radius)
                    {
                        focusX = (centerX > radius) ? centerX - radius : focusX = radius;
                    }
                }
                else
                {
                    if (focusX > 0 && focusX >= radius)
                    {
                        focusX = (centerX > radius) ? centerX - radius : focusX = radius;
                    }
                }
            }

            lengthUnit = res.Cy.AnimVal.UnitType;
            // If attribute 'fy' is not specified, 'fy' will coincide with the presentational
            // value of 'cy' for the element whether the value for 'cy' was inherited or not.
            if (lengthUnit == SvgLengthType.Percentage)
            {
                if (!res.HasAttribute("fy") && (refElem == null || !refElem.HasAttribute("fy")))
                {
                    focusY = centerY;
                }
                else if (focusY.Equals(0.0))
                {
                    focusY = centerY;
                    if (focusY > 0 && focusY >= radius)
                    {
                        focusY = (centerY > radius) ? centerY - radius : focusY = radius;
                    }
                }
                else
                {
                    if (focusY > 0 && focusY >= radius)
                    {
                        focusY = (centerY > radius) ? centerY - radius : focusY = radius;
                    }
                }
            }

            GradientStopCollection gradientStops = GetGradientStops(res.Stops);

            if (gradientStops == null || gradientStops.Count == 0)
            {
                return(null);
            }

            RadialGradientBrush brush = new RadialGradientBrush(gradientStops);

            brush.RadiusX        = radius;
            brush.RadiusY        = radius;
            brush.Center         = new Point(centerX, centerY);
            brush.GradientOrigin = new Point(focusX, focusY);

            if (res.SpreadMethod != null)
            {
                SvgSpreadMethod spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal;

                if (spreadMethod != SvgSpreadMethod.None)
                {
                    brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod);
                }
            }
            if (res.GradientUnits != null)
            {
                SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal;
                if (mappingMode == SvgUnitType.ObjectBoundingBox)
                {
                    brush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
                }
                else if (mappingMode == SvgUnitType.UserSpaceOnUse)
                {
                    brush.MappingMode = BrushMappingMode.Absolute;

                    if (res.Fx.AnimVal.UnitType == SvgLengthType.Percentage)
                    {
                        brush.GradientOrigin = brush.Center;
                    }

                    _isUserSpace = true;
                }
            }

            MatrixTransform transform = GetTransformMatrix(res);

            if (transform != null && !transform.Matrix.IsIdentity)
            {
                brush.Transform = transform;
            }

            string colorInterpolation = res.GetPropertyValue("color-interpolation");

            if (!string.IsNullOrWhiteSpace(colorInterpolation))
            {
                if (string.Equals(colorInterpolation, "linearRGB", StringComparison.OrdinalIgnoreCase))
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation;
                }
                else
                {
                    brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation;
                }
            }

            return(brush);
        }