[Cycles] Color Re-Mapping >2 Colors?

I don’t really know how to explain this so I will explain it with a picture:

http://i.imgbox.com/m6XDu3hK.png

I know how to change the colours of an image which has only two colours-- mixing the diffuse by the factor of the image (or an alpha).

But how do I do it with 3 or more colours? In the above picture, let’s say I want to change those pattern colours to something different, but individually…

http://www.pasteall.org/blend/41247

Ok I figured out it’s easy to do with one diffuse:

But what if I wanted a different shader for each colour?

Ok I figured it out (finally!)… haha.

But I can’t figure out how to do 4…

You could expand over something like this:


This noodle expects a grayscale image or, at least, an image where only the red channel is relevant, leaving you the other channels to encode other data.
It has to have all possible values mapped to the range 0…1 so, regardless of the number of different materials/colors you want to use on the final result, one has to be mapped to 0.0, other to 1.0, and the rest evenly spaced in between.