|
| 1 | +namespace AngleSharp.Css.Values |
| 2 | +{ |
| 3 | + using AngleSharp.Css.Dom; |
| 4 | + using AngleSharp.Text; |
| 5 | + using System; |
| 6 | + using System.Linq; |
| 7 | + |
| 8 | + /// <summary> |
| 9 | + /// Represents a linear gradient: |
| 10 | + /// https://drafts.csswg.org/css-images-4/#conic-gradients |
| 11 | + /// </summary> |
| 12 | + sealed class CssConicGradientValue : ICssGradientFunctionValue |
| 13 | + { |
| 14 | + #region Fields |
| 15 | + |
| 16 | + private readonly CssGradientStopValue[] _stops; |
| 17 | + private readonly ICssValue _center; |
| 18 | + private readonly ICssValue _angle; |
| 19 | + private readonly Boolean _repeating; |
| 20 | + |
| 21 | + #endregion |
| 22 | + |
| 23 | + #region ctor |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Creates a new conic gradient. |
| 27 | + /// </summary> |
| 28 | + /// <param name="angle">The angle of the conic gradient.</param> |
| 29 | + /// <param name="center">The center to use.</param> |
| 30 | + /// <param name="stops">The stops to use.</param> |
| 31 | + /// <param name="repeating">Indicates if the gradient is repeating.</param> |
| 32 | + public CssConicGradientValue(ICssValue angle, ICssValue center, CssGradientStopValue[] stops, Boolean repeating = false) |
| 33 | + { |
| 34 | + _stops = stops; |
| 35 | + _center = center; |
| 36 | + _angle = angle; |
| 37 | + _repeating = repeating; |
| 38 | + } |
| 39 | + |
| 40 | + #endregion |
| 41 | + |
| 42 | + #region Properties |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the name of the function. |
| 46 | + /// </summary> |
| 47 | + public String Name => _repeating ? FunctionNames.RepeatingConicGradient : FunctionNames.ConicGradient; |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Gets the arguments. |
| 51 | + /// </summary> |
| 52 | + public ICssValue[] Arguments |
| 53 | + { |
| 54 | + get |
| 55 | + { |
| 56 | + var args = _stops.Cast<ICssValue>().ToList(); |
| 57 | + |
| 58 | + if (_center != null) |
| 59 | + { |
| 60 | + args.Insert(0, _center); |
| 61 | + } |
| 62 | + |
| 63 | + if (_angle != null) |
| 64 | + { |
| 65 | + args.Insert(0, _angle); |
| 66 | + } |
| 67 | + |
| 68 | + return args.ToArray(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets the CSS text representation. |
| 74 | + /// </summary> |
| 75 | + public String CssText |
| 76 | + { |
| 77 | + get |
| 78 | + { |
| 79 | + var defaultAngle = _angle as Angle?; |
| 80 | + var defaultPosition = _center as Point?; |
| 81 | + var offset = (defaultAngle.HasValue ? 1 : 0) + (defaultPosition.HasValue ? 1 : 0); |
| 82 | + var args = new String[_stops.Length + offset]; |
| 83 | + |
| 84 | + if (defaultAngle.HasValue) |
| 85 | + { |
| 86 | + args[0] = $"from {defaultAngle.Value.CssText}"; |
| 87 | + } |
| 88 | + |
| 89 | + if (defaultPosition.HasValue) |
| 90 | + { |
| 91 | + args[offset - 1] = $"at {defaultPosition.Value.CssText}"; |
| 92 | + } |
| 93 | + |
| 94 | + for (var i = 0; i < _stops.Length; i++) |
| 95 | + { |
| 96 | + args[offset++] = _stops[i].CssText; |
| 97 | + } |
| 98 | + |
| 99 | + return Name.CssFunction(String.Join(", ", args)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Gets the angle of the conic gradient. |
| 105 | + /// </summary> |
| 106 | + public ICssValue Angle => _angle ?? Values.Angle.Half; |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Gets the position of the conic gradient. |
| 110 | + /// </summary> |
| 111 | + public ICssValue Center => _center ?? Point.Center; |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Gets all stops. |
| 115 | + /// </summary> |
| 116 | + public CssGradientStopValue[] Stops => _stops; |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Gets if the gradient is repeating. |
| 120 | + /// </summary> |
| 121 | + public Boolean IsRepeating => _repeating; |
| 122 | + |
| 123 | + #endregion |
| 124 | + } |
| 125 | +} |
0 commit comments