| 1 | package Bio::Gonzales::Util::Graphics::Color::Generator; | ||
| 2 | |||
| 3 | use Mouse; | ||
| 4 | use Data::Dumper; | ||
| 5 | |||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | use Carp; | ||
| 9 | use List::MoreUtils qw/zip/; | ||
| 10 | |||
| 11 | use 5.010; | ||
| 12 | use SVG; | ||
| 13 | our $VERSION = '0.058'; # VERSION | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Bio::Gonzales::Util::Common::Visual::Color::Generator - generate distinguishable colors in RGB format | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use Bio::Gonzales::Util::Common::Visual::Color::Generator; | ||
| 22 | |||
| 23 | my $generator = Bio::Gonzales::Util::Common::Visual::Color::Generator->new; | ||
| 24 | my $colors = $generator->generate(30); #number of colors as argument | ||
| 25 | |||
| 26 | for my $c (@{$colors}) { | ||
| 27 | say "R: $c->[0], G: $c->[1], B: $c->[2]"; | ||
| 28 | } | ||
| 29 | |||
| 30 | =head1 SUBROUTINES/METHODS | ||
| 31 | =cut | ||
| 32 | |||
| 33 | has _patterns => ( | ||
| 34 | is => 'rw', | ||
| 35 | default => sub { | ||
| 36 | [ [0], [1], [2], [ 0, 1 ], [ 0, 2 ], [ 1, 2 ], ]; | ||
| 37 | } | ||
| 38 | ); | ||
| 39 | |||
| 40 | =head2 generate($number_of_colors) | ||
| 41 | |||
| 42 | Generate array of a given number color triplets. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub generate { | ||
| 47 | my ( $self, $number ) = @_; | ||
| 48 | |||
| 49 | my $start = 255; | ||
| 50 | |||
| 51 | my @colors; | ||
| 52 | for ( my $i = 0; $i < $number; $i++ ) { | ||
| 53 | if ( $i % 6 == 0 && $i > 0 ) { | ||
| 54 | if ( $i % 18 == 0 ) { | ||
| 55 | $start += 2 * int($start); | ||
| 56 | } else { | ||
| 57 | $start = int( ( $start + 1 ) / 2 ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | my @color = ( 0, 0, 0 ); | ||
| 62 | map { $color[$_] = $start } @{ $self->_patterns->[ $i % 6 ] }; | ||
| 63 | push @colors, \@color; | ||
| 64 | } | ||
| 65 | return \@colors; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub generate_as_hex { | ||
| 69 | my ( $self, $number ) = @_; | ||
| 70 | |||
| 71 | my $colors = $self->generate($number); | ||
| 72 | |||
| 73 | my @colors = map { "#" . sprintf "%02x%02x%02x", @{$_} } @{$colors}; | ||
| 74 | |||
| 75 | return @colors; | ||
| 76 | } | ||
| 77 | |||
| 78 | =head2 generate_as_string($number_of_colors) | ||
| 79 | |||
| 80 | Generate string "RRR GGG BBB" version from a given number of colors | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub generate_as_string { | ||
| 85 | my ( $self, $number ) = @_; | ||
| 86 | |||
| 87 | my $colors = $self->generate($number); | ||
| 88 | |||
| 89 | my @colors = map { join " ", @{$_} } @{$colors}; | ||
| 90 | return @colors; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub create_legend { | ||
| 94 | my ( $self, $args ) = @_; | ||
| 95 | |||
| 96 | my $a = $args; | ||
| 97 | if ( ref $args eq 'ARRAY' ) { | ||
| 98 | my @colors = $self->generate_as_hex( scalar @{$args} ); | ||
| 99 | $a = { zip @{$args}, @colors }; | ||
| 100 | } | ||
| 101 | |||
| 102 | my $num_elements = keys %{$a}; | ||
| 103 | |||
| 104 | my $height = ( 30 + 10 ) * $num_elements - 10; | ||
| 105 | my $width = 500; | ||
| 106 | my $svg = SVG->new( width => $width, height => $height ); | ||
| 107 | my $i = 0; | ||
| 108 | while ( my ( $group_id, $color ) = each %{$a} ) { | ||
| 109 | my $y_offset = $i * ( 30 + 10 ); | ||
| 110 | my $text1 = $svg->text( id => "text_$i", x => 45, y => $y_offset + 15 )->cdata($group_id); | ||
| 111 | my $tag = $svg->rectangle( x => 0, y => $y_offset, width => 40, height => 30, id => "rect_$i", fill => $color ); | ||
| 112 | $i++; | ||
| 113 | |||
| 114 | } | ||
| 115 | return $svg->xmlify; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | __END__ | ||
| 120 | |||
| 121 | =head1 AUTHOR | ||
| 122 | |||
| 123 | jw bargsten, C<< <joachim.bargsten at wur.nl> >> | ||
| 124 | |||
| 125 | =cut |