plotboilerplate 1.24.0 → 1.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/README.md +14 -1
  2. package/dist/index.esm.js +433 -162
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.esm.min.js +1 -1
  5. package/dist/index.esm.min.js.map +1 -1
  6. package/dist/plotboilerplate.browser.min.js +1 -1
  7. package/dist/plotboilerplate.browser.min.js.map +1 -1
  8. package/dist/plotboilerplate.js +12312 -12041
  9. package/dist/plotboilerplate.min.js +1 -1
  10. package/package.json +9 -5
  11. package/src/cjs/Bounds.d.ts +79 -25
  12. package/src/cjs/Bounds.js +102 -29
  13. package/src/cjs/Bounds.js.map +1 -1
  14. package/src/cjs/CircleSector.d.ts +3 -0
  15. package/src/cjs/CircleSector.js +28 -1
  16. package/src/cjs/CircleSector.js.map +1 -1
  17. package/src/cjs/PlotBoilerplate.d.ts +37 -6
  18. package/src/cjs/PlotBoilerplate.js +222 -116
  19. package/src/cjs/PlotBoilerplate.js.map +1 -1
  20. package/src/cjs/Polygon.d.ts +26 -7
  21. package/src/cjs/Polygon.js +56 -10
  22. package/src/cjs/Polygon.js.map +1 -1
  23. package/src/cjs/Triangle.js +2 -2
  24. package/src/cjs/Triangle.js.map +1 -1
  25. package/src/cjs/Vertex.d.ts +15 -3
  26. package/src/cjs/Vertex.js +18 -1
  27. package/src/cjs/Vertex.js.map +1 -1
  28. package/src/cjs/drawutilssvg.d.ts +2 -1
  29. package/src/cjs/drawutilssvg.js +9 -3
  30. package/src/cjs/drawutilssvg.js.map +1 -1
  31. package/src/cjs/interfaces/core.d.ts +28 -0
  32. package/src/cjs/interfaces/core.js +2 -0
  33. package/src/cjs/interfaces/core.js.map +1 -1
  34. package/src/cjs/utils/LissajousFigure.d.ts +34 -0
  35. package/src/cjs/utils/LissajousFigure.js +100 -0
  36. package/src/cjs/utils/LissajousFigure.js.map +1 -0
  37. package/src/cjs/utils/PerlinNoise.d.ts +19 -0
  38. package/src/cjs/utils/PerlinNoise.js +352 -0
  39. package/src/cjs/utils/PerlinNoise.js.map +1 -0
  40. package/src/cjs/utils/SVGPathUtils.d.ts +22 -0
  41. package/src/cjs/utils/SVGPathUtils.js +45 -0
  42. package/src/cjs/utils/SVGPathUtils.js.map +1 -0
  43. package/src/cjs/utils/algorithms/pixelCornersToRoundPaths.d.ts +23 -0
  44. package/src/cjs/utils/algorithms/pixelCornersToRoundPaths.js +270 -0
  45. package/src/cjs/utils/algorithms/pixelCornersToRoundPaths.js.map +1 -0
  46. package/src/cjs/utils/datastructures/Color.d.ts +147 -1
  47. package/src/cjs/utils/datastructures/Color.js +385 -41
  48. package/src/cjs/utils/datastructures/Color.js.map +1 -1
  49. package/src/cjs/utils/datastructures/ColorGradient.d.ts +79 -0
  50. package/src/cjs/utils/datastructures/ColorGradient.js +171 -0
  51. package/src/cjs/utils/datastructures/ColorGradient.js.map +1 -0
  52. package/src/cjs/utils/datastructures/DataGrid2d.d.ts +6 -0
  53. package/src/cjs/utils/datastructures/DataGrid2dArrayMatrix.d.ts +88 -0
  54. package/src/cjs/utils/datastructures/DataGrid2dArrayMatrix.js +155 -0
  55. package/src/cjs/utils/datastructures/DataGrid2dArrayMatrix.js.map +1 -0
  56. package/src/cjs/utils/datastructures/DataGrid2dListAdapter.d.ts +1 -7
  57. package/src/cjs/utils/datastructures/DataGrid2dListAdapter.js +3 -3
  58. package/src/cjs/utils/datastructures/DataGrid2dListAdapter.js.map +1 -1
  59. package/src/cjs/utils/datastructures/GeometryMesh.d.ts +49 -0
  60. package/src/cjs/utils/datastructures/GeometryMesh.js +58 -0
  61. package/src/cjs/utils/datastructures/GeometryMesh.js.map +1 -0
  62. package/src/cjs/utils/detectDarkMode.js +16 -7
  63. package/src/cjs/utils/detectDarkMode.js.map +1 -1
  64. package/src/cjs/utils/dom/components/ColorGradientPicker.d.ts +246 -0
  65. package/src/cjs/utils/dom/components/ColorGradientPicker.js +667 -0
  66. package/src/cjs/utils/dom/components/ColorGradientPicker.js.map +1 -0
  67. package/src/cjs/utils/dom/components/ColorGradientPickerJSX.d.ts +129 -0
  68. package/src/cjs/utils/dom/components/ColorGradientPickerJSX.js +520 -0
  69. package/src/cjs/utils/dom/components/ColorGradientPickerJSX.js.map +1 -0
  70. package/src/cjs/utils/dom/components/ColorGradientSelector.d.ts +118 -0
  71. package/src/cjs/utils/dom/components/ColorGradientSelector.js +327 -0
  72. package/src/cjs/utils/dom/components/ColorGradientSelector.js.map +1 -0
  73. package/src/cjs/utils/dom/components/Test.d.ts +7 -0
  74. package/src/cjs/utils/dom/components/Test.js +30 -0
  75. package/src/cjs/utils/dom/components/Test.js.map +1 -0
  76. package/src/cjs/utils/dom/guiSizeToggler.js +1 -1
  77. package/src/cjs/utils/dom/guiSizeToggler.js.map +1 -1
  78. package/src/cjs/utils/helpers/BezierPathInteractionHelper.d.ts +9 -3
  79. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js +20 -13
  80. package/src/cjs/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  81. package/src/cjs/utils/helpers/CircleHelper.d.ts +10 -1
  82. package/src/cjs/utils/helpers/CircleHelper.js +13 -2
  83. package/src/cjs/utils/helpers/CircleHelper.js.map +1 -1
  84. package/src/cjs/utils/helpers/CircleSectorHelper.d.ts +2 -2
  85. package/src/cjs/utils/helpers/CircleSectorHelper.js +1 -0
  86. package/src/cjs/utils/helpers/CircleSectorHelper.js.map +1 -1
  87. package/src/cjs/utils/helpers/TriangleHelper.d.ts +57 -0
  88. package/src/cjs/utils/helpers/TriangleHelper.js +83 -0
  89. package/src/cjs/utils/helpers/TriangleHelper.js.map +1 -0
  90. package/src/cjs/utils/helpers/VEllipseHelper.d.ts +6 -4
  91. package/src/cjs/utils/helpers/VEllipseHelper.js +4 -2
  92. package/src/cjs/utils/helpers/VEllipseHelper.js.map +1 -1
  93. package/src/cjs/utils/helpers/VEllipseSectorHelper.d.ts +3 -2
  94. package/src/cjs/utils/helpers/VEllipseSectorHelper.js +1 -0
  95. package/src/cjs/utils/helpers/VEllipseSectorHelper.js.map +1 -1
  96. package/src/cjs/utils/parsers/LinearColorGradientParser.d.ts +78 -0
  97. package/src/cjs/utils/parsers/LinearColorGradientParser.js +224 -0
  98. package/src/cjs/utils/parsers/LinearColorGradientParser.js.map +1 -0
  99. package/src/cjs/utils/parsers/svg/types.d.ts +8 -8
  100. package/src/createDemoDrawables.js +1 -0
  101. package/src/esm/Bounds.d.ts +79 -25
  102. package/src/esm/Bounds.js +102 -29
  103. package/src/esm/Bounds.js.map +1 -1
  104. package/src/esm/CircleSector.d.ts +3 -0
  105. package/src/esm/CircleSector.js +28 -1
  106. package/src/esm/CircleSector.js.map +1 -1
  107. package/src/esm/PlotBoilerplate.d.ts +37 -6
  108. package/src/esm/PlotBoilerplate.js +222 -116
  109. package/src/esm/PlotBoilerplate.js.map +1 -1
  110. package/src/esm/Polygon.d.ts +26 -7
  111. package/src/esm/Polygon.js +53 -10
  112. package/src/esm/Polygon.js.map +1 -1
  113. package/src/esm/Triangle.js +2 -2
  114. package/src/esm/Triangle.js.map +1 -1
  115. package/src/esm/Vertex.d.ts +15 -3
  116. package/src/esm/Vertex.js +18 -1
  117. package/src/esm/Vertex.js.map +1 -1
  118. package/src/esm/drawutilssvg.d.ts +2 -1
  119. package/src/esm/drawutilssvg.js +9 -3
  120. package/src/esm/drawutilssvg.js.map +1 -1
  121. package/src/esm/interfaces/core.d.ts +28 -0
  122. package/src/esm/interfaces/core.js +2 -0
  123. package/src/esm/interfaces/core.js.map +1 -1
  124. package/src/esm/utils/LissajousFigure.d.ts +34 -0
  125. package/src/esm/utils/LissajousFigure.js +95 -0
  126. package/src/esm/utils/LissajousFigure.js.map +1 -0
  127. package/src/esm/utils/PerlinNoise.d.ts +19 -0
  128. package/src/esm/utils/PerlinNoise.js +346 -0
  129. package/src/esm/utils/PerlinNoise.js.map +1 -0
  130. package/src/esm/utils/SVGPathUtils.d.ts +22 -0
  131. package/src/esm/utils/SVGPathUtils.js +42 -0
  132. package/src/esm/utils/SVGPathUtils.js.map +1 -0
  133. package/src/esm/utils/algorithms/pixelCornersToRoundPaths.d.ts +23 -0
  134. package/src/esm/utils/algorithms/pixelCornersToRoundPaths.js +266 -0
  135. package/src/esm/utils/algorithms/pixelCornersToRoundPaths.js.map +1 -0
  136. package/src/esm/utils/datastructures/Color.d.ts +147 -1
  137. package/src/esm/utils/datastructures/Color.js +386 -42
  138. package/src/esm/utils/datastructures/Color.js.map +1 -1
  139. package/src/esm/utils/datastructures/ColorGradient.d.ts +79 -0
  140. package/src/esm/utils/datastructures/ColorGradient.js +166 -0
  141. package/src/esm/utils/datastructures/ColorGradient.js.map +1 -0
  142. package/src/esm/utils/datastructures/DataGrid2d.d.ts +6 -0
  143. package/src/esm/utils/datastructures/DataGrid2dArrayMatrix.d.ts +88 -0
  144. package/src/esm/utils/datastructures/DataGrid2dArrayMatrix.js +150 -0
  145. package/src/esm/utils/datastructures/DataGrid2dArrayMatrix.js.map +1 -0
  146. package/src/esm/utils/datastructures/DataGrid2dListAdapter.d.ts +1 -7
  147. package/src/esm/utils/datastructures/DataGrid2dListAdapter.js +3 -3
  148. package/src/esm/utils/datastructures/DataGrid2dListAdapter.js.map +1 -1
  149. package/src/esm/utils/datastructures/GeometryMesh.d.ts +49 -0
  150. package/src/esm/utils/datastructures/GeometryMesh.js +53 -0
  151. package/src/esm/utils/datastructures/GeometryMesh.js.map +1 -0
  152. package/src/esm/utils/detectDarkMode.js +16 -7
  153. package/src/esm/utils/detectDarkMode.js.map +1 -1
  154. package/src/esm/utils/dom/components/ColorGradientPicker.d.ts +246 -0
  155. package/src/esm/utils/dom/components/ColorGradientPicker.js +743 -0
  156. package/src/esm/utils/dom/components/ColorGradientPicker.js.map +1 -0
  157. package/src/esm/utils/dom/components/ColorGradientPickerJSX.d.ts +129 -0
  158. package/src/esm/utils/dom/components/ColorGradientPickerJSX.js +582 -0
  159. package/src/esm/utils/dom/components/ColorGradientPickerJSX.js.map +1 -0
  160. package/src/esm/utils/dom/components/ColorGradientSelector.d.ts +118 -0
  161. package/src/esm/utils/dom/components/ColorGradientSelector.js +339 -0
  162. package/src/esm/utils/dom/components/ColorGradientSelector.js.map +1 -0
  163. package/src/esm/utils/dom/components/Test.d.ts +7 -0
  164. package/src/esm/utils/dom/components/Test.js +26 -0
  165. package/src/esm/utils/dom/components/Test.js.map +1 -0
  166. package/src/esm/utils/dom/guiSizeToggler.js +1 -1
  167. package/src/esm/utils/dom/guiSizeToggler.js.map +1 -1
  168. package/src/esm/utils/helpers/BezierPathInteractionHelper.d.ts +9 -3
  169. package/src/esm/utils/helpers/BezierPathInteractionHelper.js +20 -13
  170. package/src/esm/utils/helpers/BezierPathInteractionHelper.js.map +1 -1
  171. package/src/esm/utils/helpers/CircleHelper.d.ts +10 -1
  172. package/src/esm/utils/helpers/CircleHelper.js +13 -2
  173. package/src/esm/utils/helpers/CircleHelper.js.map +1 -1
  174. package/src/esm/utils/helpers/CircleSectorHelper.d.ts +2 -2
  175. package/src/esm/utils/helpers/CircleSectorHelper.js +1 -0
  176. package/src/esm/utils/helpers/CircleSectorHelper.js.map +1 -1
  177. package/src/esm/utils/helpers/TriangleHelper.d.ts +57 -0
  178. package/src/esm/utils/helpers/TriangleHelper.js +78 -0
  179. package/src/esm/utils/helpers/TriangleHelper.js.map +1 -0
  180. package/src/esm/utils/helpers/VEllipseHelper.d.ts +6 -4
  181. package/src/esm/utils/helpers/VEllipseHelper.js +4 -2
  182. package/src/esm/utils/helpers/VEllipseHelper.js.map +1 -1
  183. package/src/esm/utils/helpers/VEllipseSectorHelper.d.ts +3 -2
  184. package/src/esm/utils/helpers/VEllipseSectorHelper.js +1 -0
  185. package/src/esm/utils/helpers/VEllipseSectorHelper.js.map +1 -1
  186. package/src/esm/utils/parsers/LinearColorGradientParser.d.ts +78 -0
  187. package/src/esm/utils/parsers/LinearColorGradientParser.js +217 -0
  188. package/src/esm/utils/parsers/LinearColorGradientParser.js.map +1 -0
  189. package/src/esm/utils/parsers/svg/types.d.ts +8 -8
  190. package/src/external.d.ts +9 -0
  191. package/src/ts/Bounds.ts +107 -22
  192. package/src/ts/CircleSector.ts +39 -6
  193. package/src/ts/PlotBoilerplate.ts +233 -105
  194. package/src/ts/Polygon.ts +68 -14
  195. package/src/ts/Triangle.ts +2 -2
  196. package/src/ts/Vertex.ts +21 -3
  197. package/src/ts/drawutilssvg.ts +8 -3
  198. package/src/ts/interfaces/core.ts +31 -0
  199. package/src/ts/utils/LissajousFigure.ts +110 -0
  200. package/src/ts/utils/PerlinNoise.ts +368 -0
  201. package/src/ts/utils/SVGPathUtils.ts +55 -0
  202. package/src/ts/utils/algorithms/pixelCornersToRoundPaths.ts +379 -0
  203. package/src/ts/utils/datastructures/Color.ts +423 -44
  204. package/src/ts/utils/datastructures/ColorGradient.ts +196 -0
  205. package/src/ts/utils/datastructures/DataGrid2d.ts +7 -0
  206. package/src/ts/utils/datastructures/DataGrid2dArrayMatrix.ts +182 -0
  207. package/src/ts/utils/datastructures/DataGrid2dListAdapter.ts +4 -11
  208. package/src/ts/utils/datastructures/GeometryMesh.ts +79 -0
  209. package/src/ts/utils/detectDarkMode.ts +17 -8
  210. package/src/ts/utils/dom/components/ColorGradientPicker.tsx +889 -0
  211. package/src/ts/utils/dom/components/ColorGradientSelector.tsx +452 -0
  212. package/src/ts/utils/dom/components/Test.tsx +44 -0
  213. package/src/ts/utils/dom/components/_ColorGradientPicker_raw.ts_ +392 -0
  214. package/src/ts/utils/dom/guiSizeToggler.ts +1 -1
  215. package/src/ts/utils/helpers/BezierPathInteractionHelper.ts +25 -15
  216. package/src/ts/utils/helpers/CircleHelper.ts +17 -3
  217. package/src/ts/utils/helpers/CircleSectorHelper.ts +4 -2
  218. package/src/ts/utils/helpers/TriangleHelper.ts +102 -0
  219. package/src/ts/utils/helpers/VEllipseHelper.ts +6 -4
  220. package/src/ts/utils/helpers/VEllipseSectorHelper.ts +3 -2
  221. package/src/ts/utils/parsers/LinearColorGradientParser.ts +297 -0
  222. package/src/ts/utils/parsers/svg/types.ts +8 -8
@@ -5,16 +5,18 @@
5
5
  * @date 2025-03-31
6
6
  * @modified 2025-04-02 Adding `VEllipseHelper.drawHandleLines()`.
7
7
  * @modified 2025-04-09 Adding `VEllipseHelper.destroy()`.
8
- * @version 1.0.0
8
+ * @modified 2025-05-05 Fixed a typo in the `VEllipseHelper.destroy` method (was named `detroy`).
9
+ * @modified 2025-05-05 `VEllipseHelper` is now implementing `IShapeInteractionHelper`.
10
+ * @version 1.0.1
9
11
  */
10
12
 
11
13
  import { Line } from "../../Line";
12
14
  import { VEllipse } from "../../VEllipse";
13
15
  import { Vertex } from "../../Vertex";
14
16
  import { VertEvent, VertListener } from "../../VertexListeners";
15
- import { DrawLib } from "../../interfaces";
17
+ import { DrawLib, IShapeInteractionHelper } from "../../interfaces";
16
18
 
17
- export class VEllipseHelper {
19
+ export class VEllipseHelper implements IShapeInteractionHelper {
18
20
  private readonly ellipse: VEllipse;
19
21
  private readonly rotationControlPoint: Vertex;
20
22
 
@@ -106,7 +108,7 @@ export class VEllipseHelper {
106
108
  );
107
109
  }
108
110
 
109
- detroy(): void {
111
+ destroy(): void {
110
112
  this.ellipse.center.listeners.removeDragListener(this._centerPointHandler);
111
113
  this.rotationControlPoint.listeners.removeDragListener(this._rotationPointHandler);
112
114
  }
@@ -6,6 +6,7 @@
6
6
  * @modified 2025-04-02 Adding `VEllipseSectorHelper.drawHandleLines`.
7
7
  * @modified 2025-04-07 Modifying the calculation of `startAngle` and `endAngle` from the rotation control point: wrapping result into [0,TWO_PI).
8
8
  * @modified 2025-04-09 Adding the `VEllipseSectorHelper.destroy` method.
9
+ * @modified 2025-05-05 `VEllipseSectorHelper` is now implementing `IShapeInteractionHelper`.
9
10
  * @version 1.1.0
10
11
  */
11
12
 
@@ -14,9 +15,9 @@ import { VEllipseSector } from "../../VEllipseSector";
14
15
  import { Vertex } from "../../Vertex";
15
16
  import { VertEvent, VertListener } from "../../VertexListeners";
16
17
  import { geomutils } from "../../geomutils";
17
- import { DrawLib } from "../../interfaces";
18
+ import { DrawLib, IShapeInteractionHelper } from "../../interfaces";
18
19
 
19
- export class VEllipseSectorHelper {
20
+ export class VEllipseSectorHelper implements IShapeInteractionHelper {
20
21
  private sector: VEllipseSector;
21
22
  private startAngleControlPoint: Vertex;
22
23
  private endAngleControlPoint: Vertex;
@@ -0,0 +1,297 @@
1
+ /**
2
+ * A CSS gradient string parser.
3
+ *
4
+ * Source:
5
+ * https://stackoverflow.com/questions/20215440/parse-css-gradient-rule-with-javascript-regex
6
+ * Thanks to Dean Taylor
7
+ *
8
+ * @date 2025-09-04
9
+ * @OriginalAuthor Dean Taylor
10
+ * @RefactoredBy Ikaros Kappler, ported to Typescript.
11
+ * @version 1.0.0
12
+ */
13
+
14
+ import { Color } from "../datastructures/Color";
15
+ import { ColorGradient, ColorGradientItem, ColorGradientRotation } from "../datastructures/ColorGradient";
16
+
17
+ /**
18
+ * A wrapper type for the regular expressions used to parsed gradient strings.
19
+ */
20
+ export type ColorGradientParserRegExpLib = {
21
+ gradientSearch: RegExp;
22
+ colorStopSearch: RegExp;
23
+ };
24
+
25
+ /**
26
+ * The raw parse result.
27
+ */
28
+ export type ColorGradientParseResult = {
29
+ original: string;
30
+ line: string;
31
+ angle: string;
32
+ colorStopList: ColorGradientStopResult[];
33
+ sideCorner: string;
34
+ gradientType?: string;
35
+ parseWarning?: boolean;
36
+ };
37
+
38
+ /**
39
+ * The position string can have two formats: as percent or as pixels.
40
+ * For conversion to LinearColorGradient only relative (percent) is supported.
41
+ */
42
+ type PositionString = `${number}px` | `${number}%`;
43
+
44
+ /**
45
+ * A sub type of the result representing pairs of (color,position) elements.
46
+ */
47
+ type ColorGradientStopResult = {
48
+ color: string;
49
+ position: PositionString; // '56.5%' or '20px'
50
+ };
51
+
52
+ /**
53
+ * This must convert positioning strings to ratio numbers (in [0..1]).
54
+ * Must throw error if not parsable.
55
+ */
56
+ export type PositionToRatioConverter = (positionString: PositionString) => number;
57
+
58
+ /**
59
+ * The default implementation of `PositionToRatioConverter`.
60
+ * @param positionString
61
+ * @returns
62
+ */
63
+ export const DefaultPositionConverter: PositionToRatioConverter = (positionString: PositionString): number => {
64
+ if (typeof positionString === "undefined") {
65
+ throw new Error("Cannot parse positioning string `null`.");
66
+ }
67
+ const tmp: string = positionString.trim();
68
+ if (tmp.length === 0) {
69
+ throw new Error('Cannot parse empty positioning string "".');
70
+ }
71
+ if (!tmp.endsWith("%")) {
72
+ throw new Error(`Cannot parse positioning string, must end with '%': '${tmp}'`);
73
+ }
74
+ return Number.parseFloat(tmp) / 100.0;
75
+ };
76
+
77
+ /**
78
+ * Utility combine multiple regular expressions.
79
+ *
80
+ * Source:
81
+ * https://stackoverflow.com/questions/20215440/parse-css-gradient-rule-with-javascript-regex
82
+ * Thanks to Dean Taylor
83
+ *
84
+ * @param {Array<RegExp | string>} regexpList List of regular expressions or strings.
85
+ * @param {string} flags Normal RegExp flags.
86
+ */
87
+ const __combineRegExp = (regexpList: Array<RegExp | string>, flags: string): RegExp => {
88
+ var source: string = "";
89
+ for (var i = 0; i < regexpList.length; i++) {
90
+ const regex = regexpList[i];
91
+ if (typeof regex === "string") {
92
+ source += regex;
93
+ } else {
94
+ source += regex.source;
95
+ }
96
+ }
97
+ return new RegExp(source, flags);
98
+ };
99
+
100
+ /**
101
+ * Generate the required regular expressions once.
102
+ *
103
+ * Regular Expressions are easier to manage this way and can be well described.
104
+ *
105
+ * @result {object} Object containing regular expressions.
106
+ */
107
+ const __generateDefaultRegExpLib = (): ColorGradientParserRegExpLib => {
108
+ // Note any variables with "Capture" in name include capturing bracket set(s).
109
+ const searchFlags = "gi", // ignore case for angles, "rgb" etc
110
+ rAngle = /(?:[+-]?\d*\.?\d+)(?:deg|grad|rad|turn)/, // Angle +ive, -ive and angle types
111
+ rSideCornerCapture = /to\s+((?:(?:left|right)(?:\s+(?:top|bottom))?))/, // optional 2nd part
112
+ rComma = /\s*,\s*/, // Allow space around comma.
113
+ rColorHex = /\#(?:[a-f0-9]{6}|[a-f0-9]{3})/, // 3 or 6 character form
114
+ rDigits3 = /\(\s*(?:\d{1,3}\s*,\s*){2}\d{1,3}\s*\)/, // "(1, 2, 3)"
115
+ rDigits4 = /\(\s*(?:\d{1,3}\s*,\s*){2}\d{1,3}\s*,\s*\d*\.?\d+\)/, // "(1, 2, 3, 4)"
116
+ rValue = /(?:[+-]?\d*\.?\d+)(?:%|[a-z]+)?/, // ".9", "-5px", "100%".
117
+ rKeyword = /[_a-z-][_a-z0-9-]*/, // "red", "transparent", "border-collapse".
118
+ rColor = __combineRegExp(
119
+ ["(?:", rColorHex, "|", "(?:rgb|hsl)", rDigits3, "|", "(?:rgba|hsla)", rDigits4, "|", rKeyword, ")"],
120
+ ""
121
+ ),
122
+ rColorStop = __combineRegExp([rColor, "(?:\\s+", rValue, "(?:\\s+", rValue, ")?)?"], ""), // Single Color Stop, optional %, optional length.
123
+ rColorStopList = __combineRegExp(["(?:", rColorStop, rComma, ")*", rColorStop], ""), // List of color stops min 1.
124
+ rLineCapture = __combineRegExp(["(?:(", rAngle, ")|", rSideCornerCapture, ")"], ""), // Angle or SideCorner
125
+ rGradientSearch = __combineRegExp(["(?:(", rLineCapture, ")", rComma, ")?(", rColorStopList, ")"], searchFlags), // Capture 1:"line", 2:"angle" (optional), 3:"side corner" (optional) and 4:"stop list".
126
+ rColorStopSearch = __combineRegExp(
127
+ ["\\s*(", rColor, ")", "(?:\\s+", "(", rValue, "))?", "(?:", rComma, "\\s*)?"],
128
+ searchFlags
129
+ ); // Capture 1:"color" and 2:"position" (optional).
130
+
131
+ return {
132
+ gradientSearch: rGradientSearch,
133
+ colorStopSearch: rColorStopSearch
134
+ };
135
+ };
136
+
137
+ /**
138
+ * Actually parse the input gradient parameters string into an object for reusability.
139
+ *
140
+ *
141
+ * @note Really this only supports the standard syntax not historical versions, see MDN for details
142
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient
143
+ *
144
+ * @param {ColorGradientParserRegExpLib} regExpLib
145
+ * @param {string} input Input string in the form "to right bottom, #FF0 0%, red 20px, rgb(0, 0, 255) 100%"
146
+ * @returns {object|undefined} Object containing break down of input string including array of stop points.
147
+ */
148
+ export var __parseGradient = (regExpLib: ColorGradientParserRegExpLib, input: string): ColorGradientParseResult => {
149
+ var result: ColorGradientParseResult;
150
+ var matchGradient: RegExpExecArray, matchColorStop: string[], stopResult: ColorGradientStopResult;
151
+
152
+ // reset search position, because we reuse regex.
153
+ regExpLib.gradientSearch.lastIndex = 0;
154
+
155
+ matchGradient = regExpLib.gradientSearch.exec(input);
156
+ if (matchGradient !== null) {
157
+ result = {
158
+ original: matchGradient[0],
159
+ colorStopList: [],
160
+ line: null,
161
+ angle: null,
162
+ sideCorner: null
163
+ };
164
+
165
+ // Line (Angle or Side-Corner).
166
+ if (!!matchGradient[1]) {
167
+ result.line = matchGradient[1];
168
+ }
169
+ // Angle or undefined if side-corner.
170
+ if (!!matchGradient[2]) {
171
+ result.angle = matchGradient[2];
172
+ }
173
+ // Side-corner or undefined if angle.
174
+ if (!!matchGradient[3]) {
175
+ result.sideCorner = matchGradient[3];
176
+ }
177
+
178
+ // reset search position, because we reuse regex.
179
+ regExpLib.colorStopSearch.lastIndex = 0;
180
+
181
+ // Loop though all the color-stops.
182
+ matchColorStop = regExpLib.colorStopSearch.exec(matchGradient[4]);
183
+ while (matchColorStop !== null) {
184
+ stopResult = {
185
+ color: matchColorStop[1],
186
+ position: null
187
+ };
188
+
189
+ // Position (optional).
190
+ if (!!matchColorStop[2]) {
191
+ stopResult.position = matchColorStop[2] as PositionString;
192
+ }
193
+ result.colorStopList.push(stopResult);
194
+
195
+ // Continue searching from previous position.
196
+ matchColorStop = regExpLib.colorStopSearch.exec(matchGradient[4]);
197
+ }
198
+ }
199
+
200
+ // Can be undefined if match not found.
201
+ return result;
202
+ };
203
+
204
+ /**
205
+ * The actual parser class.
206
+ */
207
+ export class LinearColorGradientParser {
208
+ private readonly regExpLib: ColorGradientParserRegExpLib;
209
+
210
+ constructor(regExpLib?: ColorGradientParserRegExpLib) {
211
+ this.regExpLib = regExpLib || __generateDefaultRegExpLib();
212
+ }
213
+
214
+ public parse(input: string, positionConverter?: PositionToRatioConverter): ColorGradient {
215
+ const result: ColorGradientParseResult = this.parseRaw(input);
216
+
217
+ return LinearColorGradientParser.parseResultToColorGradient(result, positionConverter);
218
+ }
219
+
220
+ public parseRaw(input: string): ColorGradientParseResult {
221
+ var result: ColorGradientParseResult;
222
+ const rGradientEnclosedInBrackets: RegExp = /.*gradient\s*\(((?:\([^\)]*\)|[^\)\(]*)*)\)/; // Captures inside brackets - max one additional inner set.
223
+ const match: RegExpExecArray = rGradientEnclosedInBrackets.exec(input);
224
+
225
+ const bracketPos = input.indexOf("(");
226
+ const gradientType = bracketPos === -1 ? null : input.substring(0, bracketPos).toLowerCase();
227
+
228
+ if (match !== null) {
229
+ // Get the parameters for the gradient
230
+ result = __parseGradient(this.regExpLib, match[1]);
231
+ result.gradientType = gradientType;
232
+ if (result.original.trim() !== match[1].trim()) {
233
+ // Did not match the input exactly - possible parsing error.
234
+ result.parseWarning = true;
235
+ }
236
+ } else {
237
+ // result = new Error("Failed to find gradient");
238
+ throw new Error("Failed to find gradient");
239
+ }
240
+
241
+ return result;
242
+ }
243
+
244
+ public static parseResultToColorGradient(
245
+ result: ColorGradientParseResult,
246
+ positionConverter?: PositionToRatioConverter
247
+ ): ColorGradient {
248
+ if (!result.gradientType || result.gradientType.toLowerCase() != "linear-gradient") {
249
+ throw new Error(`Cannot create linear gradient from type '${result.gradientType}'.`);
250
+ }
251
+
252
+ if (!result.colorStopList || result.colorStopList.length <= 1) {
253
+ throw new Error(
254
+ `Cannot create linear gradient from color stop list of length '${result.colorStopList.length}'. Too few elements.`
255
+ );
256
+ }
257
+
258
+ const colorStops: Array<ColorGradientItem> = [];
259
+ const converter: PositionToRatioConverter = positionConverter ?? DefaultPositionConverter;
260
+ for (var i = 0; i < result.colorStopList.length; i++) {
261
+ const stopListItem: ColorGradientStopResult = result.colorStopList[i];
262
+ console.log(stopListItem);
263
+ const color = Color.parse(stopListItem.color);
264
+
265
+ // var position: number = LinearColorGradientParser.parsePosition(stopListItem.position);
266
+ var ratio: number;
267
+ if (typeof stopListItem.position === "undefined") {
268
+ // Try to auto-fill undefined positions by their index in the list.
269
+ ratio = i / (result.colorStopList.length - 1);
270
+ } else {
271
+ ratio = converter(stopListItem.position);
272
+ }
273
+ colorStops.push({ color: color, ratio: ratio });
274
+ }
275
+
276
+ return new ColorGradient(colorStops, result.line as ColorGradientRotation);
277
+ }
278
+ } // END class
279
+
280
+ // var test_this_one = function (regExpLib, input) {
281
+ // var result,
282
+ // rGradientEnclosedInBrackets = /.*gradient\s*\(((?:\([^\)]*\)|[^\)\(]*)*)\)/, // Captures inside brackets - max one additional inner set.
283
+ // match = rGradientEnclosedInBrackets.exec(input);
284
+
285
+ // if (match !== null) {
286
+ // // Get the parameters for the gradient
287
+ // result = parseGradient(regExpLib, match[1]);
288
+ // if (result.original.trim() !== match[1].trim()) {
289
+ // // Did not match the input exactly - possible parsing error.
290
+ // result.parseWarning = true;
291
+ // }
292
+ // } else {
293
+ // result = "Failed to find gradient";
294
+ // }
295
+
296
+ // return result;
297
+ // };
@@ -17,7 +17,7 @@ export type NumericString = `${number}`;
17
17
  export type BooleanString = "0" | "1" | `${boolean}`;
18
18
 
19
19
  // Example: 'M', -10, -7.5,
20
- export type SVGPathMoveToCommand = ["M" | "m", NumericString, NumericString];
20
+ export type SVGPathMoveToCommand = ["M" | "m", NumericString | number, NumericString | number];
21
21
  // Example: 'L', 0, -10,
22
22
  export type SVGPathLineToCommand = ["L" | "l", NumericString, NumericString, ...Array<NumericString>];
23
23
  // Example: 'V', -10,
@@ -42,13 +42,13 @@ export type SVGPathQuadraticCurveToCommand =
42
42
  // Example: 'C', -5, -15, 10, -15, 5, -10,
43
43
  export type SVGPathCubicCurveToCommand = [
44
44
  "C" | "c",
45
- NumericString,
46
- NumericString,
47
- NumericString,
48
- NumericString,
49
- NumericString,
50
- NumericString,
51
- ...Array<NumericString>
45
+ NumericString | number,
46
+ NumericString | number,
47
+ NumericString | number,
48
+ NumericString | number,
49
+ NumericString | number,
50
+ NumericString | number,
51
+ ...Array<NumericString | number>
52
52
  ];
53
53
  // Example: 'S', 15, 0, 10, 0,
54
54
  export type SVGPathShorthandCubicCurveToCommand = [