UNPKG

@tokens-studio/graph-engine

Version:

An execution engine to handle Token Studios generators and resolvers

22 lines 863 B
import Color from 'colorjs.io'; export function flattenAlpha(foreground, background) { // Decompose the foreground color to RGBA const [r1, g1, b1] = foreground.to('srgb').coords; // Default alpha to 1 if undefined const a1 = foreground.alpha || 1; if (a1 === 1) { return foreground; } else { // Decompose the background color to RGB (assume opaque) const [r2, g2, b2] = background.to('srgb').coords; // Perform alpha blending formula const alpha = 1 - a1; const r = r2 * alpha + r1 * a1; const g = g1 * a1 + g2 * alpha; const b = b1 * a1 + b2 * alpha; // Convert blended color back to Color object and then to hex string const resultColor = new Color('sRGB', [r, g, b]); return resultColor.to('srgb'); } } //# sourceMappingURL=flattenAlpha.js.map