Your Mother has asked for the wisdom of the Perl Monks concerning the following question:
For an uncookied, no authentication, setup I'd like to color users by their IPs. Not as security or anything, just to distinguish them a bit. My style of problem solving in new domains is woefully brute-trial-and-error and since this seems fun and interesting, I'm putting it up.
I've played around with things like this:
sub xor_it_together { my @ip = shift =~ /(\d+)/g; my $code = join'^',@ip; print eval "$code"; } # xor_it_together('67.167.26.54') --> 200 # xor_it_together('127.0.0.1') --> 126
That could be okay, maybe, for a gray scale; rgb(128,128,128). But different rgb would be nice. So this next bit seems to be passable (a final version should probably add a reproducable random seed, like an IP digest, so that IPs can't be reverse engineered from the colors).
use List::Util "sum"; sub ip2rgb { my @ip = shift =~ /(\d+)/g; my $r = eval "@{[ join '^', @ip ]}"; my $g = sum(@ip) % 255; # left-overs my ( $b ) = sort @ip; # lowest of group print "$r, $g, $b"; } # ip2rgb '220.17.120.164' --> 17, 11, 120 # ip2rgb '127.0.0.1' --> 126, 128, 0
Any nicer or alternative ideas?
update (20050829-1914): I'm glad I got over my temerity about posting the question. All the answers are cool and I am learning something new from all of them. Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Creative presentation algorithm: IP to a color
by Zaxo (Archbishop) on Aug 29, 2005 at 01:18 UTC | |
by Argel (Prior) on Aug 31, 2005 at 00:09 UTC | |
|
Re: Creative presentation algorithm: IP to a color
by QM (Parson) on Aug 29, 2005 at 01:18 UTC | |
by eXile (Priest) on Aug 29, 2005 at 02:40 UTC | |
by Your Mother (Archbishop) on Aug 29, 2005 at 01:59 UTC | |
|
Re: Creative presentation algorithm: IP to a color
by Elijah (Hermit) on Aug 29, 2005 at 04:19 UTC | |
by eXile (Priest) on Aug 29, 2005 at 05:54 UTC | |
|
Re: Creative presentation algorithm: IP to a color
by Ven'Tatsu (Deacon) on Aug 29, 2005 at 15:27 UTC | |
|
Re: Creative presentation algorithm: IP to a color
by Codon (Friar) on Aug 29, 2005 at 17:33 UTC |