0

I have searched Stack Overflow for color inversion algorithms. Most responses involve separating channel values and calculating the respective differences of FF and said values, e.g.

    HexColor = 3DB5B1
    InvRed = FF - 3D = C2
    InvGreen = FF - B5 = 4A
    InvBlue = FF - B1 = 4E

Or less frequently, calculating the bitwise exclusive OR of FFFFFF and HexColor, e.g.

    FFFFFF XOR 3DB5B1 = C24A4E

Isn't (FFFFFF XOR HexColor) equivalent to (FFFFFF - HexColor)? That is.

    InvHexColor = FFFFFF - HexColor

And one need not separate the channels or use a bitwise operator.

I cannot find an example that fails, but I lack a proof.

It is not clear to me how I might use the answers of Bitwise XORing two numbers results in sum or difference of the numbers. Can anyone suggest how I can apply which identity or analysis to FFFFFF and an arbitrary six digit hexadecimal color code?

Tom
  • 1
  • 1
  • just try (there are only 256 cases). If you use unsigned it is equivalent – Giacomo Catenazzi Mar 06 '23 at 09:51
  • `not clear to me how I might use the answers of Bitwise XORing` - it gives you the exact formula in the [top answer](https://stackoverflow.com/a/21157987/11683): "*there's the lesser known identity `x - y == (x ^ y) - ((~x & y) << 1)`*". In your case `x` is `ffffff`, so `~x` is 0, which makes the subtrahend zero leaving you with `x - y == (x ^ y)`. So yes, you can use it that way, provided you always subtract from ffffff and you treat it as unsigned. – GSerg Mar 06 '23 at 13:52

0 Answers0