Last active
June 9, 2022 17:02
-
-
Save leonadler/19e6876f1c5e572532fa9c70f288a92c to your computer and use it in GitHub Desktop.
Revisions
-
leonadler revised this gist
Jun 9, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,10 +5,10 @@ * blendColors('255 0 0 / 20%', '60 60 40 / 30%', '128 96 44 / 60%') * // -> {r: 128, g: 84, b: 40, a: 0.7764705882352941} */ function overlayColors(...colors) { let canvas; if (typeof OffscreenCanvas === 'function') { canvas = new OffscreenCanvas(1, 1); } else { canvas = document.createElement('canvas'); canvas.width = canvas.height = 1; -
leonadler revised this gist
Jun 9, 2022 . No changes.There are no files selected for viewing
-
leonadler created this gist
Jun 9, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ /** * Helper to test how alpha-transparent colors are layered in the browser. * * @example * blendColors('255 0 0 / 20%', '60 60 40 / 30%', '128 96 44 / 60%') * // -> {r: 128, g: 84, b: 40, a: 0.7764705882352941} */ function overlayColors(...colors){ let canvas; if (typeof OffscreenCanvas === 'function') { canvas = = new OffscreenCanvas(1, 1); } else { canvas = document.createElement('canvas'); canvas.width = canvas.height = 1; } const ctx = canvas.getContext('2d'); colors.forEach(col => { ctx.fillStyle = `rgb(${col})`; ctx.rect(0, 0, 1, 1) ctx.fill(); }); const [r,g,b,a] = ctx.getImageData(0,0,1,1).data; return {r, g, b, a: (a / 255) } }