| 1 | package Bio::Graphics::Browser2::OptionPick; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Bio::Graphics::Browser2::OptionPick -- Pick options | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use Bio::Graphics::Browser2::OptionPick; | ||
| 10 | my $picker = Bio::Graphics::Browser2::OptionPick->new($render_object); | ||
| 11 | |||
| 12 | print start_html(), | ||
| 13 | start_form(), | ||
| 14 | $picker->('bgcolor','orange','blue'), | ||
| 15 | end_form(), | ||
| 16 | end_html(); | ||
| 17 | |||
| 18 | =head1 Author | ||
| 19 | |||
| 20 | Copyright 2007 Cold Spring Harbor Laboratory. | ||
| 21 | |||
| 22 | This package and its accompanying libraries is free software; you can | ||
| 23 | redistribute it and/or modify it under the terms of the GPL (either | ||
| 24 | version 1, or at your option, any later version) or the Artistic | ||
| 25 | License 2.0. Refer to LICENSE for the full license text. In addition, | ||
| 26 | please see DISCLAIMER.txt for disclaimers of warranty. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | |||
| 31 | use strict; | ||
| 32 | use Bio::Graphics::Panel; | ||
| 33 | use Bio::Graphics::Glyph::heat_map; | ||
| 34 | |||
| 35 | # my @GRADIENT = | ||
| 36 | # ('white','#D3D3D3','#A9A9A9','gray','black','red','yellow','blue','green','orange','magenta','cyan', | ||
| 37 | # '#FFCCCC','#FFAAAA','#FF9999','#EE7777','#EE5555','#EE3333','#EE2222','#DD0000','#DD2222','#EE3344','#EE5566', | ||
| 38 | # '#EE6699','#EE88BB','#EEAADD','#EEBBFF','#EEBBFF','#EEBBFF','#DDBBFF','#DDBBFF','#CCBBFF','#CCCCFF','#CCCCFF', | ||
| 39 | # '#AAAAEE','#8899EE','#7777DD','#5566CC','#3355BB','#2233BB','#0022AA','#2244AA','#3366BB','#5588CC','#7799CC', | ||
| 40 | # '#88BBDD','#AADDDD','#CCFFEE','#BBFFCC','#AAFFAA','#99EE88','#77EE66','#66EE44','#55EE22','#44EE00','#66EE11', | ||
| 41 | # '#77EE11','#99EE22','#AAEE22','#CCEE33','#DDEE33','#FFFF44'); | ||
| 42 | my $heatmap = 'Bio::Graphics::Glyph::heat_map'; | ||
| 43 | my $bgp = 'Bio::Graphics::Panel'; | ||
| 44 | |||
| 45 | my @GRADIENT = sort { | ||
| 46 | my ($r1,$g1,$b1) = $bgp->color_name_to_rgb($a); | ||
| 47 | my ($r2,$g2,$b2) = $bgp->color_name_to_rgb($b); | ||
| 48 | my ($h1,$s1,$v1) = $heatmap->RGBtoHSV($r1,$g1,$b1); | ||
| 49 | my ($h2,$s2,$v2) = $heatmap->RGBtoHSV($r2,$g2,$b2); | ||
| 50 | return $h1<=>$h2 || $s1<=>$s2 || $v1<=>$v2; | ||
| 51 | } grep {!/gradient/} Bio::Graphics::Panel->color_names; | ||
| 52 | my %LABELS = (); | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my $self = shift; | ||
| 56 | my $render_object = shift; | ||
| 57 | return bless {render_object => $render_object},ref $self || $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub translate { | ||
| 61 | my $self = shift; | ||
| 62 | my $str = shift; | ||
| 63 | return $self->{render_object}->tr($str) || $str; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub render_object { shift->{render_object} } | ||
| 67 | |||
| 68 | |||
| 69 | sub color_pick { | ||
| 70 | my $self = shift; | ||
| 71 | my ($form_name,$default_color,$current_color,$class) = @_; | ||
| 72 | $class ||= 'color_picker'; | ||
| 73 | |||
| 74 | my ($bgcolor,$fontcolor); | ||
| 75 | my $dynamic = $self->translate('DYNAMIC_VALUE'); | ||
| 76 | |||
| 77 | if (ref($default_color)) { # CODE or something else horrible | ||
| 78 | $default_color = $dynamic; | ||
| 79 | $bgcolor = 'white'; | ||
| 80 | $fontcolor = 'black'; | ||
| 81 | } else { | ||
| 82 | $bgcolor = $default_color; | ||
| 83 | my ($r,$g,$b) = Bio::Graphics::Panel->color_name_to_rgb($default_color); | ||
| 84 | my $avg = ($r+$g+$b)/3; | ||
| 85 | $fontcolor = $avg > 128 ? 'black' : 'white'; | ||
| 86 | } | ||
| 87 | |||
| 88 | $current_color ||= $default_color; | ||
| 89 | |||
| 90 | my ($current_bg,$current_fg); | ||
| 91 | if ($current_color eq $dynamic) { | ||
| 92 | $current_bg = 'white'; | ||
| 93 | $current_fg = 'black'; | ||
| 94 | } else { | ||
| 95 | $current_bg = $current_color; | ||
| 96 | my ($r,$g,$b) = Bio::Graphics::Panel->color_name_to_rgb($current_color); | ||
| 97 | my $avg = ($r+$g+$b)/3; | ||
| 98 | $current_fg = $avg > 128 ? 'black' : 'white'; | ||
| 99 | } | ||
| 100 | |||
| 101 | my $menu = qq(<select name="$form_name" | ||
| 102 | class="$class", | ||
| 103 | style="background-color:$current_bg;color:$current_fg" | ||
| 104 | onChange="var s=this.options[this.selectedIndex];Element.extend(s);this.style.background=s.getStyle('background-color');this.style.color=s.style.color">\n); | ||
| 105 | |||
| 106 | my $default = $self->translate('DEFAULT'); | ||
| 107 | |||
| 108 | # add default color | ||
| 109 | my $selected = $default_color eq $current_color ? 'selected' : ''; | ||
| 110 | $menu .= qq(<option value="$default_color" style="color:$fontcolor;background-color:$bgcolor"$selected>$default_color $default</option>\n); | ||
| 111 | |||
| 112 | my $index = 0; | ||
| 113 | for my $color (@GRADIENT) { | ||
| 114 | next if $color eq $default_color; | ||
| 115 | my $selected = $color eq $current_color ? 'selected' : ''; | ||
| 116 | my $description = $color; | ||
| 117 | my ($r,$g,$b) = Bio::Graphics::Panel->color_name_to_rgb($color); | ||
| 118 | my $avg = ($r+$g+$b)/3; | ||
| 119 | my $fontcolor = $avg > 128 ? 'black' : 'white'; | ||
| 120 | $menu .= qq(<option value="$color" bgcolor="$color" style="background-color:$color;color:$fontcolor"$selected>$description</option>\n); | ||
| 121 | } | ||
| 122 | $menu .= "</select\n>"; | ||
| 123 | return $menu; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub popup_menu { | ||
| 127 | my $self = shift; | ||
| 128 | my %args = @_; | ||
| 129 | my $name = $args{-name}; | ||
| 130 | my $current = $args{-current}; | ||
| 131 | my $default = $args{-default}; | ||
| 132 | my $values = $args{-values}; | ||
| 133 | my $id = $args{-id}; | ||
| 134 | my $class = $args{-class}; | ||
| 135 | my $labels = $args{-labels} || {}; | ||
| 136 | my $scripts = $args{-scripts} || {}; | ||
| 137 | |||
| 138 | my $dynamic = $self->translate('DYNAMIC_VALUE'); | ||
| 139 | my %seen; | ||
| 140 | my @values = grep {!$seen{$_}++} map { ref($_) || /^CODE\(/ ? $dynamic : $_ } @$values; | ||
| 141 | |||
| 142 | $current ||= $default; | ||
| 143 | my $def = $self->translate('DEFAULT'); | ||
| 144 | my $def_label = $labels->{$default} || $default; | ||
| 145 | my %labels = (%$labels,$default => "$def_label $def"); | ||
| 146 | my @extra = $id ? (-id => $id) : (); | ||
| 147 | push @extra,(-class=>$class) if $class; | ||
| 148 | |||
| 149 | return CGI::popup_menu(-name => $name, | ||
| 150 | -values => \@values, | ||
| 151 | -default => $current, | ||
| 152 | -labels => \%labels, | ||
| 153 | %$scripts, | ||
| 154 | @extra, | ||
| 155 | -override=>1); | ||
| 156 | } | ||
| 157 | |||
| 158 | 1; |