Skip to content

Instantly share code, notes, and snippets.

@joshtronic
Created January 19, 2018 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshtronic/d778de050de984eac01cfe694553b50f to your computer and use it in GitHub Desktop.
Save joshtronic/d778de050de984eac01cfe694553b50f to your computer and use it in GitHub Desktop.
<input type="color" />
<button>Button Text</button>
<script>
document.addEventListener('DOMContentLoaded', () => {
document.querySelector('input').oninput = (e) => {
const color = e.target.value;
let [r, g, b] = color.substr(1).match(/.{1,2}/g);
let yiq = (
(parseInt(r, 16) * 299)
+ (parseInt(g, 16) * 587)
+ (parseInt(b, 16) * 114)
) / 1000;
document.querySelector('button').style.backgroundColor = color;
document.querySelector('button').style.color = (yiq >= 150 ? '#000' : '#fff');
};
}, false);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment