| 1 | package App::Codit::Plugins::Colors; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Codit::Plugins::Colors - plugin for App::Codit | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw( $VERSION ); | ||
| 12 | $VERSION = '0.19'; | ||
| 13 | |||
| 14 | require Tk::ColorPicker; | ||
| 15 | use Tie::Watch; | ||
| 16 | |||
| 17 | use base qw( Tk::AppWindow::BaseClasses::PluginJobs ); | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | Easily select and insert colors. | ||
| 22 | |||
| 23 | =head1 DETAILS | ||
| 24 | |||
| 25 | The Colors plugin lets you choose a color and insert it's hex value into your document. | ||
| 26 | |||
| 27 | You can select a color in RGB, CMY and HSV space. Whenever you select a color it is added to the Recent tab. | ||
| 28 | |||
| 29 | It allows you to specify color depths 4, 8, 12 and 16 bits per color. | ||
| 30 | |||
| 31 | You can pick a color from any place on the screen with the pick button. This does not work on Windows. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub new { | ||
| 36 | my $class = shift; | ||
| 37 | my $self = $class->SUPER::new(@_); | ||
| 38 | return undef unless defined $self; | ||
| 39 | |||
| 40 | my $page = $self->ToolRightPageAdd('Colors', 'fill-color', undef, 'Select and insert colors', 350); | ||
| 41 | |||
| 42 | my @padding = (-padx => 3, -pady => 3); | ||
| 43 | |||
| 44 | my $eframe = $page->Frame->pack(-fill => 'x'); | ||
| 45 | |||
| 46 | my $fframe = $eframe->Frame->pack(-side => 'left'); | ||
| 47 | |||
| 48 | my $picker; | ||
| 49 | my $color = ''; | ||
| 50 | my $entry = $fframe->Entry( | ||
| 51 | -textvariable => \$color, | ||
| 52 | )->pack(@padding, -fill => 'x'); | ||
| 53 | $entry->bind('<Key>', [$self, 'updateEntry']); | ||
| 54 | $self->{ENTRY} = $entry; | ||
| 55 | |||
| 56 | my $bframe = $fframe->Frame->pack(-fill => 'x'); | ||
| 57 | |||
| 58 | $bframe->Button( | ||
| 59 | -text => 'Insert', | ||
| 60 | -command => sub { | ||
| 61 | if ($picker->validate($color)) { | ||
| 62 | $self->cmdExecute('edit_insert', 'insert', $color); | ||
| 63 | $picker->historyAdd($picker->getHEX); | ||
| 64 | $picker->historyUpdate; | ||
| 65 | } | ||
| 66 | }, | ||
| 67 | )->pack(@padding, -side => 'left', -expand => 1, -fill => 'x'); | ||
| 68 | |||
| 69 | $bframe->Button( | ||
| 70 | -text => 'Copy', | ||
| 71 | -command => sub { | ||
| 72 | if ($picker->validate($color)) { | ||
| 73 | $self->clipboardClear; | ||
| 74 | $self->clipboardAppend($color); | ||
| 75 | $picker->historyAdd($picker->getHEX); | ||
| 76 | $picker->historyUpdate; | ||
| 77 | } | ||
| 78 | }, | ||
| 79 | )->pack(@padding, -side => 'left', -expand => 1, -fill => 'x'); | ||
| 80 | |||
| 81 | my $indicator = $eframe->Label( | ||
| 82 | -width => 4, | ||
| 83 | -relief => 'sunken', | ||
| 84 | -borderwidth => 2, | ||
| 85 | )->pack(@padding, -side => 'left', -expand => 1, -fill => 'both'); | ||
| 86 | $self->{INDICATOR} = $indicator; | ||
| 87 | |||
| 88 | $picker = $page->ColorPicker( | ||
| 89 | -depthselect => 1, | ||
| 90 | -notationselect => 1, | ||
| 91 | -historyfile => $self->extGet('ConfigFolder')->ConfigFolder . '/color_history', | ||
| 92 | -updatecall => ['updatePicker', $self], | ||
| 93 | )->pack(-padx => 2, -pady => 2, -expand => 1, -fill => 'both'); | ||
| 94 | $self->{PICKER} = $picker; | ||
| 95 | $self->jobStart('selection_check', 'SelectionCheck', $self); | ||
| 96 | return $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub _ent { | ||
| 100 | my ($self, $value) = @_; | ||
| 101 | my $entry = $self->{ENTRY}; | ||
| 102 | if (defined $value) { | ||
| 103 | $entry->delete('0', 'end'); | ||
| 104 | $entry->insert('end', $value); | ||
| 105 | } | ||
| 106 | return $entry | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _ind { | ||
| 110 | my ($self, $value) = @_; | ||
| 111 | $self->{INDICATOR}->configure(-background => $self->_pick->convert($value)) if defined $value; | ||
| 112 | return $self->{INDICATOR} | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _pick { | ||
| 116 | my ($self, $value) = @_; | ||
| 117 | $self->{PICKER}->put($value) if defined $value; | ||
| 118 | return $self->{PICKER} | ||
| 119 | } | ||
| 120 | |||
| 121 | sub SelectionCheck { | ||
| 122 | my $self = shift; | ||
| 123 | my @sel = $self->cmdExecute('doc_get_sel'); | ||
| 124 | my $pick = $self->_pick; | ||
| 125 | if (@sel) { | ||
| 126 | my $text = $self->cmdExecute('doc_get_text', @sel); | ||
| 127 | chomp($text); | ||
| 128 | if ($self->_pick->validate($text)) { | ||
| 129 | $pick->put($text); | ||
| 130 | $self->_ent($pick->notationCurrent); | ||
| 131 | $self->updateEntry; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub Unload { | ||
| 137 | my $self = shift; | ||
| 138 | # $self->extGet('ToolPanel')->deletePage('Colors'); | ||
| 139 | $self->ToolRightPageRemove('Colors'); | ||
| 140 | return $self->SUPER::Unload | ||
| 141 | } | ||
| 142 | |||
| 143 | sub updateEntry { | ||
| 144 | my ($self, $value) = @_; | ||
| 145 | $value = $self->_ent->get unless defined $value; | ||
| 146 | my $pick = $self->_pick; | ||
| 147 | if ($self->_pick->validate($value)) { | ||
| 148 | $self->_ind($pick->getHEX); | ||
| 149 | $self->_ent->configure(-foreground => $self->configGet('-foreground')); | ||
| 150 | $self->_pick($value); | ||
| 151 | } else { | ||
| 152 | $self->_ind($self->configGet('-background')); | ||
| 153 | $self->_ent->configure(-foreground => $self->configGet('-errorcolor')); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | sub updatePicker { | ||
| 158 | my ($self, $value) = @_; | ||
| 159 | $self->_ent($value); | ||
| 160 | $self->_ind($self->_pick->getHEX); | ||
| 161 | } | ||
| 162 | |||
| 163 | =head1 LICENSE | ||
| 164 | |||
| 165 | Same as Perl. | ||
| 166 | |||
| 167 | =head1 AUTHOR | ||
| 168 | |||
| 169 | Hans Jeuken (hanje at cpan dot org) | ||
| 170 | |||
| 171 | =head1 TODO | ||
| 172 | |||
| 173 | =over 4 | ||
| 174 | |||
| 175 | =back | ||
| 176 | |||
| 177 | =head1 BUGS AND CAVEATS | ||
| 178 | |||
| 179 | If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>. | ||
| 180 | |||
| 181 | =head1 SEE ALSO | ||
| 182 | |||
| 183 | =over 4 | ||
| 184 | |||
| 185 | =back | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 |