plus-lighter.js (1062B)
1 import { clamp01, multiplyAlpha, unmultiplyAlpha } from "./utils.js"; 2 3 export function plusLighter(pixels) { 4 if (pixels.length === 1) return pixels[0]; 5 6 return pixels.reduce((destination, source) => { 7 const premultipliedSource = multiplyAlpha(source); 8 const premultipliedDestination = multiplyAlpha(destination); 9 const premultipliedResult = premultipliedDestination.map((channel, i) => 10 clamp01(channel + premultipliedSource[i]) 11 ); 12 return unmultiplyAlpha(premultipliedResult); 13 }); 14 } 15 16 export const tests = [ 17 // Each test is a list of colors to composite. 18 // Each color is [r, g, b, a], unmultiplied, in the range 0-1. 19 [ 20 [1, 0, 0, 0.5], 21 [0, 0, 1, 0.5], 22 ], 23 [ 24 [1, 0, 0, 0.25], 25 [0, 0, 1, 0.25], 26 ], 27 [ 28 [0.5, 0, 0, 0.5], 29 [0, 0, 1, 0.5], 30 ], 31 // Test clamping 32 [ 33 [1, 0, 0, 1], 34 [0, 0, 1, 1], 35 ], 36 // Test more than two elements 37 [ 38 [1, 0, 0, 0.25], 39 [0, 0, 1, 0.25], 40 [0, 1, 0, 0.25], 41 [0.5, 0.4, 0.25, 0.25], 42 ], 43 // Test a single element 44 [ 45 [0.5, 0, 0, 0.25], 46 ], 47 ];