In my PHP (v5.2.17) script, I want to select a unique colour for the current user’s entries, based on their IP address. I don’t want to map the colour values from the hex codes, because I also want to fade the colours of each entry over time. The colour must always have one of the RGB values set to zero (it’s like a set of bright, primary colours).
Is there a clever mathematical solution to do this?
I’d greatly appreciate if any math genuises reading this would share some insights. 🙂
Are you really limiting yourself to just six “base” colors?
I presume you’re going to apply a linear function to these colors to try to fade them out. This won’t necessarily look as good as you think it might — RGB as a representation isn’t very linear. You can cheaply approximate a better “linear” representation by using an HSV or HSL representation instead. They surely aren’t perfect but it will feel a little more natural than RGB.
As for mapping the IP address to a color, you could store these color combinations in an array and pick among the six elements by using a simple hash function. Something like this might be sufficient:
(I just picked the multiplier
17out of the air — its binary representation is10001, which means the bits of each byte in the address get “smeared” over each other. There might be better values. Once you’ve gotten a few colors selected and a handful of IP addresses you can try changing the multiplier to e.g.21or53and see what makes most sense.)