-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
518 lines (517 loc) · 15.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"use strict";
var poline = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
Poline: () => Poline,
hslToPoint: () => hslToPoint,
pointToHSL: () => pointToHSL,
positionFunctions: () => positionFunctions,
randomHSLPair: () => randomHSLPair,
randomHSLTriple: () => randomHSLTriple
});
var pointToHSL = (xyz, invertedLightness) => {
const [x, y, z] = xyz;
const cx = 0.5;
const cy = 0.5;
const radians = Math.atan2(y - cy, x - cx);
let deg = radians * (180 / Math.PI);
deg = (360 + deg) % 360;
const s = z;
const dist = Math.sqrt(Math.pow(y - cy, 2) + Math.pow(x - cx, 2));
const l = dist / cx;
return [deg, s, invertedLightness ? 1 - l : l];
};
var hslToPoint = (hsl, invertedLightness) => {
const [h, s, l] = hsl;
const cx = 0.5;
const cy = 0.5;
const radians = h / (180 / Math.PI);
const dist = (invertedLightness ? 1 - l : l) * cx;
const x = cx + dist * Math.cos(radians);
const y = cy + dist * Math.sin(radians);
const z = s;
return [x, y, z];
};
var randomHSLPair = (startHue = Math.random() * 360, saturations = [Math.random(), Math.random()], lightnesses = [0.75 + Math.random() * 0.2, 0.3 + Math.random() * 0.2]) => [
[startHue, saturations[0], lightnesses[0]],
[(startHue + 60 + Math.random() * 180) % 360, saturations[1], lightnesses[1]]
];
var randomHSLTriple = (startHue = Math.random() * 360, saturations = [Math.random(), Math.random(), Math.random()], lightnesses = [
0.75 + Math.random() * 0.2,
Math.random() * 0.2,
0.75 + Math.random() * 0.2
]) => [
[startHue, saturations[0], lightnesses[0]],
[(startHue + 60 + Math.random() * 180) % 360, saturations[1], lightnesses[1]],
[(startHue + 60 + Math.random() * 180) % 360, saturations[2], lightnesses[2]]
];
var vectorOnLine = (t, p1, p2, invert = false, fx = (t2, invert2) => invert2 ? 1 - t2 : t2, fy = (t2, invert2) => invert2 ? 1 - t2 : t2, fz = (t2, invert2) => invert2 ? 1 - t2 : t2) => {
const tModifiedX = fx(t, invert);
const tModifiedY = fy(t, invert);
const tModifiedZ = fz(t, invert);
const x = (1 - tModifiedX) * p1[0] + tModifiedX * p2[0];
const y = (1 - tModifiedY) * p1[1] + tModifiedY * p2[1];
const z = (1 - tModifiedZ) * p1[2] + tModifiedZ * p2[2];
return [x, y, z];
};
var vectorsOnLine = (p1, p2, numPoints = 4, invert = false, fx = (t, invert2) => invert2 ? 1 - t : t, fy = (t, invert2) => invert2 ? 1 - t : t, fz = (t, invert2) => invert2 ? 1 - t : t) => {
const points = [];
for (let i = 0; i < numPoints; i++) {
const [x, y, z] = vectorOnLine(
i / (numPoints - 1),
p1,
p2,
invert,
fx,
fy,
fz
);
points.push([x, y, z]);
}
return points;
};
var linearPosition = (t) => {
return t;
};
var exponentialPosition = (t, reverse = false) => {
if (reverse) {
return 1 - (1 - t) ** 2;
}
return t ** 2;
};
var quadraticPosition = (t, reverse = false) => {
if (reverse) {
return 1 - (1 - t) ** 3;
}
return t ** 3;
};
var cubicPosition = (t, reverse = false) => {
if (reverse) {
return 1 - (1 - t) ** 4;
}
return t ** 4;
};
var quarticPosition = (t, reverse = false) => {
if (reverse) {
return 1 - (1 - t) ** 5;
}
return t ** 5;
};
var sinusoidalPosition = (t, reverse = false) => {
if (reverse) {
return 1 - Math.sin((1 - t) * Math.PI / 2);
}
return Math.sin(t * Math.PI / 2);
};
var asinusoidalPosition = (t, reverse = false) => {
if (reverse) {
return 1 - Math.asin(1 - t) / (Math.PI / 2);
}
return Math.asin(t) / (Math.PI / 2);
};
var arcPosition = (t, reverse = false) => {
if (reverse) {
return Math.sqrt(1 - (1 - t) ** 2);
}
return 1 - Math.sqrt(1 - t);
};
var smoothStepPosition = (t) => {
return t ** 2 * (3 - 2 * t);
};
var positionFunctions = {
linearPosition,
exponentialPosition,
quadraticPosition,
cubicPosition,
quarticPosition,
sinusoidalPosition,
asinusoidalPosition,
arcPosition,
smoothStepPosition
};
var distance = (p1, p2, hueMode = false) => {
const a1 = p1[0];
const a2 = p2[0];
let diffA = 0;
if (hueMode && a1 !== null && a2 !== null) {
diffA = Math.min(Math.abs(a1 - a2), 360 - Math.abs(a1 - a2));
diffA = diffA / 360;
} else {
diffA = a1 === null || a2 === null ? 0 : a1 - a2;
}
const a = diffA;
const b = p1[1] === null || p2[1] === null ? 0 : p2[1] - p1[1];
const c = p1[2] === null || p2[2] === null ? 0 : p2[2] - p1[2];
return Math.sqrt(a * a + b * b + c * c);
};
var ColorPoint = class {
constructor({ xyz, color, invertedLightness } = {}) {
this.x = 0;
this.y = 0;
this.z = 0;
this.color = [0, 0, 0];
this._invertedLightness = false;
this._invertedLightness = invertedLightness || false;
this.positionOrColor({ xyz, color, invertedLightness });
}
positionOrColor({ xyz, color, invertedLightness }) {
if (xyz && color) {
throw new Error("Point must be initialized with either x,y,z or hsl");
} else if (xyz) {
this.x = xyz[0];
this.y = xyz[1];
this.z = xyz[2];
this.color = pointToHSL(
[this.x, this.y, this.z],
invertedLightness || false
);
} else if (color) {
this.color = color;
[this.x, this.y, this.z] = hslToPoint(color, invertedLightness || false);
}
}
set position([x, y, z]) {
this.x = x;
this.y = y;
this.z = z;
this.color = pointToHSL(
[this.x, this.y, this.z],
this._invertedLightness
);
}
get position() {
return [this.x, this.y, this.z];
}
set hsl([h, s, l]) {
this.color = [h, s, l];
[this.x, this.y, this.z] = hslToPoint(
this.color,
this._invertedLightness
);
}
get hsl() {
return this.color;
}
get hslCSS() {
const [h, s, l] = this.color;
return `hsl(${h.toFixed(2)}, ${(s * 100).toFixed(2)}%, ${(l * 100).toFixed(
2
)}%)`;
}
get oklchCSS() {
const [h, s, l] = this.color;
return `oklch(${(l * 100).toFixed(2)}% ${(s * 0.4).toFixed(3)} ${h.toFixed(
2
)})`;
}
get lchCSS() {
const [h, s, l] = this.color;
return `lch(${(l * 100).toFixed(2)}% ${(s * 150).toFixed(2)} ${h.toFixed(
2
)})`;
}
shiftHue(angle) {
this.color[0] = (360 + (this.color[0] + angle)) % 360;
[this.x, this.y, this.z] = hslToPoint(
this.color,
this._invertedLightness
);
}
};
var Poline = class {
constructor({
anchorColors = randomHSLPair(),
numPoints = 4,
positionFunction = sinusoidalPosition,
positionFunctionX,
positionFunctionY,
positionFunctionZ,
closedLoop,
invertedLightness
} = {
anchorColors: randomHSLPair(),
numPoints: 4,
positionFunction: sinusoidalPosition,
closedLoop: false
}) {
this._needsUpdate = true;
this._positionFunctionX = sinusoidalPosition;
this._positionFunctionY = sinusoidalPosition;
this._positionFunctionZ = sinusoidalPosition;
this.connectLastAndFirstAnchor = false;
this._animationFrame = null;
this._invertedLightness = false;
if (!anchorColors || anchorColors.length < 2) {
throw new Error("Must have at least two anchor colors");
}
this._anchorPoints = anchorColors.map(
(point) => new ColorPoint({ color: point, invertedLightness })
);
this._numPoints = numPoints + 2;
this._positionFunctionX = positionFunctionX || positionFunction || sinusoidalPosition;
this._positionFunctionY = positionFunctionY || positionFunction || sinusoidalPosition;
this._positionFunctionZ = positionFunctionZ || positionFunction || sinusoidalPosition;
this.connectLastAndFirstAnchor = closedLoop || false;
this._invertedLightness = invertedLightness || false;
this.updateAnchorPairs();
}
get numPoints() {
return this._numPoints - 2;
}
set numPoints(numPoints) {
if (numPoints < 1) {
throw new Error("Must have at least one point");
}
this._numPoints = numPoints + 2;
this.updateAnchorPairs();
}
set positionFunction(positionFunction) {
if (Array.isArray(positionFunction)) {
if (positionFunction.length !== 3) {
throw new Error("Position function array must have 3 elements");
}
if (typeof positionFunction[0] !== "function" || typeof positionFunction[1] !== "function" || typeof positionFunction[2] !== "function") {
throw new Error("Position function array must have 3 functions");
}
this._positionFunctionX = positionFunction[0];
this._positionFunctionY = positionFunction[1];
this._positionFunctionZ = positionFunction[2];
} else {
this._positionFunctionX = positionFunction;
this._positionFunctionY = positionFunction;
this._positionFunctionZ = positionFunction;
}
this.updateAnchorPairs();
}
get positionFunction() {
if (this._positionFunctionX === this._positionFunctionY && this._positionFunctionX === this._positionFunctionZ) {
return this._positionFunctionX;
}
return [
this._positionFunctionX,
this._positionFunctionY,
this._positionFunctionZ
];
}
set positionFunctionX(positionFunctionX) {
this._positionFunctionX = positionFunctionX;
this.updateAnchorPairs();
}
get positionFunctionX() {
return this._positionFunctionX;
}
set positionFunctionY(positionFunctionY) {
this._positionFunctionY = positionFunctionY;
this.updateAnchorPairs();
}
get positionFunctionY() {
return this._positionFunctionY;
}
set positionFunctionZ(positionFunctionZ) {
this._positionFunctionZ = positionFunctionZ;
this.updateAnchorPairs();
}
get positionFunctionZ() {
return this._positionFunctionZ;
}
get anchorPoints() {
return this._anchorPoints;
}
set anchorPoints(anchorPoints) {
this._anchorPoints = anchorPoints;
this.updateAnchorPairs();
}
updateAnchorPairs() {
this._anchorPairs = [];
const anchorPointsLength = this.connectLastAndFirstAnchor ? this.anchorPoints.length : this.anchorPoints.length - 1;
for (let i = 0; i < anchorPointsLength; i++) {
const pair = [
this.anchorPoints[i],
this.anchorPoints[(i + 1) % this.anchorPoints.length]
];
this._anchorPairs.push(pair);
}
this.points = this._anchorPairs.map((pair, i) => {
const p1position = pair[0] ? pair[0].position : [0, 0, 0];
const p2position = pair[1] ? pair[1].position : [0, 0, 0];
return vectorsOnLine(
p1position,
p2position,
this._numPoints,
i % 2 ? true : false,
this.positionFunctionX,
this.positionFunctionY,
this.positionFunctionZ
).map(
(p) => new ColorPoint({ xyz: p, invertedLightness: this._invertedLightness })
);
});
}
addAnchorPoint({
xyz,
color,
insertAtIndex
}) {
const newAnchor = new ColorPoint({
xyz,
color,
invertedLightness: this._invertedLightness
});
if (insertAtIndex) {
this.anchorPoints.splice(insertAtIndex, 0, newAnchor);
} else {
this.anchorPoints.push(newAnchor);
}
this.updateAnchorPairs();
return newAnchor;
}
removeAnchorPoint({
point,
index
}) {
if (!point && index === void 0) {
throw new Error("Must provide a point or index");
}
let apid;
if (index !== void 0) {
apid = index;
} else if (point) {
apid = this.anchorPoints.indexOf(point);
}
if (apid > -1 && apid < this.anchorPoints.length) {
this.anchorPoints.splice(apid, 1);
this.updateAnchorPairs();
} else {
throw new Error("Point not found");
}
}
updateAnchorPoint({
point,
pointIndex,
xyz,
color
}) {
if (pointIndex) {
point = this.anchorPoints[pointIndex];
}
if (!point) {
throw new Error("Must provide a point or pointIndex");
}
if (!xyz && !color) {
throw new Error("Must provide a new xyz position or color");
}
if (xyz)
point.position = xyz;
if (color)
point.hsl = color;
this.updateAnchorPairs();
return point;
}
getClosestAnchorPoint({
xyz,
hsl,
maxDistance = 1
}) {
if (!xyz && !hsl) {
throw new Error("Must provide a xyz or hsl");
}
let distances;
if (xyz) {
distances = this.anchorPoints.map(
(anchor) => distance(anchor.position, xyz)
);
} else if (hsl) {
distances = this.anchorPoints.map(
(anchor) => distance(anchor.hsl, hsl, true)
);
}
const minDistance = Math.min(...distances);
if (minDistance > maxDistance) {
return null;
}
const closestAnchorIndex = distances.indexOf(minDistance);
return this.anchorPoints[closestAnchorIndex] || null;
}
set closedLoop(newStatus) {
this.connectLastAndFirstAnchor = newStatus;
this.updateAnchorPairs();
}
get closedLoop() {
return this.connectLastAndFirstAnchor;
}
set invertedLightness(newStatus) {
this._invertedLightness = newStatus;
this.updateAnchorPairs();
}
get invertedLightness() {
return this._invertedLightness;
}
get flattenedPoints() {
return this.points.flat().filter((p, i) => i != 0 ? i % this._numPoints : true);
}
get colors() {
const colors = this.flattenedPoints.map((p) => p.color);
if (this.connectLastAndFirstAnchor) {
colors.pop();
}
return colors;
}
cssColors(mode = "hsl") {
const methods = {
hsl: (p) => p.hslCSS,
oklch: (p) => p.oklchCSS,
lch: (p) => p.lchCSS
};
const cssColors = this.flattenedPoints.map(methods[mode]);
if (this.connectLastAndFirstAnchor) {
cssColors.pop();
}
return cssColors;
}
get colorsCSS() {
return this.cssColors("hsl");
}
get colorsCSSlch() {
return this.cssColors("lch");
}
get colorsCSSoklch() {
return this.cssColors("oklch");
}
shiftHue(hShift = 20) {
this.anchorPoints.forEach((p) => p.shiftHue(hShift));
this.updateAnchorPairs();
}
};
var { p5 } = globalThis;
if (p5) {
console.info("p5 detected, adding poline to p5 prototype");
const poline = new Poline();
p5.prototype.poline = poline;
const polineColors = () => poline.colors.map(
(c) => `hsl(${Math.round(c[0])},${c[1] * 100}%,${c[2] * 100}%)`
);
p5.prototype.polineColors = polineColors;
p5.prototype.registerMethod("polineColors", p5.prototype.polineColors);
globalThis.poline = poline;
globalThis.polineColors = polineColors;
}
return __toCommonJS(src_exports);
})();