# Generating hex color from a pair of numbers

I want to generate a color, in hexadecimal from a pair of two numbers. These numbers could be of any value. But the resulting number (in decimal) must be between 0 and 16777215

Its not the most common of things so I don't believe there would be some algorithm already out there for it.

The color needs to come out as unique for every pair, forgetting about order of the numbers, for example 456, 782 would be the same color as 782, 456, there would have to be a limit of the values in this pair otherwise as Henning Makholm has said, there wouldn't be a finite amount of possibilities.

The number shouldn't be a random number and it should be able to be produced again using the same pair.

Pseudo code is always good if you are into programming :)

Thanks.

-
You can't achieve uniqueness here, since there are only 16777216 different possible outputs but infinitely many different possible inputs. For example, apply the pigeonhole principle to the inputs $$\{(0,0),(1,1),(2,2),\ldots,(16777216,16777216)\}$$ There are not enough possible outputs to give each of these inputs its own output. –  Henning Makholm Feb 3 '13 at 3:03
Uniqueness up to a maximum pair value? –  FabianCook Feb 3 '13 at 3:08

If the inputs are always between 0 and 4095, you can just do $$f(x,y) = \max(x,y) + 4096\cdot\min(x,y)$$ since $\sqrt{16777216}=4096$.

With a more involved packing you'd be able to handle up about $\sqrt 2 \cdot 4096 \approx 5800$ different input values, but I'm guessing that would not be worth the trouble.

If you want small changes to the input to make clearly visible differences in the output color, you can run the resulting 24-bit number through a shift-xor/add cascade in the shape of these hashers. Beware that you'll need to mask the intermediate value to 24 bits at least before each right shift (and of course at the end) in order to preserve uniqueness. On the other hand, you can pick any constants you want without losing uniqueness -- the worst that can happen is that the inputs will get less thoroughly mixed than the could otherwise be.

-