How can I generate the opposite color according to current color?

前端 未结 11 639
北海茫月
北海茫月 2020-11-28 01:53

I\'m trying to create a color opposite of current color. I mean if current color is black, then I need to generate white.

Actually I have a text

11条回答
  •  时光取名叫无心
    2020-11-28 02:40

    Simple and elegant.

    function invertHex(hex) {
      return (Number(`0x1${hex}`) ^ 0xFFFFFF).toString(16).substr(1).toUpperCase()
    }
    
    invertHex('00FF00'); // Returns FF00FF
    

提交回复
热议问题