Skip to content

Instantly share code, notes, and snippets.

@jelbourn
Created July 9, 2019 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jelbourn/79c473aab404abcd36dc7cfaa7ac02ac to your computer and use it in GitHub Desktop.
Save jelbourn/79c473aab404abcd36dc7cfaa7ac02ac to your computer and use it in GitHub Desktop.
Alpha blending calculator
<!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>
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);
}
@sagemodeninja
Copy link

Nice! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment