Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Generating Visually Distinct Colors

by iblech (Friar)
on Mar 23, 2005 at 13:39 UTC ( [id://441758]=note: print w/replies, xml ) Need Help??


in reply to Generating Visually Distinct Colors

japhy, you might find Coloring IRC logs nicely interesting, too.

This is the algorithm my IRC bot iblechbot uses, too (screenshot).

First, a raw IRC log is read to find out which nicks were online (and talked) at the same time. This information is used to build a interference graph. Then, using the graph, all nicks get distict colors.

Because of the interference graph, the colors used are reduced to a minimum:

  • 10:00 - A joins - color: 1/3 # notice: three colors needed
  • 10:03 - B joins - color: 2/3
  • 10:05 - C joins - color: 3/3
  • (Time passes...)
  • 20:00 - D joins - color: 1/2 # notice: only two colors used here
  • 20:02 - E joins - color: 2/2

Using the colornum/total_num_of_colors information, the actual color pair (foreground, background) is built:

sub calc_color { my ($i, $ncolors) = @_; $ncolors = 1 if $ncolors == 0; # No division /0. my $a = 0.95; # tune these for the starting and ending concentra +tions of R,G,B my $b = 0.5; my $rgb = [ [$a,$b,$b], [$b,$a,$b], [$b,$b,$a], [$a,$a,$b], [$a,$b,$ +a], [$b,$a,$a] ]; my $rgbmax = 125; # tune these two for the outmost ranges of colou +r depth my $rgbmin = 240; my $n = $i % @$rgb; my $m = $rgbmin + ($rgbmax - $rgbmin) * ($ncolors - $i) / $ncolors; my @c = map { $rgb->[$n][$_] * $m } 0 .. 2; my $g = $c[0] * .3 + $c[1] * .59 + $c[2] * .11; my $f = $g > 127 ? "#000000" : "#ffffff"; my $h = sprintf "#%02x%02x%02x", @c; ($f, $h); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://441758]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found