Created
March 20, 2021 22:58
-
-
Save SeinopSys/1bcf41a6127a7cafe7466284b5142f3d to your computer and use it in GitHub Desktop.
JavaScrip hex color mix function
This file contains 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 mix = (hex1, hex2, percent) => { | |
const breakup = (hex) => hex.match(/[a-f0-d]{2}/ig).map(n => parseInt(n, 16)); | |
const rgb1 = breakup(hex1); | |
const rgb2 = breakup(hex2); | |
const mixed = rgb1.map((c, i) => Math.round((c * (1 - percent)) + (rgb2[i] * percent)).toString(16)); | |
return '#'+mixed.map(s => s.length < 2 ? '0'+s : s).join(''); | |
}; | |
console.log(mix('#000000','#ffffff', .2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment