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!


In reply to Creative presentation algorithm: IP to a color by Your Mother

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.