]> git.donarmstrong.com Git - biopieces.git/commitdiff
added color routine to Draw.pm
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 9 Feb 2010 17:29:06 +0000 (17:29 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 9 Feb 2010 17:29:06 +0000 (17:29 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@871 74ccb610-7750-0410-82ae-013aeee3265d

code_perl/Maasha/BGB/Draw.pm

index 907b8173e7e65258e8f59389eed6fbdc61703e77..ab4498f53823ce8e97b55799eec63130f7d738b9 100644 (file)
@@ -34,6 +34,7 @@ use Data::Dumper;
 use Cairo;
 use Pango;
 use MIME::Base64;
+use POSIX;
 
 use vars qw( @ISA @EXPORT );
 
@@ -135,9 +136,9 @@ sub palette
         [  70, 170, 130 ],
         [ 130, 170,  50 ],
     ];  
-    
+
     $color = $palette->[ $i ];
-    
+
     map { $_ /= 255 } @{ $color };
     
     return $color;
@@ -186,6 +187,50 @@ sub base64_png
 }
 
 
+sub get_distinct_colors
+{
+    # Martin A. Hansen, November 2003.
+
+    # returns a number of distinct colors
+
+    my ( $num_colors
+       ) = @_;
+
+    # returns triplet of colors [ 0, 255, 127 ]
+
+    my ( $num_discrete, @color_vals, $c, @colors, $r_idx, $g_idx, $b_idx, $i );
+
+    $num_discrete = POSIX::ceil( $num_colors ** ( 1 / 3 ) );
+
+    @color_vals = map {
+        $c = 1 - ($_ / ($num_discrete - 1) );
+        $c < 0 ? 0 : ($c > 1 ? 1 : $c);
+    } 0 .. $num_discrete;
+
+    ( $r_idx, $g_idx, $b_idx ) = ( 0, 0, 0 );
+
+    foreach $i ( 1 .. $num_colors )
+    {
+        push @colors, [ @color_vals [ $r_idx, $g_idx, $b_idx ] ];
+        
+        if ( ++$b_idx >= $num_discrete )
+        {
+            if ( ++$g_idx >= $num_discrete ) {
+                $r_idx = ( $r_idx + 1 ) % $num_discrete;
+            }
+            
+            $g_idx %= $num_discrete;
+        }
+
+        $b_idx %= $num_discrete;
+    }
+
+    @colors = map { [ int( $_->[ 0 ] * 255), int( $_->[ 1 ] * 255), int( $_->[ 2 ] * 255 ) ] } @colors;
+    return wantarray ? @colors : \@colors;
+}
+
+
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 1;