http://qs1969.pair.com?node_id=305198

pg has asked for the wisdom of the Perl Monks concerning the following question:

I run a chat room on my web site. To make the content more attractive, and also to visually confirm to the user that the content has refreshed when they click "listen", I change the background color each time randomly.

Now if the foreground color stays the same all the time, it could be too close to the background color, and the user will not be able to read. So I come up with this piece of code to generate two colors that (I believe) are well contrasted to each other:

sub random_color { my ($r, $g, $b) = (int(rand(256)), int(rand(256)), int(rand(256))) +; my $color1 = sprintf("#%02x%02x%02x", $r, $g, $b); my $color2 = sprintf("#%02x%02x%02x", ($r + 128) % 256, ($g + 128) + % 256, ($b + 128) % 256); return ($color1, $color2); }
and I use it in this way:
my ($c1, $c2) = random_color(); $msg .= $cgi->table({-border => 1, bgcolor => $c1, sty +le => "Color: $c2;"}, $cgi->Tr($table_data));

So far, it seems to me work very well, but I am not sure whether this really gurantees the well-contrastness. Maybe some one can enlighten me, or come up with some other interesting way to generate the colors, and give a different look and feel.