public static double CalculateRadiusForSize(RadialSize radiusSize, double height, double width, double centreX, double centreY) { double minx, miny, maxX, maxY, radius; switch (radiusSize) { case (RadialSize.ClosestCorner): minx = Math.Min(centreX, width - centreX); miny = Math.Min(centreY, height - centreY); radius = Math.Sqrt((minx * minx) + (miny * miny)); break; case (RadialSize.FarthestCorner): maxX = Math.Max(centreX, width - centreX); maxY = Math.Max(centreY, height - centreY); radius = Math.Sqrt((maxX * maxX) + (maxY * maxY)); break; case (RadialSize.ClosestSide): minx = Math.Min(centreX, width - centreX); miny = Math.Min(centreY, height - centreY); radius = Math.Min(minx, miny); break; case (RadialSize.FarthestSide): case (RadialSize.None): default: maxX = Math.Max(centreX, width - centreX); maxY = Math.Max(centreY, height - centreY); radius = Math.Max(maxX, maxY); break; } return(radius); }
protected virtual double[] GetCoords(PDFPoint offset, PDFSize size, RadialSize radiusSize, PDFUnit?centreX, PDFUnit?centreY) { double[] all = new double[6]; var height = Math.Abs(size.Height.PointsValue); var width = size.Width.PointsValue; //Get the centre and the radius PDFPoint c = CacluateRadialCentre(centreX, centreY, height, width); double radius = CalculateRadiusForSize(radiusSize, height, width, c.X.PointsValue, c.Y.PointsValue); if (radius <= 0) { radius = 0.01; } //apply the centres based on the page location all[0] = c.X.PointsValue + offset.X.PointsValue; all[1] = offset.Y.PointsValue - c.Y.PointsValue; all[2] = 0.0; all[3] = c.X.PointsValue + offset.X.PointsValue; all[4] = offset.Y.PointsValue - c.Y.PointsValue; all[5] = radius; return(all); }
public static bool TryParseRadial(string value, out PDFGradientRadialDescriptor radial) { radial = null; string[] all = _splitter.Split(value); if (all.Length == 0) { return(false); } RadialShape shape = RadialShape.Circle; RadialSize size = RadialSize.FarthestCorner; PDFUnit? xpos = null; PDFUnit? ypos = null; int colorStopIndex = 0; if (all[0].StartsWith("circle")) { shape = RadialShape.Circle; all[0] = all[0].Substring("circle".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("ellipse")) { radial = null; return(false); //shape = RadialShape.Ellipse; //all[0] = all[0].Substring("ellipse".Length).TrimStart(); //colorStopIndex = 1; } if (all[0].StartsWith("closest-side")) { //TODO:Parse at percents size = RadialSize.ClosestSide; all[0] = all[0].Substring("closest-side".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("closest-corner")) { size = RadialSize.ClosestCorner; all[0] = all[0].Substring("closest-corner".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("farthest-side")) { size = RadialSize.FarthestSide; all[0] = all[0].Substring("farthest-side".Length).TrimStart(); colorStopIndex = 1; } else if (all[0].StartsWith("farthest-corner")) { size = RadialSize.FarthestCorner; all[0] = all[0].Substring("farthest-corner".Length).TrimStart(); colorStopIndex = 1; } //TODO: Support relative radii positions e.g. 10% 40% at .... if (all[0].StartsWith("at")) { all[0] = all[0].Substring("at".Length).TrimStart().ToLower(); var parts = all[0].Split(' '); foreach (var part in parts) { if (string.IsNullOrWhiteSpace(part)) { continue; } var item = part.Trim(); switch (item) { case ("top"): ypos = 0; break; case ("left"): xpos = 0; break; case ("bottom"): ypos = Double.MaxValue; break; case ("right"): xpos = Double.MaxValue; break; default: PDFUnit found; if (PDFUnit.TryParse(item, out found)) { if (xpos.HasValue) { ypos = found; } else { xpos = found; } } break; } } colorStopIndex = 1; } PDFGradientColor[] colors = new PDFGradientColor[all.Length - colorStopIndex]; for (int i = 0; i < colors.Length; i++) { PDFGradientColor parsed; if (PDFGradientColor.TryParse(all[i + colorStopIndex], out parsed)) { colors[i] = parsed; } else { return(false); } } radial = new PDFGradientRadialDescriptor() { Repeating = false, Shape = shape, Size = size, XCentre = xpos, YCentre = ypos, Colors = new List <PDFGradientColor>(colors) }; return(true); }
public PDFGradientRadialDescriptor(RadialShape shape, RadialSize size) : base(GradientType.Radial) { Shape = shape; Size = size; }