Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding alpha functionality to bezier interpolation #228

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/generator/bezier.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,29 @@ const bezier = function(colors) {
// 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');
const linearInterpolation = (x0, x1) => x0 + (t * (x1 - x0));
const lab = ([0, 1, 2].map((i) => linearInterpolation(lab0[i], lab1[i])));
const alpha = linearInterpolation(colors[0].alpha(), colors[1].alpha());
return new Color(lab, 'lab').alpha(alpha);
};
} 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');
const quadraticInterpolation = (x0, x1, x2) => ((1-t)*(1-t) * x0) + (2 * (1-t) * t * x1) + (t * t * x2)
const lab = ([0, 1, 2].map((i) => quadraticInterpolation(lab0[i], lab1[i], lab2[i])));
const alpha = quadraticInterpolation(colors[0].alpha(), colors[1].alpha(), colors[2].alpha());
return new Color(lab, 'lab').alpha( alpha );
};
} 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');
const cubicInterpolation = (x0, x1, x2, x3) => ((1-t)*(1-t)*(1-t) * x0) + (3 * (1-t) * (1-t) * t * x1) + (3 * (1-t) * t * t * x2) + (t*t*t * x3);
const lab = ([0, 1, 2].map((i) => cubicInterpolation(lab0[i], lab1[i], lab2[i], lab3[i])));
const alpha = cubicInterpolation(colors[0].alpha(), colors[1].alpha(), colors[2].alpha(), colors[3].alpha());
return new Color(lab, 'lab').alpha(alpha);
};
} else if (colors.length === 5) {
const I0 = bezier(colors.slice(0, 3));
Expand Down
32 changes: 16 additions & 16 deletions test/bezier.js → test/bezier.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ vows

'simple two color linear interpolation': {
topic: {
f: chroma.bezier(['white', 'black'])
f: chroma.bezier(['white', chroma('black').alpha(0)])
},
'starts from white'(topic) { assert.equal(topic.f(0).hex(), '#ffffff'); },
'ends in black'(topic) { assert.equal(topic.f(1).hex(), '#000000'); },
'center is grey'(topic) { assert.equal(topic.f(0.5).hex(), '#777777'); }
'ends in transparent black'(topic) { assert.equal(topic.f(1).hex(), '#00000000'); },
'center is transluscent grey'(topic) { assert.equal(topic.f(0.5).hex(), '#77777780'); }
},

'three color quadratic bezier interpolation': {
topic: {
f: chroma.bezier(['white', 'red', 'black'])
f: chroma.bezier(['white', chroma('red').alpha(.5), chroma('black').alpha(0)])
},
'starts from white'(topic) { assert.equal(topic.f(0).hex(), '#ffffff'); },
'ends in black'(topic) { assert.equal(topic.f(1).hex(), '#000000'); },
'center is a greyish red'(topic) { assert.equal(topic.f(0.5).hex(), '#c45c44'); }
'ends in transparent black'(topic) { assert.equal(topic.f(1).hex(), '#00000000'); },
'center is a transluscent greyish red'(topic) { assert.equal(topic.f(0.5).hex(), '#c45c4480'); }
},

'four color cubic bezier interpolation': {
topic: {
f: chroma.bezier(['white', 'yellow', 'red', 'black'])
f: chroma.bezier(['white', chroma('yellow').alpha(1/3), chroma('red').alpha(2/3), chroma('black').alpha(0)])
},
'starts from white'(topic) { assert.equal(topic.f(0).hex(), '#ffffff'); },
'ends in black'(topic) { assert.equal(topic.f(1).hex(), '#000000'); },
'1st quarter'(topic) { assert.equal(topic.f(0.25).hex(), '#ffe085'); },
'center'(topic) { assert.equal(topic.f(0.5).hex(), '#e69735'); },
'3rd quarter'(topic) { assert.equal(topic.f(0.75).hex(), '#914213'); }
'ends in transparent black'(topic) { assert.equal(topic.f(1).hex(), '#00000000'); },
'1st quarter'(topic) { assert.equal(topic.f(0.25).hex(), '#ffe085a7'); },
'center'(topic) { assert.equal(topic.f(0.5).hex(), '#e6973580'); },
'3rd quarter'(topic) { assert.equal(topic.f(0.75).hex(), '#91421358'); }
},

'five color diverging quadratic bezier interpolation': {
topic: {
f: chroma.bezier(['darkred', 'orange', 'snow', 'lightgreen', 'royalblue'])
f: chroma.bezier(['darkred', chroma('orange').alpha(.75), chroma('snow').alpha(.5), chroma('lightgreen').alpha(.25), chroma('royalblue').alpha(0)])
},
'starts from darkred'(topic) { assert.equal(topic.f(0).hex(), '#8b0000'); },
'ends in royalblue'(topic) { assert.equal(topic.f(1).hex(), '#4169e1'); },
'center is snow'(topic) { assert.equal(topic.f(0.5).hex(), '#fffafa'); },
'1st quarter'(topic) { assert.equal(topic.f(0.25).hex(), '#e9954e'); },
'3rd quarter'(topic) { assert.equal(topic.f(0.75).hex(), '#a6cfc1'); }
'ends in transparent royalblue'(topic) { assert.equal(topic.f(1).hex(), '#4169e100'); },
'center is snow'(topic) { assert.equal(topic.f(0.5).hex(), '#fffafa80'); },
'1st quarter'(topic) { assert.equal(topic.f(0.25).hex(), '#e9954ebf'); },
'3rd quarter'(topic) { assert.equal(topic.f(0.75).hex(), '#a6cfc140'); }
},

'using bezier in a chroma.scale': {
Expand Down