utils.js (811B)
1 export function multiplyAlpha(pixel) { 2 return pixel.map((channel, i) => { 3 // Pass the alpha channel through unchanged 4 if (i === 3) return channel; 5 // Otherwise, multiply by alpha 6 return channel * pixel[3]; 7 }); 8 } 9 10 export function unmultiplyAlpha(pixel) { 11 return pixel.map((channel, i) => { 12 // Pass the alpha channel through unchanged 13 if (i === 3) return channel; 14 // Avoid divide-by-zero 15 if (pixel[3] === 0) return channel; 16 // Divide by alpha 17 return channel / pixel[3]; 18 }); 19 } 20 21 export function clamp01(value) { 22 if (value < 0) return 0; 23 if (value > 1) return 1; 24 return value; 25 } 26 27 const toPercent = (num) => `${num * 100}%`; 28 export const toCSSColor = (pixel) => 29 `rgb(${toPercent(pixel[0])} ${toPercent(pixel[1])} ${toPercent(pixel[2])} / ${ 30 pixel[3] 31 })`;