/// <summary> /// Applies a distance value equally to all the entries in the spacers list from the first color with distance at last index to the col provided /// </summary> /// <param name="lastValue">the index of the previous last color with a vlaue (or -1 for none)</param> /// <param name="spacers">All the spacer colors to add values to</param> /// <param name="col">The ultimate color to reach - it must have a distance value</param> private void ApplySplitDistanceToSpacers(int lastValue, List <PDFGradientColor> spacers, PDFGradientColor col) { double min = 0.0; if (lastValue >= 0) //first time { min = this.Colors[lastValue].Distance.Value; } double max = col.Distance.Value; double split = (max - min) / spacers.Count; for (var j = 0; j < spacers.Count; j++) { spacers[j].Distance = min + split; min += split; } spacers.Clear(); }
public static bool TryParse(string value, out PDFGradientColor color) { color = null; if (null == value) { return(false); } value = value.Trim(); PDFColor colVal = PDFColor.Transparent; double? opacity = null; double? distance = null; if (value.StartsWith("rgba(")) //We have opacity { var end = value.IndexOf(")"); if (end < 0) { return(false); } else if (end >= value.Length - 1) { if (!PDFColor.TryParseRGBA(value, out colVal, out opacity)) { return(false); } } else { var colS = value.Substring(0, end + 1); if (!PDFColor.TryParseRGBA(value, out colVal, out opacity)) { return(false); } value = value.Substring(end + 1).Trim(); if (value.EndsWith("%")) { value = value.Substring(0, value.Length - 1); } double distV; if (double.TryParse(value, out distV)) { distance = distV; } } color = new PDFGradientColor(colVal, distance, opacity); return(true); } else if (value.StartsWith("rgb(")) //We have rbg and maybe a distance { var end = value.IndexOf(")"); if (end == value.Length - 1) { if (!PDFColor.TryParse(value, out colVal)) { return(false); } } else { var colS = value.Substring(0, end); value = value.Substring(end).Trim(); if (value.EndsWith("%")) { value = value.Substring(0, value.Length - 1); } double distV; if (double.TryParse(value, out distV)) { distance = distV; } } color = new PDFGradientColor(colVal, distance, opacity); return(true); } else if (value.IndexOf(" ") > 0) //we have a named or hex color and a distance { var colS = value.Substring(0, value.IndexOf(" ")).Trim(); value = value.Substring(value.IndexOf(" ")).Trim(); if (!PDFColor.TryParse(colS, out colVal)) { return(false); } if (value.EndsWith("%")) { value = value.Substring(0, value.Length - 1); } double distV; if (double.TryParse(value, out distV)) { distance = distV; } color = new PDFGradientColor(colVal, distance, opacity); return(true); } else if (PDFColor.TryParse(value, out colVal)) //we just have a named or hex color { color = new PDFGradientColor(colVal, distance, opacity); return(true); } else //not recognised { return(false); } }
/// <summary> /// Parses a linear gradient from a string without decorations e.g. "to top right, red, green /// </summary> /// <param name="value"></param> /// <param name="linear"></param> /// <returns></returns> public static bool TryParseLinear(string value, out PDFGradientLinearDescriptor linear) { linear = null; string[] all = _splitter.Split(value); if (all.Length == 0) { return(false); } int colorStopIndex = 0; double angle; if (all[0].StartsWith("to ")) { var ga = all[0].Substring(3).Trim().Replace(" ", "_"); GradientAngle parsed; if (Enum.TryParse(ga, true, out parsed)) { angle = (double)parsed; } else { return(false); } colorStopIndex = 1; } else if (char.IsNumber(all[0], 0)) { var deg = all[0]; if (deg.EndsWith("deg")) { deg = deg.Substring(0, deg.Length - 3); } if (!double.TryParse(deg, out angle)) { return(false); } colorStopIndex = 1; } else { angle = (double)GradientAngle.Bottom; } 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); } } linear = new PDFGradientLinearDescriptor() { Angle = angle, Colors = new List <PDFGradientColor>(colors) }; return(true); }
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); }