@macrostrat/column-components
Version:
React rendering primitives for stratigraphic columns
70 lines (69 loc) • 1.98 kB
JavaScript
import Color from "../Color.js";
import "../io/lab/index.js";
import scale from "./scale.js";
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) {
[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) {
[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) {
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) {
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;
};
const bezier$1 = (colors) => {
const f = bezier(colors);
f.scale = () => scale(f);
return f;
};
export {
bezier$1 as default
};
//# sourceMappingURL=bezier.js.map