| 1 | |||
| 2 | # color selection page | ||
| 3 | |||
| 4 | package App::GUI::Juliagraph::Frame::Tab::Color; | ||
| 5 | 1 | ▶ | use base qw/Wx::Panel/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | use v5.12; | ||
| 7 | use warnings; | ||
| 8 | use Wx; | ||
| 9 | use App::GUI::Juliagraph::Frame::Panel::ColorBrowser; | ||
| 10 | use App::GUI::Juliagraph::Frame::Panel::ColorPicker; | ||
| 11 | use App::GUI::Juliagraph::Frame::Panel::ColorSetPicker; | ||
| 12 | use App::GUI::Juliagraph::Widget::ColorDisplay; | ||
| 13 | use App::GUI::Juliagraph::Widget::PositionMarker; | ||
| 14 | use Graphics::Toolkit::Color qw/color/; | ||
| 15 | |||
| 16 | our $default_color_def = $App::GUI::Juliagraph::Frame::Panel::ColorSetPicker::default_color; | ||
| 17 | my $default_settings = { 1=> 'black', 2=> 'red', 3=> 'orange', 4 => 'blue', | ||
| 18 | tilt => 0, delta_S => 0, delta_L => 0 }; | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my ( $class, $parent, $config, $size ) = @_; | ||
| 22 | my $self = $class->SUPER::new( $parent, -1); | ||
| 23 | |||
| 24 | $self->{'call_back'} = sub {}; | ||
| 25 | $self->{'config'} = $config; | ||
| 26 | $self->{'color_count'} = $size; # number of displayed colors | ||
| 27 | $self->{'active_color_count'} = 4; # nr of currently used colors, overwritten on init | ||
| 28 | $self->{'current_color_nr'} = 0; # index starts from 0 | ||
| 29 | $self->{'display_size'} = 30; | ||
| 30 | |||
| 31 | $self->{'used_colors'} = [ color('blue')->gradient( to => 'red', steps => $self->{'active_color_count'}) ]; | ||
| 32 | $self->{'used_colors'}[$_] = color( $default_color_def ) for $self->{'active_color_count'} .. $self->{'color_count'}-1; | ||
| 33 | $self->{'color_marker'} = [ map { App::GUI::Juliagraph::Widget::PositionMarker->new | ||
| 34 | ($self, $self->{'display_size'}, 20, $_, '', $default_color_def) } 0 .. $self->{'color_count'}-1 ]; | ||
| 35 | $self->{'color_display'}[$_] = App::GUI::Juliagraph::Widget::ColorDisplay->new | ||
| 36 | ($self, $self->{'display_size'}-2, $self->{'display_size'}, | ||
| 37 | $_, $self->{'used_colors'}[$_]->values(as => 'hash') ) for 0 .. $self->{'color_count'}-1; | ||
| 38 | $self->{'color_marker'}[$_-1]->SetToolTip("color $_, to change (marked by arrow - crosses mark currently passive colors)") for 1 .. $self->{'color_count'}-1; | ||
| 39 | $self->{'color_display'}[$_-1]->SetToolTip("color $_, to change (marked by arrow - crosses mark currently passive colors)") for 1 .. $self->{'color_count'}-1; | ||
| 40 | $self->{'color_marker'}[$size-1]->SetToolTip("lost color, often background color, shown where values do not converge"); | ||
| 41 | $self->{'color_display'}[$size-1]->SetToolTip("last color, often background color, shown where values do not converge"); | ||
| 42 | |||
| 43 | $self->{'label'}{'color_set_store'} = Wx::StaticText->new($self, -1, 'Color Set Store' ); | ||
| 44 | $self->{'label'}{'color_set_funct'} = Wx::StaticText->new($self, -1, 'Colors Set Function' ); | ||
| 45 | $self->{'label'}{'used_colors'} = Wx::StaticText->new($self, -1, 'Currently Used Colors' ); | ||
| 46 | $self->{'label'}{'selected_color'} = Wx::StaticText->new($self, -1, 'Selected Color' ); | ||
| 47 | $self->{'label'}{'color_store'} = Wx::StaticText->new($self, -1, 'Color Store' ); | ||
| 48 | |||
| 49 | $self->{'widget'}{'tilt'} = Wx::ComboBox->new( $self, -1, 1, [-1,-1], [80, -1], [ -6, -5, -4, -3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 4, 5, 6]); | ||
| 50 | $self->{'widget'}{'delta_S'} = Wx::TextCtrl->new( $self, -1, 0, [-1,-1], [50,-1], &Wx::wxTE_RIGHT); | ||
| 51 | $self->{'widget'}{'delta_L'} = Wx::TextCtrl->new( $self, -1, 0, [-1,-1], [50,-1], &Wx::wxTE_RIGHT); | ||
| 52 | |||
| 53 | $self->{'button'}{'gradient'} = Wx::Button->new( $self, -1, 'Gradient', [-1,-1], [ 75, 17] ); | ||
| 54 | $self->{'button'}{'complement'} = Wx::Button->new( $self, -1, 'Complement', [-1,-1], [100, 17] ); | ||
| 55 | $self->{'button'}{'left'} = Wx::Button->new( $self, -1, '<', [-1,-1], [30, 17] ); | ||
| 56 | $self->{'button'}{'right'} = Wx::Button->new( $self, -1, '>', [-1,-1], [30, 17] ); | ||
| 57 | $self->{'button'}{'left'}->SetToolTip("Move currently selected color to the left."); | ||
| 58 | $self->{'button'}{'right'}->SetToolTip("Move currently selected color to the left."); | ||
| 59 | $self->{'button'}{'gradient'}->SetToolTip("Create gradient between first and current color. Adheres to tilt settings."); | ||
| 60 | $self->{'button'}{'complement'}->SetToolTip("Create color set from first up to current color as complementary colors. Adheres to both delta values."); | ||
| 61 | $self->{'widget'}{'tilt'}->SetToolTip("tilt of gradient (0 = linear) and also of gray scale"); | ||
| 62 | $self->{'widget'}{'delta_S'}->SetToolTip("max. satuaration deviation when computing complement colors ( -100 .. 100)"); | ||
| 63 | $self->{'widget'}{'delta_L'}->SetToolTip("max. lightness deviation when computing complement colors ( -100 .. 100)"); | ||
| 64 | |||
| 65 | |||
| 66 | $self->{'picker'} = App::GUI::Juliagraph::Frame::Panel::ColorPicker->new( $self, $config->get_value('color') ); | ||
| 67 | $self->{'setpicker'} = App::GUI::Juliagraph::Frame::Panel::ColorSetPicker->new( $self, $config->get_value('color_set'), $self->{'color_count'}); | ||
| 68 | |||
| 69 | $self->{'browser'} = App::GUI::Juliagraph::Frame::Panel::ColorBrowser->new( $self, 'selected', {red => 0, green => 0, blue => 0} ); | ||
| 70 | $self->{'browser'}->SetCallBack( sub { $self->set_current_color( $_[0] ) }); | ||
| 71 | |||
| 72 | Wx::Event::EVT_LEFT_DOWN( $self->{'color_display'}[$_], sub { $self->set_current_color_nr( $_[0]->get_nr ) }) for 0 .. $self->{'color_count'}-1; | ||
| 73 | Wx::Event::EVT_LEFT_DOWN( $self->{'color_marker'}[$_], sub { $self->set_current_color_nr( $_[0]->get_nr ) }) for 0 .. $self->{'color_count'}-1; | ||
| 74 | |||
| 75 | Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'gradient'}, sub { | ||
| 76 | my @c = $self->get_all_colors; | ||
| 77 | my @new_colors = $c[0]->gradient( to => $c[ $self->{'current_color_nr'} ], in => 'RGB', | ||
| 78 | steps => $self->{'current_color_nr'}+1, | ||
| 79 | tilt => $self->{'widget'}{'tilt'}->GetValue ); | ||
| 80 | $self->set_all_colors( @new_colors ); | ||
| 81 | }); | ||
| 82 | Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'complement'}, sub { | ||
| 83 | my @c = $self->get_all_colors; | ||
| 84 | my @new_colors = $c[ $self->{'current_color_nr'} ]->complement( steps => $self->{'current_color_nr'}+1, | ||
| 85 | target => { | ||
| 86 | s => $self->{'widget'}{'delta_S'}->GetValue, | ||
| 87 | l => $self->{'widget'}{'delta_L'}->GetValue | ||
| 88 | },); | ||
| 89 | $self->set_all_colors( @new_colors ); | ||
| 90 | }); | ||
| 91 | Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'left'}, sub { | ||
| 92 | my $pos = $self->get_current_color_nr; | ||
| 93 | my @colors = $self->get_all_colors; | ||
| 94 | my $selected = splice @colors, $pos, 1; | ||
| 95 | $pos--; | ||
| 96 | $pos = $self->{'color_count'} - 1 if $pos < 0; | ||
| 97 | splice @colors, $pos, 0, $selected; | ||
| 98 | $self->set_all_colors( @colors ); | ||
| 99 | $self->set_current_color_nr( $pos ); | ||
| 100 | }); | ||
| 101 | Wx::Event::EVT_BUTTON( $self, $self->{'button'}{'right'}, sub { | ||
| 102 | my $pos = $self->get_current_color_nr; | ||
| 103 | my @colors = $self->get_all_colors; | ||
| 104 | my $selected = splice @colors, $pos, 1; | ||
| 105 | $pos++; | ||
| 106 | $pos = 0 if $pos >= $self->{'color_count'}; | ||
| 107 | splice @colors, $pos, 0, $selected; | ||
| 108 | $self->set_all_colors( @colors ); | ||
| 109 | $self->set_current_color_nr( $pos ); | ||
| 110 | }); | ||
| 111 | |||
| 112 | my $std_attr = &Wx::wxALIGN_LEFT | &Wx::wxGROW ; | ||
| 113 | my $all_attr = $std_attr | &Wx::wxALL | &Wx::wxALIGN_CENTER_HORIZONTAL | &Wx::wxALIGN_CENTER_VERTICAL; | ||
| 114 | my $next_attr = &Wx::wxGROW | &Wx::wxTOP | &Wx::wxALIGN_CENTER_HORIZONTAL; | ||
| 115 | |||
| 116 | my $f_sizer = Wx::BoxSizer->new(&Wx::wxHORIZONTAL); | ||
| 117 | $f_sizer->AddSpacer( 10 ); | ||
| 118 | $f_sizer->Add( $self->{'button'}{'gradient'}, 0, $all_attr, 5 ); | ||
| 119 | $f_sizer->Add( $self->{'widget'}{'tilt'}, 0, $all_attr, 5 ); | ||
| 120 | $f_sizer->AddSpacer( 20 ); | ||
| 121 | $f_sizer->Add( $self->{'button'}{'complement'},0, $all_attr, 5 ); | ||
| 122 | $f_sizer->Add( $self->{'widget'}{'delta_S'}, 0, $all_attr, 5 ); | ||
| 123 | $f_sizer->Add( $self->{'widget'}{'delta_L'}, 0, $all_attr, 5 ); | ||
| 124 | $f_sizer->AddSpacer( 20 ); | ||
| 125 | $f_sizer->Add( $self->{'button'}{'left'}, 0, $all_attr, 5 ); | ||
| 126 | $f_sizer->Add( $self->{'button'}{'right'}, 0, $all_attr, 5 ); | ||
| 127 | $f_sizer->Add( 0, 1, &Wx::wxEXPAND | &Wx::wxGROW); | ||
| 128 | |||
| 129 | my $state_sizer = $self->{'state_sizer'} = Wx::BoxSizer->new(&Wx::wxHORIZONTAL); # $self->{'plate_sizer'}->Clear(1); | ||
| 130 | $state_sizer->AddSpacer( 12 ); | ||
| 131 | my @option_sizer; | ||
| 132 | for my $nr (0 .. $self->{'color_count'}-1){ | ||
| 133 | #$state_sizer->AddSpacer( 1 ); | ||
| 134 | $option_sizer[$nr] = Wx::BoxSizer->new( &Wx::wxVERTICAL ); | ||
| 135 | $option_sizer[$nr]->AddSpacer( 2 ); | ||
| 136 | $option_sizer[$nr]->Add( $self->{'color_display'}[$nr],0, $all_attr, 3); | ||
| 137 | $option_sizer[$nr]->Add( $self->{'color_marker'}[$nr], 0, $all_attr, 3); | ||
| 138 | $state_sizer->Add( $option_sizer[$nr], 0, $all_attr, 6); | ||
| 139 | #$state_sizer->AddSpacer( 1 ); | ||
| 140 | } | ||
| 141 | $state_sizer->Insert( 11, Wx::StaticLine->new( $self, -1,[-1,-1],[-1,-1], &Wx::wxLI_VERTICAL), 0, &Wx::wxGROW); | ||
| 142 | $state_sizer->Add( 0, 1, &Wx::wxEXPAND | &Wx::wxGROW); | ||
| 143 | |||
| 144 | my $sizer = Wx::BoxSizer->new(&Wx::wxVERTICAL); | ||
| 145 | $sizer->AddSpacer( 10 ); | ||
| 146 | $sizer->Add( $self->{'label'}{'color_set_store'}, 0, &Wx::wxALIGN_CENTER_HORIZONTAL, 0); | ||
| 147 | $sizer->Add( $self->{'setpicker'}, 0, $all_attr, 10); | ||
| 148 | $sizer->Add( Wx::StaticLine->new( $self, -1), 0, $all_attr, 0); | ||
| 149 | $sizer->AddSpacer( 10 ); | ||
| 150 | $sizer->Add( $self->{'label'}{'color_set_funct'}, 0, &Wx::wxALIGN_CENTER_HORIZONTAL, 0); | ||
| 151 | $sizer->Add( $f_sizer, 0, $all_attr, 10); | ||
| 152 | $sizer->AddSpacer( 2 ); | ||
| 153 | $sizer->Add( Wx::StaticLine->new( $self, -1), 0, $all_attr, 0); | ||
| 154 | $sizer->AddSpacer( 10 ); | ||
| 155 | $sizer->Add( $self->{'label'}{'used_colors'}, 0, &Wx::wxALIGN_CENTER_HORIZONTAL, 0); | ||
| 156 | $sizer->Add( $state_sizer, 0, $all_attr, 5); | ||
| 157 | $sizer->AddSpacer( 5 ); | ||
| 158 | $sizer->Add( Wx::StaticLine->new( $self, -1), 0, $all_attr, 0); | ||
| 159 | $sizer->AddSpacer( 10 ); | ||
| 160 | $sizer->Add( $self->{'label'}{'selected_color'}, 0, &Wx::wxALIGN_CENTER_HORIZONTAL, 10); | ||
| 161 | $sizer->Add( $self->{'browser'}, 0, $next_attr, 10); | ||
| 162 | $sizer->Add( Wx::StaticLine->new( $self, -1), 0, $next_attr, 8); | ||
| 163 | $sizer->AddSpacer( 10 ); | ||
| 164 | $sizer->Add( $self->{'label'}{'color_store'}, 0, &Wx::wxALIGN_CENTER_HORIZONTAL, 10); | ||
| 165 | $sizer->Add( $self->{'picker'}, 0, $std_attr| &Wx::wxLEFT, 10); | ||
| 166 | $sizer->Add( 0, 1, &Wx::wxEXPAND | &Wx::wxGROW); | ||
| 167 | |||
| 168 | $self->SetSizer( $sizer ); | ||
| 169 | $self->set_active_color_count( $self->{'active_color_count'} ); | ||
| 170 | $self->set_current_color_nr ( $self->{'current_color_nr'} ); | ||
| 171 | $self->init; | ||
| 172 | $self; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub SetCallBack { | ||
| 176 | my ($self, $code) = @_; | ||
| 177 | return unless ref $code eq 'CODE'; | ||
| 178 | $self->{'call_back'} = $code; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub set_active_color_count { | ||
| 182 | my ($self, $count) = @_; | ||
| 183 | return unless defined $count and $count > 1 and $count < 11; | ||
| 184 | $self->{'active_color_count'} = $count; | ||
| 185 | $self->{'color_marker'}[$_]->set_state('passive') for 0 .. $self->{'active_color_count'}-1; | ||
| 186 | $self->{'color_marker'}[$_]->set_state('disabled') for $self->{'active_color_count'} .. $self->{'color_count'}-1; | ||
| 187 | $self->{'color_marker'}[ $self->{'current_color_nr'} ]->set_state('active'); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub get_current_color_nr { $_[0]->{'current_color_nr'} } | ||
| 191 | sub set_current_color_nr { | ||
| 192 | my ($self, $nr) = @_; | ||
| 193 | $nr //= $self->{'current_color_nr'}; | ||
| 194 | my $old_marker_state = ($self->{'current_color_nr'} < $self->{'active_color_count'}) ? 'passive' : 'disabled'; | ||
| 195 | $self->{'color_marker'}[$self->{'current_color_nr'}]->set_state( $old_marker_state ); | ||
| 196 | $self->{'color_marker'}[ $nr ]->set_state('active'); | ||
| 197 | $self->{'current_color_nr'} = $nr; | ||
| 198 | $self->{'browser'}->set_data( $self->{'used_colors'}[$self->{'current_color_nr'}]->values(as => 'hash'), 'silent' ); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub init { $_[0]->set_settings( $default_settings ) } | ||
| 202 | |||
| 203 | sub set_settings { | ||
| 204 | my ($self, $settings) = @_; | ||
| 205 | return unless ref $settings eq 'HASH' and exists $settings->{'tilt'}; | ||
| 206 | $self->{'widget'}{$_}->SetValue( $settings->{$_} // $default_settings->{$_} ) for qw/tilt delta_S delta_L/; | ||
| 207 | $self->set_all_colors( grep {defined $_} map {$settings->{$_}} 1 .. $self->{'color_count'} ); | ||
| 208 | } | ||
| 209 | |||
| 210 | sub get_state { $_[0]->get_settings } | ||
| 211 | sub get_settings { | ||
| 212 | my ($self) = @_; | ||
| 213 | my $data = { | ||
| 214 | tilt => $self->{'widget'}{'tilt'}->GetValue, | ||
| 215 | delta_S => $self->{'widget'}{'delta_S'}->GetValue, | ||
| 216 | delta_L => $self->{'widget'}{'delta_L'}->GetValue, | ||
| 217 | }; | ||
| 218 | $data->{$_} = $self->{'used_colors'}[$_-1]->values(as => 'hex_string') for 1 .. $self->{'color_count'}; | ||
| 219 | $data; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub get_current_color { | ||
| 223 | my ($self) = @_; | ||
| 224 | $self->{'used_colors'}[$self->{'current_color_nr'}]; | ||
| 225 | } | ||
| 226 | |||
| 227 | sub set_current_color { | ||
| 228 | my ($self, $color) = @_; | ||
| 229 | return unless ref $color eq 'HASH'; | ||
| 230 | $self->{'used_colors'}[$self->{'current_color_nr'}] = color( $color ); | ||
| 231 | $self->{'color_display'}[$self->{'current_color_nr'}]->set_color( $color ); | ||
| 232 | $self->{'browser'}->set_data( $color ); | ||
| 233 | $self->{'call_back'}->( 'color' ); # update whole app | ||
| 234 | } | ||
| 235 | |||
| 236 | sub set_all_colors { | ||
| 237 | my ($self, @colors) = @_; | ||
| 238 | return unless @colors; | ||
| 239 | for my $i (0 .. $#colors){ | ||
| 240 | my $temp = $colors[ $i ]; | ||
| 241 | $colors[ $i ] = color( $temp ) if ref $temp ne 'Graphics::Toolkit::Color'; | ||
| 242 | return "value number $i: $temp is no color" if ref $colors[ $i ] ne 'Graphics::Toolkit::Color'; | ||
| 243 | } | ||
| 244 | $self->{'used_colors'} = [@colors]; | ||
| 245 | $self->{'used_colors'}[$_] = color( $default_color_def ) for @colors .. $self->{'color_count'}-1; | ||
| 246 | $self->{'color_display'}[$_]->set_color( $self->{'used_colors'}[$_]->values(as => 'hash') ) for 0 .. $self->{'color_count'}-1; | ||
| 247 | $self->set_current_color_nr; | ||
| 248 | $self->{'call_back'}->( 'color' ); # update whole app | ||
| 249 | } | ||
| 250 | |||
| 251 | sub get_all_colors { @{$_[0]->{'used_colors'}} } | ||
| 252 | sub get_active_colors { @{$_[0]->{'used_colors'}}[ 0 .. $_[0]->{'active_color_count'} - 1] } | ||
| 253 | |||
| 254 | sub update_config { | ||
| 255 | my ($self) = @_; | ||
| 256 | $self->{'config'}->set_value('color', $self->{'picker'}->get_config); | ||
| 257 | $self->{'config'}->set_value('color_set', $self->{'setpicker'}->get_config); | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | 1; | ||