jmchart 1.2.18 → 1.2.19

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.
@@ -152,11 +152,13 @@ define(['exports'], (function (exports) { 'use strict';
152
152
  darkseagreen: "#8fbc8f",
153
153
  darkslateblue: "#483d8b",
154
154
  darkslategray: "#2f4f4f",
155
+ darkslategrey: "#2f4f4f",
155
156
  darkturquoise: "#00ced1",
156
157
  darkviolet: "#9400d3",
157
158
  deeppink: "#ff1493",
158
159
  deepskyblue: "#00bfff",
159
160
  dimgray: "#696969",
161
+ dimgrey: "#696969",
160
162
  dodgerblue: "#1e90ff",
161
163
  firebrick: "#b22222",
162
164
  floralwhite: "#fffaf0",
@@ -191,6 +193,7 @@ define(['exports'], (function (exports) { 'use strict';
191
193
  lightseagreen: "#20b2aa",
192
194
  lightskyblue: "#87cefa",
193
195
  lightslategray: "#778899",
196
+ lightslategrey: "#778899",
194
197
  lightsteelblue: "#b0c4de",
195
198
  lightyellow: "#ffffe0",
196
199
  lime: "#00ff00",
@@ -231,6 +234,7 @@ define(['exports'], (function (exports) { 'use strict';
231
234
  powderblue: "#b0e0e6",
232
235
  purple: "#800080",
233
236
  red: "#ff0000",
237
+ rebeccapurple: "#663399",
234
238
  rosybrown: "#bc8f8f",
235
239
  royalblue: "#4169e1",
236
240
  saddlebrown: "#8b4513",
@@ -243,6 +247,7 @@ define(['exports'], (function (exports) { 'use strict';
243
247
  skyblue: "#87ceeb",
244
248
  slateblue: "#6a5acd",
245
249
  slategray: "#708090",
250
+ slategrey: "#708090",
246
251
  snow: "#fffafa",
247
252
  springgreen: "#00ff7f",
248
253
  steelblue: "#4682b4",
@@ -257,7 +262,37 @@ define(['exports'], (function (exports) { 'use strict';
257
262
  whitesmoke: "#f5f5f5",
258
263
  yellow: "#ffff00",
259
264
  yellowgreen: "#9acd32",
260
- transparent: "rgba(0,0,0,0)"
265
+ transparent: "rgba(0,0,0,0)",
266
+ // grey 别名(已有 darkslategrey/lightslategrey/slategrey/dimgrey)
267
+ // 以下为 CSS 系统颜色
268
+ activeborder: "#bfcaca",
269
+ activecaption: "#000080",
270
+ appworkspace: "#ababab",
271
+ background: "#636363",
272
+ buttonface: "#c0c0c0",
273
+ buttonhighlight: "#dedede",
274
+ buttonshadow: "#808080",
275
+ buttontext: "#000000",
276
+ captiontext: "#000000",
277
+ graytext: "#808080",
278
+ highlight: "#b3d4fc",
279
+ highlighttext: "#000000",
280
+ inactiveborder: "#d4d0c8",
281
+ inactivecaption: "#bfbbb0",
282
+ inactivecaptiontext: "#545454",
283
+ infobackground: "#fbfcc5",
284
+ infotext: "#000000",
285
+ menu: "#c0c0c0",
286
+ menutext: "#000000",
287
+ scrollbar: "#c0c0c0",
288
+ threeddarkshadow: "#696969",
289
+ threedface: "#c0c0c0",
290
+ threedhighlight: "#dfdfdf",
291
+ threedlightshadow: "#dcdcdc",
292
+ threedshadow: "#808080",
293
+ window: "#ffffff",
294
+ windowframe: "#646464",
295
+ windowtext: "#000000"
261
296
  };
262
297
 
263
298
  /**
@@ -1093,28 +1128,65 @@ define(['exports'], (function (exports) { 'use strict';
1093
1128
  d = location.radius * 2;
1094
1129
  }
1095
1130
  if(!d) {
1096
- d = Math.min(location.width,location.height);
1131
+ d = Math.min(location.width || 0, location.height || 0);
1097
1132
  }
1133
+ if(d <= 0) {
1134
+ d = Math.max(bounds.width || 0, bounds.height || 0, 100);
1135
+ }
1136
+
1137
+ const width = bounds.width || d;
1138
+ const height = bounds.height || d;
1098
1139
 
1099
- //let offsetLine = 1;//渐变长度或半径
1100
- //处理百分比参数
1101
1140
  if(jmUtils.checkPercent(x1)) {
1102
- x1 = jmUtils.percentToNumber(x1) * (bounds.width || d);
1141
+ x1 = jmUtils.percentToNumber(x1) * width;
1142
+ }
1143
+ else if(typeof x1 === 'number' && x1 >= 0 && x1 <= 1) {
1144
+ x1 = x1 * width;
1145
+ }
1146
+ else if(typeof x1 === 'string') {
1147
+ const num = parseFloat(x1);
1148
+ if(!isNaN(num)) x1 = num;
1103
1149
  }
1104
1150
  if(jmUtils.checkPercent(x2)) {
1105
- x2 = jmUtils.percentToNumber(x2) * (bounds.width || d);
1151
+ x2 = jmUtils.percentToNumber(x2) * width;
1152
+ }
1153
+ else if(typeof x2 === 'number' && x2 >= 0 && x2 <= 1) {
1154
+ x2 = x2 * width;
1155
+ }
1156
+ else if(typeof x2 === 'string') {
1157
+ const num = parseFloat(x2);
1158
+ if(!isNaN(num)) x2 = num;
1106
1159
  }
1107
1160
  if(jmUtils.checkPercent(y1)) {
1108
- y1 = jmUtils.percentToNumber(y1) * (bounds.height || d);
1161
+ y1 = jmUtils.percentToNumber(y1) * height;
1162
+ }
1163
+ else if(typeof y1 === 'number' && y1 >= 0 && y1 <= 1) {
1164
+ y1 = y1 * height;
1165
+ }
1166
+ else if(typeof y1 === 'string') {
1167
+ const num = parseFloat(y1);
1168
+ if(!isNaN(num)) y1 = num;
1109
1169
  }
1110
1170
  if(jmUtils.checkPercent(y2)) {
1111
- y2 = jmUtils.percentToNumber(y2) * (bounds.height || d);
1112
- }
1171
+ y2 = jmUtils.percentToNumber(y2) * height;
1172
+ }
1173
+ else if(typeof y2 === 'number' && y2 >= 0 && y2 <= 1) {
1174
+ y2 = y2 * height;
1175
+ }
1176
+ else if(typeof y2 === 'string') {
1177
+ const num = parseFloat(y2);
1178
+ if(!isNaN(num)) y2 = num;
1179
+ }
1180
+
1181
+ x1 = Number(x1) || 0;
1182
+ y1 = Number(y1) || 0;
1183
+ x2 = Number(x2) || 0;
1184
+ y2 = Number(y2) || 0;
1113
1185
 
1114
- let sx1 = Number(x1) + bounds.left;
1115
- let sy1 = Number(y1) + bounds.top;
1116
- let sx2 = Number(x2) + bounds.left;
1117
- let sy2 = Number(y2) + bounds.top;
1186
+ let sx1 = x1 + (bounds.left || 0);
1187
+ let sy1 = y1 + (bounds.top || 0);
1188
+ let sx2 = x2 + (bounds.left || 0);
1189
+ let sy2 = y2 + (bounds.top || 0);
1118
1190
  if(this.type === 'linear') {
1119
1191
  if(control.mode === 'webgl' && control.webglControl) {
1120
1192
  // WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
@@ -1128,24 +1200,47 @@ define(['exports'], (function (exports) { 'use strict';
1128
1200
  else if(this.type === 'radial') {
1129
1201
  let r1 = this.r1||0;
1130
1202
  let r2 = this.r2;
1203
+
1204
+ if(d <= 0) {
1205
+ d = Math.max(bounds.width || 0, bounds.height || 0, 1);
1206
+ }
1207
+
1131
1208
  if(jmUtils.checkPercent(r1)) {
1132
1209
  r1 = jmUtils.percentToNumber(r1);
1133
1210
  r1 = d * r1;
1134
1211
  }
1212
+ else if(typeof r1 === 'number' && r1 >= 0 && r1 <= 1) {
1213
+ r1 = r1 * d;
1214
+ }
1215
+ else if(typeof r1 === 'string') {
1216
+ r1 = parseFloat(r1) || 0;
1217
+ }
1218
+
1135
1219
  if(jmUtils.checkPercent(r2)) {
1136
1220
  r2 = jmUtils.percentToNumber(r2);
1137
1221
  r2 = d * r2;
1138
1222
  }
1223
+ else if(typeof r2 === 'number' && r2 >= 0 && r2 <= 1) {
1224
+ r2 = r2 * d;
1225
+ }
1226
+ else if(typeof r2 === 'string') {
1227
+ r2 = parseFloat(r2);
1228
+ }
1229
+
1230
+ if(r2 === undefined || r2 === null || isNaN(Number(r2)) || Number(r2) <= 0) {
1231
+ r2 = d / 2;
1232
+ }
1233
+
1234
+ r1 = Number(r1) || 0;
1235
+ r2 = Number(r2) || d / 2;
1236
+
1139
1237
  if(control.mode === 'webgl' && control.webglControl) {
1140
- // WebGL 着色器中 v_text_coord 是绝对坐标,需要传递绝对坐标
1141
1238
  gradient = control.webglControl.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2, bounds);
1142
1239
  gradient.key = this.toString();
1143
1240
  }
1144
- //offsetLine = Math.abs(r2 - r1);//二圆半径差
1145
1241
  else if(context.createRadialGradient) {
1146
1242
  gradient = context.createRadialGradient(sx1, sy1, r1, sx2, sy2, r2);
1147
1243
  }
1148
- //小程序的接口特殊
1149
1244
  else if(context.createCircularGradient) {
1150
1245
  gradient = context.createCircularGradient(sx1, sy1, r2);
1151
1246
  }
@@ -1168,67 +1263,539 @@ define(['exports'], (function (exports) { 'use strict';
1168
1263
  }
1169
1264
 
1170
1265
  /**
1171
- * 变换为字条串格式
1172
- * linear-gradient(x1 y1 x2 y2, color1 step, color2 step, ...); //radial-gradient(x1 y1 r1 x2 y2 r2, color1 step,color2 step, ...);
1173
- * linear-gradient线性渐变,x1 y1表示起点,x2 y2表示结束点,color表颜色,step为当前颜色偏移
1174
- * radial-gradient径向渐变,x1 y1 r1分别表示内圆中心和半径,x2 y2 r2为结束圆 中心和半径,颜色例似线性渐变 step为0-1之间
1266
+ * 解析渐变字符串
1267
+ * 支持的格式:
1268
+ * - linear-gradient(180deg, #8b5cf6 0%, #6366f1 50%, #4f46e5 100%)
1269
+ * - linear-gradient(to top, red, blue)
1270
+ * - linear-gradient(to right bottom, red, blue)
1271
+ * - linear-gradient(45deg, red, blue)
1272
+ * - linear-gradient(0.5turn, red, blue)
1273
+ * - radial-gradient(circle, red, blue)
1274
+ * - radial-gradient(ellipse at top, red, blue)
1175
1275
  *
1176
1276
  * @method fromString
1177
1277
  * @for jmGradient
1178
- * @return {string}
1278
+ * @param {string} s 渐变字符串
1179
1279
  */
1180
1280
  fromString(s) {
1181
- if(!s) return;
1182
- let ms = s.match(/(linear|radial)-gradient\s*\(\s*([^,]+)\s*,\s*((.|\s)+)\)/i);
1183
- if(!ms || ms.length < 3) return;
1184
- this.type = ms[1].toLowerCase();
1185
-
1186
- const ps = jmUtils.trim(ms[2]).split(/\s+/);
1187
- //线性渐变
1188
- if(this.type == 'linear') {
1189
- if(ps.length <= 2) {
1190
- this.x2 = ps[0];
1191
- this.y2 = ps[1]||0;
1281
+ if(!s) {
1282
+ console.warn('jmGradient: 渐变字符串为空');
1283
+ return;
1284
+ }
1285
+ // 使用 [\s\S] 匹配任意字符(包括换行符),支持多行渐变字符串
1286
+ const gradientMatch = s.match(/(linear|radial)-gradient\s*\(\s*([\s\S]+)\)/i);
1287
+ if(!gradientMatch || gradientMatch.length < 3) {
1288
+ console.warn('jmGradient: 无效的渐变字符串格式: "' + s + '"');
1289
+ return;
1290
+ }
1291
+
1292
+ const type = gradientMatch[1].toLowerCase();
1293
+ if(type !== 'linear' && type !== 'radial') {
1294
+ console.warn('jmGradient: 不支持的渐变类型 "' + type + '",仅支持 linear 和 radial');
1295
+ return;
1296
+ }
1297
+
1298
+ this.type = type;
1299
+ const content = jmUtils.trim(gradientMatch[2]);
1300
+
1301
+ const splitIndex = this._findSplitIndex(content);
1302
+ if(splitIndex < 0) {
1303
+ console.warn('jmGradient: 无法解析渐变内容: "' + content + '"');
1304
+ return;
1305
+ }
1306
+
1307
+ const params = content.substring(0, splitIndex).trim();
1308
+ const colorPart = content.substring(splitIndex + 1).trim();
1309
+
1310
+ if(!colorPart) {
1311
+ console.warn('jmGradient: 未找到颜色停止点');
1312
+ return;
1313
+ }
1314
+
1315
+ if(this.type === 'linear') {
1316
+ this._parseLinearParams(params);
1317
+ }
1318
+ else {
1319
+ this._parseRadialParams(params);
1320
+ }
1321
+
1322
+ const colorCount = this._parseColorStops(colorPart);
1323
+ if(colorCount === 0) {
1324
+ console.warn('jmGradient: 未找到有效的颜色停止点: "' + colorPart + '"');
1325
+ }
1326
+ else if(colorCount < 2) {
1327
+ console.warn('jmGradient: 颜色停止点至少需要2个,当前只有 ' + colorCount + ' 个');
1328
+ }
1329
+ }
1330
+
1331
+ /**
1332
+ * 找到参数和颜色的分割位置(第一个不在括号内的逗号)
1333
+ * @param {string} content 内容字符串
1334
+ * @returns {number} 分割位置索引
1335
+ */
1336
+ _findSplitIndex(content) {
1337
+ let depth = 0;
1338
+
1339
+ for(let i = 0; i < content.length; i++) {
1340
+ const char = content[i];
1341
+ if(char === '(') {
1342
+ depth++;
1192
1343
  }
1193
- else {
1194
- this.x1 = ps[0];
1195
- this.y1 = ps[1];
1196
- this.x2 = ps[2];
1197
- this.y2 = ps[3];
1344
+ else if(char === ')') {
1345
+ depth--;
1346
+ }
1347
+ else if(char === ',' && depth === 0) {
1348
+ return i;
1349
+ }
1350
+ }
1351
+
1352
+ return -1;
1353
+ }
1354
+
1355
+ /**
1356
+ * 验证渐变配置是否有效
1357
+ * @returns {boolean} 是否有效
1358
+ */
1359
+ isValid() {
1360
+ if(!this.type) return false;
1361
+ if(this.type === 'linear') {
1362
+ return typeof this.x1 !== 'undefined' ||
1363
+ typeof this.x2 !== 'undefined' ||
1364
+ typeof this._angle !== 'undefined';
1365
+ }
1366
+ if(this.type === 'radial') {
1367
+ return this.stops && this.stops.length >= 2;
1368
+ }
1369
+ return false;
1370
+ }
1371
+
1372
+ /**
1373
+ * 解析线性渐变参数
1374
+ * @param {string} params 参数字符串
1375
+ */
1376
+ _parseLinearParams(params) {
1377
+ const trimmed = jmUtils.trim(params);
1378
+
1379
+ if(trimmed.startsWith('to ')) {
1380
+ const direction = trimmed.substring(3).toLowerCase().trim();
1381
+ const dir = this._directionToAngle(direction);
1382
+ this._angle = dir.angle;
1383
+ this.x1 = dir.x1;
1384
+ this.y1 = dir.y1;
1385
+ this.x2 = dir.x2;
1386
+ this.y2 = dir.y2;
1387
+ }
1388
+ else if(this._hasAngleUnit(trimmed)) {
1389
+ const angle = this._parseAngle(trimmed);
1390
+ this._angle = angle;
1391
+ const coords = this._angleToCoords(angle);
1392
+ this.x1 = coords.x1;
1393
+ this.y1 = coords.y1;
1394
+ this.x2 = coords.x2;
1395
+ this.y2 = coords.y2;
1396
+ }
1397
+ else if(trimmed.startsWith('at ')) {
1398
+ const radialMatch = trimmed.match(/at\s+(.+)/i);
1399
+ if(radialMatch) {
1400
+ this._parseRadialParams(trimmed);
1198
1401
  }
1199
1402
  }
1200
- //径向渐变
1201
1403
  else {
1202
- if(ps.length <= 3) {
1203
- this.x2 = ps[0];
1204
- this.y2 = ps[1]||0;
1205
- this.r2 = ps[2]||0;
1404
+ const parts = trimmed.split(/\s+/);
1405
+ if(parts.length >= 4) {
1406
+ this.x1 = parts[0];
1407
+ this.y1 = parts[1];
1408
+ this.x2 = parts[2];
1409
+ this.y2 = parts[3];
1206
1410
  }
1207
- else {
1208
- this.x1 = ps[0];
1209
- this.y1 = ps[1];
1210
- this.r1 = ps[2];
1211
- this.x2 = ps[3];
1212
- this.y2 = ps[3];
1213
- this.r2 = ps[3];
1411
+ else if(parts.length === 2) {
1412
+ this.x1 = 0;
1413
+ this.y1 = 0;
1414
+ this.x2 = parts[0];
1415
+ this.y2 = parts[1];
1416
+ }
1417
+ }
1418
+ }
1419
+
1420
+ /**
1421
+ * 解析径向渐变参数
1422
+ * @param {string} params 参数字符串
1423
+ */
1424
+ _parseRadialParams(params) {
1425
+ const trimmed = jmUtils.trim(params);
1426
+
1427
+ this.shape = 'ellipse';
1428
+ this.position = { x: '50%', y: '50%' };
1429
+
1430
+ const atMatch = trimmed.match(/^(.+?)\s+at\s+(.+)$/i);
1431
+ if(atMatch) {
1432
+ const shapePart = jmUtils.trim(atMatch[1]);
1433
+ const posPart = jmUtils.trim(atMatch[2]);
1434
+ this._parseRadialShape(shapePart);
1435
+ this._parseRadialPosition(posPart);
1436
+ }
1437
+ else if(trimmed.startsWith('circle') || trimmed.startsWith('ellipse')) {
1438
+ this._parseRadialShape(trimmed);
1439
+ this.x1 = '50%';
1440
+ this.y1 = '50%';
1441
+ this.x2 = '50%';
1442
+ this.y2 = '50%';
1443
+ }
1444
+ else {
1445
+ const parts = trimmed.split(/\s+/);
1446
+ if(parts.length >= 3) {
1447
+ this.x1 = parts[0];
1448
+ this.y1 = parts[1];
1449
+ this.r1 = parts[2];
1450
+ }
1451
+ if(parts.length >= 6) {
1452
+ this.x2 = parts[3];
1453
+ this.y2 = parts[4];
1454
+ this.r2 = parts[5];
1455
+ }
1456
+ }
1457
+
1458
+ if(this.x1 === undefined && this.y1 === undefined) {
1459
+ this.x1 = '50%';
1460
+ this.y1 = '50%';
1461
+ }
1462
+ if(this.x2 === undefined && this.y2 === undefined) {
1463
+ this.x2 = '50%';
1464
+ this.y2 = '50%';
1465
+ }
1466
+ if(this.r2 === undefined) {
1467
+ this.r2 = '50%';
1468
+ }
1469
+ }
1470
+
1471
+ /**
1472
+ * 解析径向渐变形状
1473
+ * @param {string} shapePart 形状描述
1474
+ */
1475
+ _parseRadialShape(shapePart) {
1476
+ if(shapePart.startsWith('circle')) {
1477
+ this.shape = 'circle';
1478
+ const sizeMatch = shapePart.match(/circle\s*\(\s*([^)]+)\s*\)/i);
1479
+ if(sizeMatch) {
1480
+ this.r2 = jmUtils.trim(sizeMatch[1]);
1214
1481
  }
1215
1482
  }
1216
- //解析颜色偏移
1217
- //color step
1218
- const pars = ms[3].match(/((rgb(a)?\s*\([\d,\.\s]+\))|(#[a-zA-Z\d]+))\s+([\d\.]+)/ig);
1219
- if(pars && pars.length) {
1220
- for(let i=0;i<pars.length;i++) {
1221
- const par = jmUtils.trim(pars[i]);
1222
- const spindex = par.lastIndexOf(' ');
1223
- if(spindex > -1) {
1224
- const offset = Number(par.substr(spindex + 1));
1225
- const color = jmUtils.trim(par.substr(0, spindex));
1226
- if(!isNaN(offset) && color) {
1227
- this.addStop(offset, color);
1483
+ else if(shapePart.startsWith('ellipse')) {
1484
+ this.shape = 'ellipse';
1485
+ const sizeMatch = shapePart.match(/ellipse\s*\(\s*([^)]+)\s*\)/i);
1486
+ if(sizeMatch) {
1487
+ const sizes = jmUtils.trim(sizeMatch[1]).split(/\s+/);
1488
+ if(sizes.length >= 2) {
1489
+ this.rx = sizes[0];
1490
+ this.ry = sizes[1];
1491
+ }
1492
+ }
1493
+ }
1494
+ }
1495
+
1496
+ /**
1497
+ * 解析径向渐变位置
1498
+ * @param {string} posPart 位置描述
1499
+ */
1500
+ _parseRadialPosition(posPart) {
1501
+ const parts = posPart.split(/\s+/);
1502
+ if(parts.length >= 2) {
1503
+ this.x1 = parts[0];
1504
+ this.y1 = parts[1];
1505
+ this.x2 = parts[0];
1506
+ this.y2 = parts[1];
1507
+ }
1508
+ }
1509
+
1510
+ /**
1511
+ * 解析颜色停止点
1512
+ * @param {string} colorPart 颜色部分字符串
1513
+ * @returns {number} 成功解析的颜色数量
1514
+ */
1515
+ _parseColorStops(colorPart) {
1516
+ if(!colorPart) {
1517
+ return 0;
1518
+ }
1519
+
1520
+ const stops = this._splitColorStops(colorPart);
1521
+ let lastOffset = -1;
1522
+ let colorCount = 0;
1523
+
1524
+ for(let i = 0; i < stops.length; i++) {
1525
+ const stop = jmUtils.trim(stops[i]);
1526
+ if(!stop) continue;
1527
+
1528
+ const parsed = this._parseSingleColorStop(stop);
1529
+ if(!parsed) {
1530
+ continue;
1531
+ }
1532
+
1533
+ let { color, offset } = parsed;
1534
+
1535
+ if(color === 'transparent') {
1536
+ color = 'rgba(0,0,0,0)';
1537
+ }
1538
+
1539
+ if(!this._isValidColor(color)) {
1540
+ console.warn('jmGradient: 无效的颜色格式 "' + color + '"');
1541
+ continue;
1542
+ }
1543
+
1544
+ const normalizedOffset = this._normalizeOffset(offset);
1545
+ let finalOffset = normalizedOffset;
1546
+
1547
+ if(finalOffset === null) {
1548
+ if(i === 0) {
1549
+ finalOffset = 0;
1550
+ }
1551
+ else if(i === stops.length - 1) {
1552
+ finalOffset = 1;
1553
+ }
1554
+ else {
1555
+ const nextOffset = this._findNextOffset(stops, i);
1556
+ if(nextOffset !== null) {
1557
+ finalOffset = (lastOffset + nextOffset) / 2;
1558
+ }
1559
+ else {
1560
+ finalOffset = Math.min(1, lastOffset + (1 - lastOffset) / (stops.length - i));
1228
1561
  }
1229
1562
  }
1230
1563
  }
1564
+
1565
+ if(finalOffset !== null) {
1566
+ if(finalOffset < 0 || finalOffset > 1) {
1567
+ console.warn('jmGradient: 颜色偏移量 ' + finalOffset + ' 超出有效范围 [0, 1],将调整为有效范围');
1568
+ finalOffset = Math.max(0, Math.min(1, finalOffset));
1569
+ }
1570
+ lastOffset = finalOffset;
1571
+ this.addStop(finalOffset, color);
1572
+ colorCount++;
1573
+ }
1574
+ }
1575
+
1576
+ return colorCount;
1577
+ }
1578
+
1579
+ /**
1580
+ * 分割颜色停止点字符串
1581
+ * @param {string} colorPart 颜色部分字符串
1582
+ * @returns {string[]} 颜色停止点数组
1583
+ */
1584
+ _splitColorStops(colorPart) {
1585
+ const stops = [];
1586
+ let depth = 0;
1587
+ let current = '';
1588
+
1589
+ for(let i = 0; i < colorPart.length; i++) {
1590
+ const char = colorPart[i];
1591
+ if(char === '(') {
1592
+ depth++;
1593
+ current += char;
1594
+ }
1595
+ else if(char === ')') {
1596
+ depth--;
1597
+ current += char;
1598
+ }
1599
+ else if(char === ',' && depth === 0) {
1600
+ stops.push(current.trim());
1601
+ current = '';
1602
+ }
1603
+ else {
1604
+ current += char;
1605
+ }
1606
+ }
1607
+
1608
+ if(current.trim()) {
1609
+ stops.push(current.trim());
1610
+ }
1611
+
1612
+ return stops;
1613
+ }
1614
+
1615
+ /**
1616
+ * 解析单个颜色停止点
1617
+ * @param {string} stop 单个颜色停止点字符串
1618
+ * @returns {object|null} {color, offset} 或 null
1619
+ */
1620
+ _parseSingleColorStop(stop) {
1621
+ const hexMatch = stop.match(/^(#[a-fA-F0-9]{3,8})\s*(\d+(?:\.\d+)?%?)?$/i);
1622
+ if(hexMatch) {
1623
+ return { color: hexMatch[1], offset: hexMatch[2] || null };
1624
+ }
1625
+
1626
+ const rgbaMatch = stop.match(/^(rgba?\s*\([^)]+\))\s*(\d+(?:\.\d+)?%?)?$/i);
1627
+ if(rgbaMatch) {
1628
+ return { color: rgbaMatch[1], offset: rgbaMatch[2] || null };
1629
+ }
1630
+
1631
+ const hslaMatch = stop.match(/^(hsla?\s*\([^)]+\))\s*(\d+(?:\.\d+)?%?)?$/i);
1632
+ if(hslaMatch) {
1633
+ return { color: hslaMatch[1], offset: hslaMatch[2] || null };
1634
+ }
1635
+
1636
+ const namedMatch = stop.match(/^([a-zA-Z]+)\s*(\d+(?:\.\d+)?%?)?$/i);
1637
+ if(namedMatch && this._isValidColor(namedMatch[1])) {
1638
+ return { color: namedMatch[1], offset: namedMatch[2] || null };
1639
+ }
1640
+
1641
+ return null;
1642
+ }
1643
+
1644
+ /**
1645
+ * 查找下一个有偏移量的颜色停止点
1646
+ * @param {string[]} stops 颜色停止点数组
1647
+ * @param {number} currentIndex 当前索引
1648
+ * @returns {number|null} 下一个偏移量或null
1649
+ */
1650
+ _findNextOffset(stops, currentIndex) {
1651
+ for(let i = currentIndex + 1; i < stops.length; i++) {
1652
+ const parsed = this._parseSingleColorStop(jmUtils.trim(stops[i]));
1653
+ if(parsed && parsed.offset) {
1654
+ return this._normalizeOffset(parsed.offset);
1655
+ }
1656
+ }
1657
+ return null;
1658
+ }
1659
+
1660
+ /**
1661
+ * 验证颜色格式是否有效
1662
+ * @param {string} color 颜色字符串
1663
+ * @returns {boolean} 是否有效
1664
+ */
1665
+ _isValidColor(color) {
1666
+ if(!color) return false;
1667
+
1668
+ const hexPattern = /^#([a-fA-F0-9]{3,8})$/;
1669
+ if(hexPattern.test(color)) return true;
1670
+
1671
+ // 支持 rgba(r,g,b,a) 和 rgba(r, g, b, a) 等各种空格格式
1672
+ const rgbPattern = /^rgba?\s*\(\s*\d{1,3}\s*,\s*\d{1,3}\s*,\s*\d{1,3}\s*(,\s*[\d.]+\s*)?\)$/i;
1673
+ if(rgbPattern.test(color)) return true;
1674
+
1675
+ const hslPattern = /^hsla?\s*\(\s*\d{1,3}\s*,\s*\d{1,3}%?\s*,\s*\d{1,3}%?\s*(,\s*[\d.]+\s*)?\)$/i;
1676
+ if(hslPattern.test(color)) return true;
1677
+
1678
+ // 使用 jmUtils 中的完整 CSS 颜色关键字表
1679
+ if(colorKeywords && colorKeywords[color.toLowerCase()]) return true;
1680
+
1681
+ // 宽松处理:符合 CSS 关键字命名规则的字符串也视为有效颜色
1682
+ // (纯字母,可能在运行时被浏览器或其他环境解析)
1683
+ if(/^[a-zA-Z]+$/.test(color)) return true;
1684
+
1685
+ return false;
1686
+ }
1687
+
1688
+ /**
1689
+ * 标准化偏移值
1690
+ * @param {string} offset 偏移字符串
1691
+ * @returns {number|null} 0-1之间的数值或null
1692
+ */
1693
+ _normalizeOffset(offset) {
1694
+ if(!offset) return null;
1695
+ offset = jmUtils.trim(offset);
1696
+ if(offset.endsWith('%')) {
1697
+ return parseFloat(offset) / 100;
1698
+ }
1699
+ const num = parseFloat(offset);
1700
+ if(isNaN(num)) return null;
1701
+ if(num > 1) {
1702
+ return num / 100;
1703
+ }
1704
+ return num;
1705
+ }
1706
+
1707
+ /**
1708
+ * 检查字符串是否包含角度单位
1709
+ * @param {string} str 待检查字符串
1710
+ * @returns {boolean}
1711
+ */
1712
+ _hasAngleUnit(str) {
1713
+ return /^-?\d+(\.\d+)?\s*(deg|rad|grad|turn)$/i.test(str);
1714
+ }
1715
+
1716
+ /**
1717
+ * 解析角度值
1718
+ * @param {string} angleStr 角度字符串
1719
+ * @returns {number} 弧度值
1720
+ */
1721
+ _parseAngle(angleStr) {
1722
+ angleStr = jmUtils.trim(angleStr);
1723
+ const match = angleStr.match(/^(-?\d+(\.\d+)?)\s*(deg|rad|grad|turn)?$/i);
1724
+ if(!match) return 0;
1725
+
1726
+ let value = parseFloat(match[1]);
1727
+ const unit = (match[3] || 'deg').toLowerCase();
1728
+
1729
+ switch(unit) {
1730
+ case 'deg':
1731
+ return value * Math.PI / 180;
1732
+ case 'rad':
1733
+ return value;
1734
+ case 'grad':
1735
+ return value * Math.PI / 200;
1736
+ case 'turn':
1737
+ return value * 2 * Math.PI;
1738
+ default:
1739
+ return value * Math.PI / 180;
1740
+ }
1741
+ }
1742
+
1743
+ /**
1744
+ * 将角度转换为起点和终点坐标
1745
+ * @param {number} angle 弧度值
1746
+ * @returns {object} 坐标对象
1747
+ */
1748
+ _angleToCoords(angle) {
1749
+ const x = Math.cos(angle);
1750
+ const y = -Math.sin(angle);
1751
+
1752
+ return {
1753
+ x1: Math.round((0.5 - x * 0.5) * 1000) / 1000,
1754
+ y1: Math.round((0.5 + y * 0.5) * 1000) / 1000,
1755
+ x2: Math.round((0.5 + x * 0.5) * 1000) / 1000,
1756
+ y2: Math.round((0.5 - y * 0.5) * 1000) / 1000
1757
+ };
1758
+ }
1759
+
1760
+ /**
1761
+ * 将方向关键词转换为角度和坐标
1762
+ * @param {string} direction 方向描述
1763
+ * @returns {object} 包含angle和坐标的对象
1764
+ */
1765
+ _directionToAngle(direction) {
1766
+ const directions = {
1767
+ 'to top': { angle: 0, x1: '50%', y1: '100%', x2: '50%', y2: '0%' },
1768
+ 'to bottom': { angle: Math.PI, x1: '50%', y1: '0%', x2: '50%', y2: '100%' },
1769
+ 'to left': { angle: -Math.PI / 2, x1: '100%', y1: '50%', x2: '0%', y2: '50%' },
1770
+ 'to right': { angle: Math.PI / 2, x1: '0%', y1: '50%', x2: '100%', y2: '50%' },
1771
+ 'to top left': { angle: -Math.PI * 3 / 4, x1: '100%', y1: '100%', x2: '0%', y2: '0%' },
1772
+ 'to top right': { angle: -Math.PI / 4, x1: '0%', y1: '100%', x2: '100%', y2: '0%' },
1773
+ 'to bottom left': { angle: Math.PI * 3 / 4, x1: '100%', y1: '0%', x2: '0%', y2: '100%' },
1774
+ 'to bottom right': { angle: Math.PI / 4, x1: '0%', y1: '0%', x2: '100%', y2: '100%' },
1775
+ 'top': { angle: 0, x1: '50%', y1: '100%', x2: '50%', y2: '0%' },
1776
+ 'bottom': { angle: Math.PI, x1: '50%', y1: '0%', x2: '50%', y2: '100%' },
1777
+ 'left': { angle: -Math.PI / 2, x1: '100%', y1: '50%', x2: '0%', y2: '50%' },
1778
+ 'right': { angle: Math.PI / 2, x1: '0%', y1: '50%', x2: '100%', y2: '50%' }
1779
+ };
1780
+
1781
+ const dir = directions[direction];
1782
+ if(dir) {
1783
+ return dir;
1784
+ }
1785
+
1786
+ const keywordMatch = direction.match(/to\s+(top|bottom|left|right)/i);
1787
+ if(keywordMatch) {
1788
+ const mainDir = keywordMatch[1].toLowerCase();
1789
+ const secDir = direction.replace(keywordMatch[0], '').trim();
1790
+ if(secDir) {
1791
+ const combined = `to ${mainDir} ${secDir}`;
1792
+ if(directions[combined]) {
1793
+ return directions[combined];
1794
+ }
1795
+ }
1231
1796
  }
1797
+
1798
+ return { angle: 0, x1: '50%', y1: '100%', x2: '50%', y2: '0%' };
1232
1799
  }
1233
1800
 
1234
1801
  /**