This image contains every 18-bit color exactly once

image

I've been reminded of this age-old stack overflow coding challenge the other day and wanted to figure out how one could use fragment shaders to transform any input image into an image that uses every $n$-bit color exactly once.

The approach I took was to map every pixel from the input image into a ${(2^n)}^3$-cube, sort the pixels inside the cube by their RGB value respectively, and replace every pixel in the image by the final coordinate of the corresponding pixel inside the cube.

With this approach, it's fairly easy to see that every color must only be used exactly once and it doesn't take too long to render, but the results strongly depend on how colorful the input image was in the first place. The base image I used to get the above result was a WIP screenshot I took while working on this shader, so the image was already close to containing every color.

Here are some more results:

image imageimage image