Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function blendColors() {
- var args = [].prototype.slice.call(arguments);
- var base = [0, 0, 0, 0];
- var mix;
- var added;
- while (added = args.shift()) {
- if (typeof added[3] === 'undefined') {
- added[3] = 1;
- }
- // check if both alpha channels exist.
- if (base[3] && added[3]) {
- mix = [0, 0, 0, 0];
- // alpha
- mix[3] = 1 - (1 - added[3]) * (1 - base[3]);
- // red
- mix[0] = Math.round((added[0] * added[3] / mix[3]) + (base[0] * base[3] * (1 - added[3]) / mix[3]));
- // green
- mix[1] = Math.round((added[1] * added[3] / mix[3]) + (base[1] * base[3] * (1 - added[3]) / mix[3]));
- // blue
- mix[2] = Math.round((added[2] * added[3] / mix[3]) + (base[2] * base[3] * (1 - added[3]) / mix[3]));
- } else if (added) {
- mix = added;
- } else {
- mix = base;
- }
- base = mix;
- }
- return mix;
- }
- // outputs [85, 170, 0, 0.75]
- console.log(blendColors(
- [0, 0, 0, 0],
- [255, 255, 255, 0],
- [255, 0, 0, .5],
- [0, 255, 0, .5]
- ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement