Search code examples
javascripthexbit-manipulationxorbitwise-xor

Can Bitwise complement of a hex color code always return 6 characters?


In Javascript, is there a way to make the bitwise complement of a hex color always return a 6 character string?

For instance, ('0xff0537' ^ '0xffffff').toString(16) generates a 4 character string of 'fac8'.

However, a ('0x00adeb' ^ '0xffffff').toString(16) returns a 6 character string of 'ff5214'.


Solution

  • I ended up solving this issue as so:

    return ('000000' + (('0xffffff' ^ '0x00adeb').toString(16))).slice(-6);