Easy Hex Color Invert
let color = 0xFFFF00;
function toHexString(col) {
return '#' + ('000000' + col.toString(16)).substr(-6);
}
function onClick() {
// invert the color
color ^= 0xFFFFFF;
document.body.style.background = toHexString(color);
}
onClick();
document.addEventListener('click', onClick);
console.log('try a different initial color');
console.log('click anywhere to invert background...');
Easily invert a hex color. Expanding on yesterdays post – just one of many reasons you may want to work with colors in their integer form.