@whitesev/utils 2.11.2 → 2.11.4

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.
@@ -1620,6 +1620,26 @@ declare class Utils {
1620
1620
  * @param timeout 超时时间,默认为`1500ms`
1621
1621
  */
1622
1622
  hasWorkerCSP(timeout?: number): Promise<boolean>;
1623
+ /**
1624
+ * 计算两个坐标的直线距离
1625
+ * @param positionX 坐标x信息
1626
+ * @param positionY 坐标y信息
1627
+ * @param otherPositionX 坐标x信息
1628
+ * @param otherPositionY 坐标y信息
1629
+ */
1630
+ calcPositionDistance(positionX: number | string, positionY: number | string, otherPositionX: number | string, otherPositionY: number | string): number;
1631
+ /**
1632
+ * 计算两个坐标的直线距离
1633
+ * @param position 坐标信息
1634
+ * @param otherPosition 坐标信息
1635
+ */
1636
+ calcPositionDistance(position: {
1637
+ x: number | string;
1638
+ y: number | string;
1639
+ }, otherPosition: {
1640
+ x: number | string;
1641
+ y: number | string;
1642
+ }): number;
1623
1643
  }
1624
1644
  declare const utils: Utils;
1625
1645
  export { utils as Utils };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.11.2",
4
+ "version": "2.11.4",
5
5
  "description": "一个常用的工具库",
6
6
  "keywords": [
7
7
  "ScriptCat",
@@ -83,10 +83,18 @@ export class ColorConversion {
83
83
  if (isNaN(level)) {
84
84
  throw new TypeError(`输入错误的level:${level}`);
85
85
  }
86
+ if (level < 0 || level > 1) {
87
+ throw new TypeError(`level must between 0~1.0: ${level}`);
88
+ }
86
89
  const rgbc = this.hexToRgb(color);
87
90
  for (let index = 0; index < 3; index++) {
88
91
  const rgbcItemValue = rgbc[index];
89
- const value = Math.floor(Number(rgbcItemValue) * (1 - level));
92
+ let value = Math.floor(Number(rgbcItemValue) * (1 - level));
93
+ if (value > 255) {
94
+ value = 255;
95
+ } else if (value < 0) {
96
+ value = 0;
97
+ }
90
98
  Reflect.set(rgbc, index, value);
91
99
  }
92
100
 
@@ -107,10 +115,18 @@ export class ColorConversion {
107
115
  if (isNaN(level)) {
108
116
  throw new TypeError(`输入错误的level:${level}`);
109
117
  }
118
+ if (level < 0 || level > 1) {
119
+ throw new TypeError(`level must between 0~1.0: ${level}`);
120
+ }
110
121
  const rgbc = this.hexToRgb(color);
111
122
  for (let index = 0; index < 3; index++) {
112
123
  const rgbcItemValue = Number(rgbc[index]);
113
- const value = Math.floor(255 - rgbcItemValue * level + rgbcItemValue);
124
+ let value = Math.floor(255 - rgbcItemValue * level + rgbcItemValue);
125
+ if (value > 255) {
126
+ value = 255;
127
+ } else if (value < 0) {
128
+ value = 0;
129
+ }
114
130
  Reflect.set(rgbc, index, value);
115
131
  }
116
132
  return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
package/src/Utils.ts CHANGED
@@ -3959,6 +3959,78 @@ class Utils {
3959
3959
  }
3960
3960
  });
3961
3961
  }
3962
+ /**
3963
+ * 计算两个坐标的直线距离
3964
+ * @param positionX 坐标x信息
3965
+ * @param positionY 坐标y信息
3966
+ * @param otherPositionX 坐标x信息
3967
+ * @param otherPositionY 坐标y信息
3968
+ */
3969
+ calcPositionDistance(
3970
+ positionX: number | string,
3971
+ positionY: number | string,
3972
+ otherPositionX: number | string,
3973
+ otherPositionY: number | string
3974
+ ): number;
3975
+ /**
3976
+ * 计算两个坐标的直线距离
3977
+ * @param position 坐标信息
3978
+ * @param otherPosition 坐标信息
3979
+ */
3980
+ calcPositionDistance(
3981
+ position: { x: number | string; y: number | string },
3982
+ otherPosition: { x: number | string; y: number | string }
3983
+ ): number;
3984
+ calcPositionDistance(...args: any[]) {
3985
+ let position = {
3986
+ x: 0,
3987
+ y: 0,
3988
+ };
3989
+ let otherPosition = {
3990
+ x: 0,
3991
+ y: 0,
3992
+ };
3993
+ if (typeof args[0] === "object" && args[0] != null && typeof args[1] === "object" && args[1] != null) {
3994
+ position = args[0];
3995
+ otherPosition = args[1];
3996
+ } else if (args.length === 4) {
3997
+ position = {
3998
+ x: args[0],
3999
+ y: args[1],
4000
+ };
4001
+ otherPosition = {
4002
+ x: args[2],
4003
+ y: args[3],
4004
+ };
4005
+ } else {
4006
+ throw new Error("Invalid arguments");
4007
+ }
4008
+ if (typeof position.x === "string") {
4009
+ position.x = Number(position.x);
4010
+ }
4011
+ if (isNaN(position.x)) {
4012
+ throw new Error(`Invalid x: ${position.x}`);
4013
+ }
4014
+ if (typeof position.y === "string") {
4015
+ position.y = Number(position.y);
4016
+ }
4017
+ if (isNaN(position.y)) {
4018
+ throw new Error(`Invalid y: ${position.y}`);
4019
+ }
4020
+ if (typeof otherPosition.x === "string") {
4021
+ otherPosition.x = Number(otherPosition.x);
4022
+ }
4023
+ if (isNaN(otherPosition.x)) {
4024
+ throw new Error(`Invalid x: ${otherPosition.x}`);
4025
+ }
4026
+ if (typeof otherPosition.y === "string") {
4027
+ otherPosition.y = Number(otherPosition.y);
4028
+ }
4029
+ if (isNaN(otherPosition.y)) {
4030
+ throw new Error(`Invalid y: ${otherPosition.y}`);
4031
+ }
4032
+ return Math.sqrt(Math.pow(otherPosition.x - position.x, 2) + Math.pow(otherPosition.y - position.y, 2));
4033
+ }
3962
4034
  }
3963
4035
 
3964
4036
  const utils = new Utils();