Created
July 9, 2019 23:53
-
-
Save jelbourn/79c473aab404abcd36dc7cfaa7ac02ac to your computer and use it in GitHub Desktop.
Alpha blending calculator
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 characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<p> | |
<label> | |
Base color | |
<input id="base"> | |
</label> | |
</p> | |
<p> | |
<label> | |
Overlay color | |
<input id="overlay"> | |
</label> | |
</p> | |
<p> | |
<label> | |
Overlay alpha | |
<input id="alpha" type="number"> | |
</label> | |
</p> | |
<p> | |
<button type="button"> | |
Compute | |
</button> | |
</p> | |
<p id="result"></p> | |
<script id="jsbin-javascript"> | |
const button = document.querySelector('button'); | |
button.addEventListener('click', compute); | |
const baseInput = document.getElementById('base'); | |
const overlayInput = document.getElementById('overlay'); | |
const alphaInput = document.getElementById('alpha'); | |
function getRgbFromHex(hex) { | |
if (hex[0] === '#') { | |
hex = hex.slice(1); | |
} | |
console.log({ | |
r: parseInt(hex[0] + hex[1], 16), | |
g: parseInt(hex[2] + hex[3], 16), | |
b: parseInt(hex[4] + hex[5], 16), | |
}) | |
return { | |
r: parseInt(hex[0] + hex[1], 16), | |
g: parseInt(hex[2] + hex[3], 16), | |
b: parseInt(hex[4] + hex[5], 16), | |
}; | |
} | |
function blend(baseValue, overlayValue, alpha) { | |
return Math.round((overlayValue * alpha) + (baseValue * (1 - alpha))); | |
} | |
function compute() { | |
const base = getRgbFromHex(baseInput.value); | |
const overlay = getRgbFromHex(overlayInput.value); | |
const alpha = alphaInput.valueAsNumber; | |
const blended = { | |
r: blend(base.r, overlay.r, alpha), | |
g: blend(base.g, overlay.g, alpha), | |
b: blend(base.b, overlay.b, alpha), | |
}; | |
document.getElementById('result').textContent = | |
'#' + | |
blended.r.toString(16) + | |
blended.g.toString(16) + | |
blended.b.toString(16); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const button = document.querySelector('button'); | |
button.addEventListener('click', compute); | |
const baseInput = document.getElementById('base'); | |
const overlayInput = document.getElementById('overlay'); | |
const alphaInput = document.getElementById('alpha'); | |
function getRgbFromHex(hex) { | |
if (hex[0] === '#') { | |
hex = hex.slice(1); | |
} | |
console.log({ | |
r: parseInt(hex[0] + hex[1], 16), | |
g: parseInt(hex[2] + hex[3], 16), | |
b: parseInt(hex[4] + hex[5], 16), | |
}) | |
return { | |
r: parseInt(hex[0] + hex[1], 16), | |
g: parseInt(hex[2] + hex[3], 16), | |
b: parseInt(hex[4] + hex[5], 16), | |
}; | |
} | |
function blend(baseValue, overlayValue, alpha) { | |
return Math.round((overlayValue * alpha) + (baseValue * (1 - alpha))); | |
} | |
function compute() { | |
const base = getRgbFromHex(baseInput.value); | |
const overlay = getRgbFromHex(overlayInput.value); | |
const alpha = alphaInput.valueAsNumber; | |
const blended = { | |
r: blend(base.r, overlay.r, alpha), | |
g: blend(base.g, overlay.g, alpha), | |
b: blend(base.b, overlay.b, alpha), | |
}; | |
document.getElementById('result').textContent = | |
'#' + | |
blended.r.toString(16) + | |
blended.g.toString(16) + | |
blended.b.toString(16); | |
} | |
</script></body> | |
</html> |
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 characters
const button = document.querySelector('button'); | |
button.addEventListener('click', compute); | |
const baseInput = document.getElementById('base'); | |
const overlayInput = document.getElementById('overlay'); | |
const alphaInput = document.getElementById('alpha'); | |
function getRgbFromHex(hex) { | |
if (hex[0] === '#') { | |
hex = hex.slice(1); | |
} | |
console.log({ | |
r: parseInt(hex[0] + hex[1], 16), | |
g: parseInt(hex[2] + hex[3], 16), | |
b: parseInt(hex[4] + hex[5], 16), | |
}) | |
return { | |
r: parseInt(hex[0] + hex[1], 16), | |
g: parseInt(hex[2] + hex[3], 16), | |
b: parseInt(hex[4] + hex[5], 16), | |
}; | |
} | |
function blend(baseValue, overlayValue, alpha) { | |
return Math.round((overlayValue * alpha) + (baseValue * (1 - alpha))); | |
} | |
function compute() { | |
const base = getRgbFromHex(baseInput.value); | |
const overlay = getRgbFromHex(overlayInput.value); | |
const alpha = alphaInput.valueAsNumber; | |
const blended = { | |
r: blend(base.r, overlay.r, alpha), | |
g: blend(base.g, overlay.g, alpha), | |
b: blend(base.b, overlay.b, alpha), | |
}; | |
document.getElementById('result').textContent = | |
'#' + | |
blended.r.toString(16) + | |
blended.g.toString(16) + | |
blended.b.toString(16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Thanks!