Skip to content

Instantly share code, notes, and snippets.

@SeinopSys
Created March 20, 2021 22:58
Show Gist options
  • Save SeinopSys/1bcf41a6127a7cafe7466284b5142f3d to your computer and use it in GitHub Desktop.
Save SeinopSys/1bcf41a6127a7cafe7466284b5142f3d to your computer and use it in GitHub Desktop.
JavaScrip hex color mix function
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