@bitbybit-dev/base 0.19.7 → 0.19.8
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.
- package/babel.config.cjs +0 -1
- package/{index.js → index.ts} +2 -1
- package/lib/api/inputs/base-inputs.ts +18 -0
- package/lib/api/inputs/{color-inputs.d.ts → color-inputs.ts} +48 -26
- package/lib/api/inputs/{lists-inputs.d.ts → lists-inputs.ts} +190 -91
- package/lib/api/inputs/{logic-inputs.d.ts → logic-inputs.ts} +46 -24
- package/lib/api/inputs/{math-inputs.d.ts → math-inputs.ts} +97 -53
- package/lib/api/inputs/{point-inputs.d.ts → point-inputs.ts} +168 -77
- package/lib/api/inputs/text-inputs.ts +108 -0
- package/lib/api/inputs/{transforms-inputs.d.ts → transforms-inputs.ts} +64 -35
- package/lib/api/inputs/{vector-inputs.d.ts → vector-inputs.ts} +104 -48
- package/lib/api/services/color.test.ts +86 -0
- package/lib/api/services/{color.js → color.ts} +34 -15
- package/lib/api/services/{geometry-helper.js → geometry-helper.ts} +43 -31
- package/lib/api/services/{index.d.ts → index.ts} +1 -1
- package/lib/api/services/lists.test.ts +612 -0
- package/lib/api/services/{lists.js → lists.ts} +83 -59
- package/lib/api/services/logic.test.ts +187 -0
- package/lib/api/services/{logic.js → logic.ts} +32 -24
- package/lib/api/services/math.test.ts +622 -0
- package/lib/api/services/{math.js → math.ts} +136 -71
- package/lib/api/services/{point.js → point.ts} +67 -32
- package/lib/api/services/text.test.ts +55 -0
- package/lib/api/services/{text.js → text.ts} +17 -7
- package/lib/api/services/{transforms.js → transforms.ts} +83 -37
- package/lib/api/services/vector.test.ts +360 -0
- package/lib/api/services/{vector.js → vector.ts} +80 -42
- package/lib/{index.d.ts → index.ts} +1 -0
- package/package.json +1 -1
- package/tsconfig.bitbybit.json +26 -0
- package/tsconfig.json +24 -0
- package/babel.config.d.cts +0 -5
- package/index.d.ts +0 -1
- package/lib/api/index.js +0 -1
- package/lib/api/inputs/base-inputs.d.ts +0 -35
- package/lib/api/inputs/base-inputs.js +0 -1
- package/lib/api/inputs/color-inputs.js +0 -164
- package/lib/api/inputs/index.js +0 -9
- package/lib/api/inputs/inputs.js +0 -9
- package/lib/api/inputs/lists-inputs.js +0 -576
- package/lib/api/inputs/logic-inputs.js +0 -111
- package/lib/api/inputs/math-inputs.js +0 -391
- package/lib/api/inputs/point-inputs.js +0 -521
- package/lib/api/inputs/text-inputs.d.ts +0 -83
- package/lib/api/inputs/text-inputs.js +0 -120
- package/lib/api/inputs/transforms-inputs.js +0 -200
- package/lib/api/inputs/vector-inputs.js +0 -304
- package/lib/api/services/color.d.ts +0 -114
- package/lib/api/services/geometry-helper.d.ts +0 -15
- package/lib/api/services/index.js +0 -9
- package/lib/api/services/lists.d.ts +0 -287
- package/lib/api/services/logic.d.ts +0 -99
- package/lib/api/services/math.d.ts +0 -349
- package/lib/api/services/point.d.ts +0 -222
- package/lib/api/services/text.d.ts +0 -69
- package/lib/api/services/transforms.d.ts +0 -122
- package/lib/api/services/vector.d.ts +0 -320
- package/lib/index.js +0 -1
- /package/lib/api/{index.d.ts → index.ts} +0 -0
- /package/lib/api/inputs/{index.d.ts → index.ts} +0 -0
- /package/lib/api/inputs/{inputs.d.ts → inputs.ts} +0 -0
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import * as Inputs from "../inputs";
|
|
2
|
+
import { MathBitByBit } from "./math";
|
|
3
|
+
|
|
1
4
|
export class Color {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
constructor(private readonly math: MathBitByBit) { }
|
|
8
|
+
|
|
5
9
|
/**
|
|
6
10
|
* Creates a hex color
|
|
7
11
|
* @param inputs Color hex
|
|
@@ -10,9 +14,10 @@ export class Color {
|
|
|
10
14
|
* @shortname color
|
|
11
15
|
* @drawable false
|
|
12
16
|
*/
|
|
13
|
-
hexColor(inputs) {
|
|
17
|
+
hexColor(inputs: Inputs.Color.HexDto): Inputs.Base.Color {
|
|
14
18
|
return inputs.color;
|
|
15
19
|
}
|
|
20
|
+
|
|
16
21
|
/**
|
|
17
22
|
* Creates rgb color from hex
|
|
18
23
|
* @param inputs Color hex
|
|
@@ -21,7 +26,7 @@ export class Color {
|
|
|
21
26
|
* @shortname hex to rgb
|
|
22
27
|
* @drawable false
|
|
23
28
|
*/
|
|
24
|
-
hexToRgb(inputs) {
|
|
29
|
+
hexToRgb(inputs: Inputs.Color.HexDto): Inputs.Base.ColorRGB {
|
|
25
30
|
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(inputs.color);
|
|
26
31
|
return result ? {
|
|
27
32
|
r: parseInt(result[1], 16),
|
|
@@ -29,6 +34,7 @@ export class Color {
|
|
|
29
34
|
b: parseInt(result[3], 16)
|
|
30
35
|
} : undefined;
|
|
31
36
|
}
|
|
37
|
+
|
|
32
38
|
/**
|
|
33
39
|
* Creates hex color from rgb
|
|
34
40
|
* @param inputs Color hext
|
|
@@ -37,10 +43,11 @@ export class Color {
|
|
|
37
43
|
* @shortname rgb to hex
|
|
38
44
|
* @drawable false
|
|
39
45
|
*/
|
|
40
|
-
rgbToHex(inputs) {
|
|
46
|
+
rgbToHex(inputs: Inputs.Color.RGBMinMaxDto): Inputs.Base.Color {
|
|
41
47
|
let r = inputs.r;
|
|
42
48
|
let g = inputs.g;
|
|
43
49
|
let b = inputs.b;
|
|
50
|
+
|
|
44
51
|
// sometimes rgb values are in 0 - 100 or 0 - 1 ranges
|
|
45
52
|
// so we need to remap them to 0 - 255
|
|
46
53
|
if (inputs.max !== 255) {
|
|
@@ -48,9 +55,13 @@ export class Color {
|
|
|
48
55
|
g = Math.round(this.math.remap({ number: g, fromLow: inputs.min, fromHigh: inputs.max, toLow: 0, toHigh: 255 }));
|
|
49
56
|
b = Math.round(this.math.remap({ number: b, fromLow: inputs.min, fromHigh: inputs.max, toLow: 0, toHigh: 255 }));
|
|
50
57
|
}
|
|
58
|
+
|
|
51
59
|
const s = `#${Number(0x1000000 + r * 0x10000 + g * 0x100 + b).toString(16).substring(1, 7)}`;
|
|
60
|
+
|
|
52
61
|
return s;
|
|
53
62
|
}
|
|
63
|
+
|
|
64
|
+
|
|
54
65
|
/**
|
|
55
66
|
* Creates hex color from rgb obj that contains {r, g, b} properties in certain range
|
|
56
67
|
* @param inputs Color hext
|
|
@@ -59,9 +70,10 @@ export class Color {
|
|
|
59
70
|
* @shortname rgb obj to hex
|
|
60
71
|
* @drawable false
|
|
61
72
|
*/
|
|
62
|
-
rgbObjToHex(inputs) {
|
|
73
|
+
rgbObjToHex(inputs: Inputs.Color.RGBObjectMaxDto): Inputs.Base.Color {
|
|
63
74
|
return this.rgbToHex({ r: inputs.rgb.r, g: inputs.rgb.g, b: inputs.rgb.b, min: inputs.min, max: inputs.max });
|
|
64
75
|
}
|
|
76
|
+
|
|
65
77
|
/**
|
|
66
78
|
* Creates rgb color from hex and maps to different range if needed
|
|
67
79
|
* @param inputs Color hext
|
|
@@ -70,7 +82,7 @@ export class Color {
|
|
|
70
82
|
* @shortname hex to rgb mapped
|
|
71
83
|
* @drawable false
|
|
72
84
|
*/
|
|
73
|
-
hexToRgbMapped(inputs) {
|
|
85
|
+
hexToRgbMapped(inputs: Inputs.Color.HexDtoMapped): Inputs.Base.ColorRGB {
|
|
74
86
|
const rgb = this.hexToRgb(inputs);
|
|
75
87
|
return {
|
|
76
88
|
r: this.math.remap({ number: rgb.r, fromLow: 0, fromHigh: 255, toLow: inputs.from, toHigh: inputs.to }),
|
|
@@ -78,6 +90,7 @@ export class Color {
|
|
|
78
90
|
b: this.math.remap({ number: rgb.b, fromLow: 0, fromHigh: 255, toLow: inputs.from, toHigh: inputs.to }),
|
|
79
91
|
};
|
|
80
92
|
}
|
|
93
|
+
|
|
81
94
|
/**
|
|
82
95
|
* Get red param
|
|
83
96
|
* @param inputs Color hext
|
|
@@ -86,10 +99,11 @@ export class Color {
|
|
|
86
99
|
* @shortname red
|
|
87
100
|
* @drawable false
|
|
88
101
|
*/
|
|
89
|
-
getRedParam(inputs) {
|
|
102
|
+
getRedParam(inputs: Inputs.Color.HexDtoMapped): number {
|
|
90
103
|
const rgb = this.hexToRgbMapped(inputs);
|
|
91
104
|
return rgb.r;
|
|
92
105
|
}
|
|
106
|
+
|
|
93
107
|
/**
|
|
94
108
|
* Get green param
|
|
95
109
|
* @param inputs Color hext
|
|
@@ -98,10 +112,11 @@ export class Color {
|
|
|
98
112
|
* @shortname green
|
|
99
113
|
* @drawable false
|
|
100
114
|
*/
|
|
101
|
-
getGreenParam(inputs) {
|
|
115
|
+
getGreenParam(inputs: Inputs.Color.HexDtoMapped): number {
|
|
102
116
|
const rgb = this.hexToRgbMapped(inputs);
|
|
103
117
|
return rgb.g;
|
|
104
118
|
}
|
|
119
|
+
|
|
105
120
|
/**
|
|
106
121
|
* Get blue param
|
|
107
122
|
* @param inputs Color hext
|
|
@@ -110,10 +125,11 @@ export class Color {
|
|
|
110
125
|
* @shortname blue
|
|
111
126
|
* @drawable false
|
|
112
127
|
*/
|
|
113
|
-
getBlueParam(inputs) {
|
|
128
|
+
getBlueParam(inputs: Inputs.Color.HexDtoMapped): number {
|
|
114
129
|
const rgb = this.hexToRgbMapped(inputs);
|
|
115
130
|
return rgb.b;
|
|
116
131
|
}
|
|
132
|
+
|
|
117
133
|
/**
|
|
118
134
|
* RGB to red
|
|
119
135
|
* @param inputs Color rgb
|
|
@@ -122,9 +138,10 @@ export class Color {
|
|
|
122
138
|
* @shortname red
|
|
123
139
|
* @drawable false
|
|
124
140
|
*/
|
|
125
|
-
rgbToRed(inputs) {
|
|
141
|
+
rgbToRed(inputs: Inputs.Color.RGBObjectDto): number {
|
|
126
142
|
return inputs.rgb.r;
|
|
127
143
|
}
|
|
144
|
+
|
|
128
145
|
/**
|
|
129
146
|
* RGB to green
|
|
130
147
|
* @param inputs Color rgb
|
|
@@ -133,9 +150,10 @@ export class Color {
|
|
|
133
150
|
* @shortname green
|
|
134
151
|
* @drawable false
|
|
135
152
|
*/
|
|
136
|
-
rgbToGreen(inputs) {
|
|
153
|
+
rgbToGreen(inputs: Inputs.Color.RGBObjectDto): number {
|
|
137
154
|
return inputs.rgb.g;
|
|
138
155
|
}
|
|
156
|
+
|
|
139
157
|
/**
|
|
140
158
|
* RGB to blue
|
|
141
159
|
* @param inputs Color rgb
|
|
@@ -144,9 +162,10 @@ export class Color {
|
|
|
144
162
|
* @shortname blue
|
|
145
163
|
* @drawable false
|
|
146
164
|
*/
|
|
147
|
-
rgbToBlue(inputs) {
|
|
165
|
+
rgbToBlue(inputs: Inputs.Color.RGBObjectDto): number {
|
|
148
166
|
return inputs.rgb.b;
|
|
149
167
|
}
|
|
168
|
+
|
|
150
169
|
/**
|
|
151
170
|
* Invert color
|
|
152
171
|
* @param inputs hex color and black and white option
|
|
@@ -155,7 +174,7 @@ export class Color {
|
|
|
155
174
|
* @shortname invert color
|
|
156
175
|
* @drawable false
|
|
157
176
|
*/
|
|
158
|
-
invert(inputs) {
|
|
177
|
+
invert(inputs: Inputs.Color.InvertHexDto): Inputs.Base.Color {
|
|
159
178
|
const { r, g, b } = this.hexToRgbMapped({ color: inputs.color, from: 0, to: 255 });
|
|
160
179
|
if (inputs.blackAndWhite) {
|
|
161
180
|
return (r * 0.299 + g * 0.587 + b * 0.114) > 186
|
|
@@ -1,34 +1,41 @@
|
|
|
1
|
+
import * as Inputs from "../inputs";
|
|
2
|
+
|
|
1
3
|
export class GeometryHelper {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return Array.isArray(value) ?
|
|
5
|
-
1 + Math.max(...value.map(this.getArrayDepth)) :
|
|
6
|
-
0;
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
transformControlPoints(transformation, transformedControlPoints) {
|
|
4
|
+
|
|
5
|
+
transformControlPoints(transformation: number[][] | number[][][], transformedControlPoints: Inputs.Base.Point3[]): Inputs.Base.Point3[] {
|
|
10
6
|
const transformationArrays = this.getFlatTransformations(transformation);
|
|
7
|
+
|
|
11
8
|
transformationArrays.forEach(transform => {
|
|
9
|
+
|
|
12
10
|
transformedControlPoints = this.transformPointsByMatrixArray(transformedControlPoints, transform);
|
|
13
11
|
});
|
|
14
12
|
return transformedControlPoints;
|
|
15
13
|
}
|
|
16
|
-
|
|
14
|
+
|
|
15
|
+
getFlatTransformations(transformation: number[][] | number[][][]): number[][] {
|
|
17
16
|
let transformationArrays = [];
|
|
17
|
+
|
|
18
18
|
if (this.getArrayDepth(transformation) === 3) {
|
|
19
19
|
transformation.forEach(transform => {
|
|
20
20
|
transformationArrays.push(...transform);
|
|
21
21
|
});
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
22
|
+
} else {
|
|
24
23
|
transformationArrays = transformation;
|
|
25
24
|
}
|
|
26
25
|
return transformationArrays;
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getArrayDepth = (value): number => {
|
|
29
|
+
return Array.isArray(value) ?
|
|
30
|
+
1 + Math.max(...value.map(this.getArrayDepth)) :
|
|
31
|
+
0;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
transformPointsByMatrixArray(points: Inputs.Base.Point3[], transform: number[]): Inputs.Base.Point3[] {
|
|
29
35
|
return this.transformPointsCoordinates(points, transform);
|
|
30
36
|
}
|
|
31
|
-
|
|
37
|
+
|
|
38
|
+
transformPointsCoordinates(points: Inputs.Base.Point3[], transform: number[]): Inputs.Base.Point3[] {
|
|
32
39
|
const transformedPoints = [];
|
|
33
40
|
for (const pt of points) {
|
|
34
41
|
const transformedVector = this.transformCoordinates(pt[0], pt[1], pt[2], transform);
|
|
@@ -36,9 +43,10 @@ export class GeometryHelper {
|
|
|
36
43
|
}
|
|
37
44
|
return transformedPoints;
|
|
38
45
|
}
|
|
46
|
+
|
|
39
47
|
// Algorithm works with arbitrary length numeric vectors. This algorithm is more costly for longer arrays of vectors
|
|
40
|
-
removeAllDuplicateVectors(vectors, tolerance = 1e-7) {
|
|
41
|
-
const cleanVectors = [];
|
|
48
|
+
removeAllDuplicateVectors(vectors: number[][], tolerance = 1e-7): number[][] {
|
|
49
|
+
const cleanVectors: number[][] = [];
|
|
42
50
|
vectors.forEach(vector => {
|
|
43
51
|
// when there are no vectors in cleanVectors array that match the current vector, push it in.
|
|
44
52
|
if (!cleanVectors.some(s => this.vectorsTheSame(vector, s, tolerance))) {
|
|
@@ -47,9 +55,10 @@ export class GeometryHelper {
|
|
|
47
55
|
});
|
|
48
56
|
return cleanVectors;
|
|
49
57
|
}
|
|
58
|
+
|
|
50
59
|
// Algorithm works with arbitrary length numeric vectors.
|
|
51
|
-
removeConsecutiveVectorDuplicates(vectors, checkFirstAndLast = true, tolerance = 1e-7) {
|
|
52
|
-
const vectorsRemaining = [];
|
|
60
|
+
removeConsecutiveVectorDuplicates(vectors: number[][], checkFirstAndLast = true, tolerance = 1e-7): number[][] {
|
|
61
|
+
const vectorsRemaining: number[][] = [];
|
|
53
62
|
if (vectors.length > 1) {
|
|
54
63
|
for (let i = 1; i < vectors.length; i++) {
|
|
55
64
|
const currentVector = vectors[i];
|
|
@@ -68,18 +77,17 @@ export class GeometryHelper {
|
|
|
68
77
|
vectorsRemaining.pop();
|
|
69
78
|
}
|
|
70
79
|
}
|
|
71
|
-
}
|
|
72
|
-
else if (vectors.length === 1) {
|
|
80
|
+
} else if (vectors.length === 1) {
|
|
73
81
|
vectorsRemaining.push(...vectors);
|
|
74
82
|
}
|
|
75
83
|
return vectorsRemaining;
|
|
76
84
|
}
|
|
77
|
-
|
|
85
|
+
|
|
86
|
+
vectorsTheSame(vec1: number[], vec2: number[], tolerance: number) {
|
|
78
87
|
let result = false;
|
|
79
88
|
if (vec1.length !== vec2.length) {
|
|
80
89
|
return result;
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
90
|
+
} else {
|
|
83
91
|
result = true;
|
|
84
92
|
for (let i = 0; i < vec1.length; i++) {
|
|
85
93
|
if (!this.approxEq(vec1[i], vec2[i], tolerance)) {
|
|
@@ -90,11 +98,13 @@ export class GeometryHelper {
|
|
|
90
98
|
}
|
|
91
99
|
return result;
|
|
92
100
|
}
|
|
93
|
-
|
|
101
|
+
|
|
102
|
+
approxEq(num1: number, num2: number, tolerance: number): boolean {
|
|
94
103
|
const res = Math.abs(num1 - num2) < tolerance;
|
|
95
104
|
return res;
|
|
96
105
|
}
|
|
97
|
-
|
|
106
|
+
|
|
107
|
+
removeConsecutivePointDuplicates(points: Inputs.Base.Point3[], checkFirstAndLast = true, tolerance = 1e-7): Inputs.Base.Point3[] {
|
|
98
108
|
const pointsRemaining = [];
|
|
99
109
|
if (points.length > 1) {
|
|
100
110
|
for (let i = 1; i < points.length; i++) {
|
|
@@ -114,21 +124,20 @@ export class GeometryHelper {
|
|
|
114
124
|
pointsRemaining.pop();
|
|
115
125
|
}
|
|
116
126
|
}
|
|
117
|
-
}
|
|
118
|
-
else if (points.length === 1) {
|
|
127
|
+
} else if (points.length === 1) {
|
|
119
128
|
pointsRemaining.push(...points);
|
|
120
129
|
}
|
|
121
130
|
return pointsRemaining;
|
|
122
131
|
}
|
|
123
|
-
|
|
132
|
+
|
|
133
|
+
arePointsTheSame(pointA: Inputs.Base.Point3 | Inputs.Base.Point2, pointB: Inputs.Base.Point3 | Inputs.Base.Point2, tolerance: number): boolean {
|
|
124
134
|
let result = false;
|
|
125
135
|
if (pointA.length === 2 && pointB.length === 2) {
|
|
126
136
|
if (this.approxEq(pointA[0], pointB[0], tolerance) &&
|
|
127
137
|
this.approxEq(pointA[1], pointB[1], tolerance)) {
|
|
128
138
|
result = true;
|
|
129
139
|
}
|
|
130
|
-
}
|
|
131
|
-
else if (pointA.length === 3 && pointB.length === 3) {
|
|
140
|
+
} else if (pointA.length === 3 && pointB.length === 3) {
|
|
132
141
|
if (this.approxEq(pointA[0], pointB[0], tolerance) &&
|
|
133
142
|
this.approxEq(pointA[1], pointB[1], tolerance) &&
|
|
134
143
|
this.approxEq(pointA[2], pointB[2], tolerance)) {
|
|
@@ -137,15 +146,18 @@ export class GeometryHelper {
|
|
|
137
146
|
}
|
|
138
147
|
return result;
|
|
139
148
|
}
|
|
140
|
-
|
|
149
|
+
|
|
150
|
+
private transformCoordinates(x: number, y: number, z: number, transformation: number[]): Inputs.Base.Vector3 {
|
|
141
151
|
const m = transformation;
|
|
142
152
|
const rx = x * m[0] + y * m[4] + z * m[8] + m[12];
|
|
143
153
|
const ry = x * m[1] + y * m[5] + z * m[9] + m[13];
|
|
144
154
|
const rz = x * m[2] + y * m[6] + z * m[10] + m[14];
|
|
145
155
|
const rw = 1 / (x * m[3] + y * m[7] + z * m[11] + m[15]);
|
|
156
|
+
|
|
146
157
|
const newx = rx * rw;
|
|
147
158
|
const newy = ry * rw;
|
|
148
159
|
const newz = rz * rw;
|
|
149
160
|
return [newx, newy, newz];
|
|
150
161
|
}
|
|
162
|
+
|
|
151
163
|
}
|