-
Notifications
You must be signed in to change notification settings - Fork 546
/
bezier.js
86 lines (82 loc) · 2.54 KB
/
bezier.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
//
// interpolates between a set of colors uzing a bezier spline
//
// @requires utils lab
import Color from '../Color.js';
import '../io/lab/index.js';
import scale from './scale.js';
// nth row of the pascal triangle
const binom_row = function (n) {
let row = [1, 1];
for (let i = 1; i < n; i++) {
let newrow = [1];
for (let j = 1; j <= row.length; j++) {
newrow[j] = (row[j] || 0) + row[j - 1];
}
row = newrow;
}
return row;
};
const bezier = function (colors) {
let I, lab0, lab1, lab2;
colors = colors.map((c) => new Color(c));
if (colors.length === 2) {
// linear interpolation
[lab0, lab1] = colors.map((c) => c.lab());
I = function (t) {
const lab = [0, 1, 2].map((i) => lab0[i] + t * (lab1[i] - lab0[i]));
return new Color(lab, 'lab');
};
} else if (colors.length === 3) {
// quadratic bezier interpolation
[lab0, lab1, lab2] = colors.map((c) => c.lab());
I = function (t) {
const lab = [0, 1, 2].map(
(i) =>
(1 - t) * (1 - t) * lab0[i] +
2 * (1 - t) * t * lab1[i] +
t * t * lab2[i]
);
return new Color(lab, 'lab');
};
} else if (colors.length === 4) {
// cubic bezier interpolation
let lab3;
[lab0, lab1, lab2, lab3] = colors.map((c) => c.lab());
I = function (t) {
const lab = [0, 1, 2].map(
(i) =>
(1 - t) * (1 - t) * (1 - t) * lab0[i] +
3 * (1 - t) * (1 - t) * t * lab1[i] +
3 * (1 - t) * t * t * lab2[i] +
t * t * t * lab3[i]
);
return new Color(lab, 'lab');
};
} else if (colors.length >= 5) {
// general case (degree n bezier)
let labs, row, n;
labs = colors.map((c) => c.lab());
n = colors.length - 1;
row = binom_row(n);
I = function (t) {
const u = 1 - t;
const lab = [0, 1, 2].map((i) =>
labs.reduce(
(sum, el, j) =>
sum + row[j] * u ** (n - j) * t ** j * el[i],
0
)
);
return new Color(lab, 'lab');
};
} else {
throw new RangeError('No point in running bezier with only one color.');
}
return I;
};
export default (colors) => {
const f = bezier(colors);
f.scale = () => scale(f);
return f;
};