Created
January 19, 2018 18:26
-
-
Save joshtronic/d778de050de984eac01cfe694553b50f to your computer and use it in GitHub Desktop.
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
<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