简体   繁体   中英

Choosing colours based on IP address

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?

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 eg 21 or 53 and see what makes most sense.)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM