1

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. :-)

Gregology
  • 1,625
  • 2
  • 18
  • 32
  • Are you looking for a unique color or a random one? Does it have to be unique within a page or on the entire site? – Louis-Philippe Huberdeau Jan 11 '12 at 01:42
  • Add the first two ip parts and divide with 3, you get the choice what channel will be zero(0-170 red, 171-340 green, rest blue). Last two parts equals the leftover channels. – Corubba Jan 11 '12 at 01:44
  • ip does not equal user. 1 user can use several ip's. 1 ip can be several users. you need a proper user management system. –  Jan 11 '12 at 02:10
  • Thanks everyone for your help. I ended up choosing the colours in HSV and then converting them to RGB with a function found [here](http://stackoverflow.com/a/3597447/1142113). The final product is a really simple text dump site [dump.gho.st](http://dump.gho.st). Thanks again everyone! – Gregology Jan 12 '12 at 03:16

3 Answers3

2

Are you really limiting yourself to just six "base" colors?

255 255 0
255 0 255
0 255 255

0 0 255
0 255 0
255 0 0

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:

b1, b2, b3, b4 = <split the four bytes from an IP address>
index = (b1 * 17 + (b2 * 17 + (b3 * 17 + b4))) % 6

(I just picked the multiplier 17 out of the air -- its binary representation is 10001, 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. 21 or 53 and see what makes most sense.)

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Thanks Sarnold, HSV or HSL seem like the best option. I think I'll choose the color in HSV and then convert it to RGB. – Gregology Jan 11 '12 at 04:59
  • Incidentally, this _simplistic_ approach breaks when your software is working with [IPv6](http://en.wikipedia.org/wiki/Ipv6) addresses. Perhaps a string-based hashing method would work better... – sarnold Jan 12 '12 at 00:08
1

Although this won't give you a result where one of {R,G,B} is always 0, a HSL representation might look good. As an example, let hue be a decimal value from 0 to 1, defined by

(float)(octet[0] + octet[1] << 8 + octet[2] << 16 + octet[3] << 24) / (2^32-1)

, where each octet[i] is an unsigned byte, and ^ is exponentiation). And then perhaps set lightness and saturation by hand, as per your preference. Just an idea!

As an added bonus, this makes fading the colours easy (just subtract some portion of "time" from saturation/lightness).

Dillon Cower
  • 111
  • 1
  • 2
-2

are you using a database to store the relations? you could always grab the user's IP Address

<?php
    function userIP(){
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $userIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $userIp=$_SERVER['REMOTE_ADDR'];
        }
        return trim($userIp);
    }
?>

Then use the function to set a usable variable of the IP:

<?php
    $Users_IP_address = userIP();
?> 

once you have that, you can assign a color that isn't in use and save the association for future reference.

Silvertiger
  • 1,680
  • 2
  • 19
  • 32
  • Also, you're looking for math to auto assign colors, but if you're looking to have only the 27 options of 255 (2 of the 3 RGB), how many users do you expect to have under this system? gotta have that variable if you want viable math. It works great if you have 3 users :) – Silvertiger Jan 11 '12 at 01:42
  • `X-Forwarded-For` is trivial to spoof. You should not reference it unless you *know* you are behind a reverse proxy and the data within can be trusted. – Charles Jan 11 '12 at 01:44