generating nice colors automagically

Replies:

  • None.

Parents:

  • None.
Here's a quick note on what is probably the least important but
most fun thing I worked on this week:

I made some improvements to the fogo archive message pages: now
replies are included inline below each message, and each message
is surrounded by a border whose color varies depending on the
sender's email address. (I wanted to choose colors this way
instead of randomly so each user would have their own color --
I think xchat does this for IRC nicks)

samples:
http://impressive.net/archives/fogo/[email protected]
http://impressive.net/archives/fogo/[email protected]

The fun part was coming up with an algorithm that chooses
aesthetically pleasing colors: not too bright and not too dark,
but not too drab either.

Here is the code I came up with; feedback/suggestions welcome:

 sub user_color {
 # calculate a hex html color for the given input (e.g. an email address)

   my $text = shift;

   use Digest::MD5 qw(md5_hex);

   # make sure color is not too dark or too light (map 0-255 to 170-234)
   my @hexbits = split(//,substr( md5_hex($text), 0, 6 ));
   my $h1 = 170 + hex($hexbits[0].$hexbits[1]) / 4;
   my $h2 = 170 + hex($hexbits[2].$hexbits[3]) / 4;
   my $h3 = 170 + hex($hexbits[4].$hexbits[5]) / 4;

   # skew one of (r,g,b) to make the colors more colorful
   # (while still choosing colors deterministically, i.e. no rand())
   my $r = $h1 % 3;
   $h1 -= $h1/6 if $r == 0;
   $h2 -= $h2/6 if $r == 1;
   $h3 -= $h3/6 if $r == 2;

   return sprintf("%02x%02x%02x", $h1, $h2, $h3);

 }

(I'm sure there are much more elegant ways to do this)

On a related note, I discovered via Gravatar [1] these cool
autogenerated user icons called Identicons [2]:

   So what is an Identicon? It’s a randomly generated assortment
   of shapes that is specific to a commenter’s email (or if you
   prefer IP address). Identicons allow visual representations of
   commenters without requiring any external sites or user
   interactions. With 40 possible shapes (about 70 with inversions)
   in 3 possible positions, around 8000 distinguishable colors
   and four different rotations for each part, there should be
   several billion possible shape combinations [...]

These are also available in monster form [3] -- I am impressed
with the quality and variety of these autogenerated monsters
(scroll down to the bottom to see them)

I will probably add gravatars to my archives at some point.

[1] http://en.gravatar.com/
[2] http://scott.sherrillmix.com/blog/blogger/wp_identicon/
[3] http://scott.sherrillmix.com/blog/blogger/wp_monsterid/

--
Gerald Oskoboiny <[email protected]>
http://impressive.net/people/gerald/


HURL: fogo mailing list archives, maintained by Gerald Oskoboiny