TITLE: Function satisfying $x = f(f(x))$ and $x \not= f(x)$
QUESTION [7 upvotes]: Is there a function that would satisfy the following conditions?:
$\forall x \in X, x = f(f(x))$ and $x \not= f(x)$,
where the set $X$ is the set of all triplets $(x_1,x_2,x_3)$ with $x_i \in \{0,1,\ldots,255\}$.
I would like to find a function that will have as an input RGB color values (triplets) and return the original color after two applications of the function.

REPLY [3 votes]: If you are doing these calculations on a computer and can get away from 
insisting on interpreting $x_1$, $x_2$, $x_3$ as integers in the range
$[0,255]$, consider thinking of $x_1$, $x_2$, $x_3$ as eight-bit bytes
or vectors of length $8$ over $\mathbb F_2$ to be a bit more formal about it.
Then, for any three bytes $a$, $b$, $c$ with at least one being nonzero,
$$f(x_1, x_2, x_3) = (x_1\oplus a, x_2\oplus b, x_3\oplus c)$$
has the desired properties that 
$$\begin{align*}
f(f(x_1, x_2, x_3)) &= f(x_1\oplus a, x_2\oplus b, x_3\oplus c)\\
&= (x_1\oplus a\oplus a, x_2\oplus b\oplus b, x_3\oplus c \oplus c)\\
&= (x_1, x_2, x_3),
\end{align*}$$
and $(x_1, x_2, x_3) \neq f(x_1, x_2, x_3)$.
As an added bonus (not necessarily important to
math.SE readers), the XOR operation on bytes is a 
machine-language instruction on most computers
and in some cases can be faster than the ADD instruction
which is often defined for full (multiple-byte) words only.  
Note that $f(x_1,x_2,x_3)=(255−x_1,255−x_2,255−x_3)$ as suggested
by @ofer is just 
$f(x_1,x_2,x_3) 
= (x_1 \oplus \mathbf 1, x_2\oplus \mathbf 1, x_3\oplus \mathbf 1)$
where $\mathbf 1 = (1,1,1,1,1,1,1,1)$ is the eight-bit all-ones byte.