| 1 | package Tk::ColorPicker; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use vars qw($VERSION); | ||
| 6 | $VERSION = '0.12'; | ||
| 7 | use Tk; | ||
| 8 | |||
| 9 | use base qw(Tk::Derived Tk::Frame); | ||
| 10 | |||
| 11 | Construct Tk::Widget 'ColorPicker'; | ||
| 12 | |||
| 13 | require Tk::NoteBook; | ||
| 14 | require Tk::ListBrowser; | ||
| 15 | use Imager::Screenshot 'screenshot'; | ||
| 16 | use Math::Round; | ||
| 17 | use Scalar::Util qw(looks_like_number); | ||
| 18 | use Convert::Color; | ||
| 19 | |||
| 20 | my @colspaces = ( | ||
| 21 | [qw[RGB Red Green Blue]], | ||
| 22 | [qw[CMY Cyan Magenta Yellow]], | ||
| 23 | [qw[HSV Hue Saturation Value]], | ||
| 24 | ); | ||
| 25 | |||
| 26 | my %depthvalues = ( | ||
| 27 | 4 => 1, | ||
| 28 | 8 => 1, | ||
| 29 | 12 => 1, | ||
| 30 | 16 => 1, | ||
| 31 | ); | ||
| 32 | |||
| 33 | my %convertcalls = ( | ||
| 34 | cmy => \&convertCMY, | ||
| 35 | cmyX => \&convertCMYx, | ||
| 36 | hex => \&convertHEX, | ||
| 37 | hsv => \&convertHSV, | ||
| 38 | rgb => \&convertRGB, | ||
| 39 | rgbX => \&convertRGBx, | ||
| 40 | ); | ||
| 41 | |||
| 42 | my %notationcalls = ( | ||
| 43 | cmy => \&getCMY, | ||
| 44 | cmyX => \&getCMYx, | ||
| 45 | hex => \&getHEX, | ||
| 46 | hsv => \&getHSV, | ||
| 47 | rgb => \&getRGB, | ||
| 48 | rgbX => \&getRGBx, | ||
| 49 | ); | ||
| 50 | |||
| 51 | my %validatecalls = ( | ||
| 52 | cmy => \&validateCMY, | ||
| 53 | cmyX => \&validateCMYx, | ||
| 54 | hsv => \&validateHSV, | ||
| 55 | rgb => \&validateRGB, | ||
| 56 | rgbX => \&validateRGBx, | ||
| 57 | ); | ||
| 58 | |||
| 59 | =head1 NAME | ||
| 60 | |||
| 61 | Tk::ColorPicker - Frame based megawidget for selecting a color. | ||
| 62 | |||
| 63 | =head1 SYNOPSIS | ||
| 64 | |||
| 65 | use Tk::ColorPicker; | ||
| 66 | my $pick = $widow->ColorPicker->pack; | ||
| 67 | |||
| 68 | =head1 DESCRIPTION | ||
| 69 | |||
| 70 | Tk::ColorPicker lets you edit a color in RGB, CMY and HSV space. | ||
| 71 | It has a facility to pick a screen color with your mouse. | ||
| 72 | It offers a history of previous selected colors. | ||
| 73 | It can work in color depths of 4, 8, 12 or 16 bits per channel. | ||
| 74 | You can switch color depth on the fly. | ||
| 75 | |||
| 76 | =head1 OPTIONS | ||
| 77 | |||
| 78 | =over 4 | ||
| 79 | |||
| 80 | =item Switch: B<-colordepth> | ||
| 81 | |||
| 82 | Default value 8 bits per color channel. Can be 4, 8, 12 or 16; | ||
| 83 | |||
| 84 | =item Switch: B<-colorheight> | ||
| 85 | |||
| 86 | Specifies the height of the color squares on the history page. Default value is 30. | ||
| 87 | |||
| 88 | =item Switch: B<-colorwidth> | ||
| 89 | |||
| 90 | Specifies the width of the color squares on the history page. Default value is 40. | ||
| 91 | |||
| 92 | =item Switch: B<-depthselect> | ||
| 93 | |||
| 94 | Default value 0. If set a row of radiobuttons is diplayed allowing you to switch | ||
| 95 | color depth. | ||
| 96 | |||
| 97 | =item Switch: B<-historycolumns> | ||
| 98 | |||
| 99 | Default value 6. Number of columns in the history tab. | ||
| 100 | |||
| 101 | =item Switch: B<-historyfile> | ||
| 102 | |||
| 103 | Undefined by default. Here you set the filename from where the history is saved | ||
| 104 | and loaded. | ||
| 105 | |||
| 106 | =item Switch: B<-indborderwidth> | ||
| 107 | |||
| 108 | Default value 2. Borderwidth of indicator labels in the recent tab. | ||
| 109 | |||
| 110 | =item Switch: B<-indicatorwidth> | ||
| 111 | |||
| 112 | Default value 4. Width of the indicator labels in the recent tab. | ||
| 113 | |||
| 114 | =item Switch: B<-indrelief> | ||
| 115 | |||
| 116 | Default value 'sunken'. Relief of the indicator labels in the recent tab. | ||
| 117 | |||
| 118 | =item Switch: B<-maxhistory> | ||
| 119 | |||
| 120 | Default value 32.Specifies the maximum number of entries in the history list. | ||
| 121 | |||
| 122 | =item Switch: B<-notation> | ||
| 123 | |||
| 124 | Selects in which notation the color should be represented. Default value I<hex>. | ||
| 125 | Possible values are: | ||
| 126 | |||
| 127 | =over 4 | ||
| 128 | |||
| 129 | =item B<cmy> C<cmy(0.5, 0.5, 0.5)> | ||
| 130 | |||
| 131 | The channel values can be between 0 and 1. It works with 3 decimals precision. For maximum | ||
| 132 | accuracy set your dolor depth to 12 bits per color. | ||
| 133 | |||
| 134 | =item B<cmyX> C<cmy8(127, 127, 127)> | ||
| 135 | |||
| 136 | The X stands for the color depth. The channel values are integers ranging from 0 to the max channel | ||
| 137 | value for the current selected color depth. | ||
| 138 | |||
| 139 | =item B<hex> C<#7F7F7F> | ||
| 140 | |||
| 141 | The hexadecimal notation of a color. | ||
| 142 | |||
| 143 | =item B<hsv> C<hsv(360, 0, 0.5)> | ||
| 144 | |||
| 145 | Hue value can be between 0 and 360 degrees with 1 decimal precision. | ||
| 146 | Saturation and value range from 0 to 1 with 3 decimals precision. | ||
| 147 | |||
| 148 | =item B<rgb> C<rgb(0.5, 0.5, 0.5)> | ||
| 149 | |||
| 150 | The channel values can be between 0 and 1. It works with 3 decimals precision. For maximum | ||
| 151 | accuracy set your dolor depth to 12 bits per color. | ||
| 152 | |||
| 153 | =item B<rgbX> C<rgb8(127, 127, 127)> | ||
| 154 | |||
| 155 | The X stands for the color depth. The channel values are integers ranging from 0 to the max channel | ||
| 156 | value for the current selected color depth. | ||
| 157 | |||
| 158 | =back | ||
| 159 | |||
| 160 | =item Switch: B<-notationselect> | ||
| 161 | |||
| 162 | Default value 0. If set a menubutton is diplayed allowing you to select a notation. | ||
| 163 | |||
| 164 | =item Switch: B<-sliderheight> | ||
| 165 | |||
| 166 | Default value 200. Sets the length of all channel sliders. | ||
| 167 | |||
| 168 | =item Switch: B<-updatecall> | ||
| 169 | |||
| 170 | Set this callback to reflect changes to the outside. | ||
| 171 | Gets the current color as parameter. | ||
| 172 | |||
| 173 | =back | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | =head1 METHODS | ||
| 178 | |||
| 179 | =over 4 | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub Populate { | ||
| 184 | my ($self,$args) = @_; | ||
| 185 | |||
| 186 | my $sliderheight = delete $args->{'-sliderheight'}; | ||
| 187 | $sliderheight = 200 unless defined $sliderheight; | ||
| 188 | |||
| 189 | $self->SUPER::Populate($args); | ||
| 190 | |||
| 191 | my $dvar = ''; | ||
| 192 | my $nvar = ''; | ||
| 193 | my $rvar = ''; | ||
| 194 | $self->{COLORDEPTH} = \$dvar; | ||
| 195 | $self->{CONFIG} = 1; | ||
| 196 | $self->{DEPTHVAR} = \$rvar; #used for the radiobuttons in depthselect | ||
| 197 | $self->{HISTORY} = []; | ||
| 198 | $self->{NOTATION} = \$nvar; | ||
| 199 | $self->{SLIDERHEIGHT} = 200; | ||
| 200 | $self->{CURRENT} = ''; | ||
| 201 | |||
| 202 | my $pick = $self->Button( | ||
| 203 | -text => 'Pick', | ||
| 204 | -command => ['pickActivate', $self], | ||
| 205 | )->pack( | ||
| 206 | -fill => 'x', | ||
| 207 | -padx => 2, | ||
| 208 | -pady => 2, | ||
| 209 | ); | ||
| 210 | $self->Advertise(Pick => $pick); | ||
| 211 | $self->bind('<Escape>', [$self, 'pickCancel']); | ||
| 212 | my $nb = $self->NoteBook->pack(-expand => 1, -fill => 'both'); | ||
| 213 | my %varpool = (); | ||
| 214 | for (@colspaces) { | ||
| 215 | my @space = @$_; | ||
| 216 | my $lab = shift @space; | ||
| 217 | my $page = $nb->add($lab, -label => $lab); | ||
| 218 | for (@space) { | ||
| 219 | my $channel = $_; | ||
| 220 | my $slframe = $page->Frame->pack(-side => 'left', -padx => 2, -expand => 1, -fill => 'y'); | ||
| 221 | my $var = 0; | ||
| 222 | $varpool{$channel} = \$var; | ||
| 223 | my %hsv = ( | ||
| 224 | Hue => 359.9, | ||
| 225 | Saturation => 1, | ||
| 226 | Value => 1, | ||
| 227 | ); | ||
| 228 | my @m = (); | ||
| 229 | if (exists $hsv{$channel}) { | ||
| 230 | push @m, -from => $hsv{$channel}; | ||
| 231 | unless ($channel eq 'Hue') { | ||
| 232 | push @m, -resolution => 0.001; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | my $slider = $slframe->Scale(@m, | ||
| 236 | -to => 0, | ||
| 237 | |||
| 238 | -orient => 'vertical', | ||
| 239 | -command => ['ChannelUpdate', $self, $channel], | ||
| 240 | -variable => \$var, | ||
| 241 | )->pack(-pady => 2, -expand => 1, -fill => 'y'); | ||
| 242 | $self->Advertise($_, $slider); | ||
| 243 | $slframe->Label(-width => 8, -text => $_)->pack; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | $self->{VARPOOL} = \%varpool; | ||
| 248 | my $history; | ||
| 249 | my $recent = $nb->add('Recent', -label => 'Recent', -raisecmd => sub { | ||
| 250 | $self->after(1, sub { | ||
| 251 | $self->historyUpdate; | ||
| 252 | }) | ||
| 253 | }); | ||
| 254 | $history = $recent->ListBrowser( | ||
| 255 | -width => 250, | ||
| 256 | -arrange => 'row', | ||
| 257 | -itemtype => 'image', | ||
| 258 | -textside => 'bottom', | ||
| 259 | -browsecmd => ['historySelect', $self], | ||
| 260 | )->pack( | ||
| 261 | -expand => 1, | ||
| 262 | -fill => 'both', | ||
| 263 | ); | ||
| 264 | $self->Advertise(History => $history); | ||
| 265 | |||
| 266 | |||
| 267 | $self->ConfigSpecs( | ||
| 268 | -balloon => ['PASSIVE'], | ||
| 269 | -colorwidth => ['PASSIVE', undef, undef, 40], | ||
| 270 | -colorheight => ['PASSIVE', undef, undef, 30], | ||
| 271 | -colordepth => ['METHOD', undef, undef, 8], | ||
| 272 | -depthselect =>['METHOD', undef, undef, 0], | ||
| 273 | -historycolumns => ['PASSIVE', undef, undef, 6], | ||
| 274 | -historyfile => ['PASSIVE'], | ||
| 275 | -indborderwidth => ['PASSIVE', undef, undef, 2], | ||
| 276 | -indicatorwidth => ['PASSIVE', undef, undef, 4], | ||
| 277 | -indrelief => ['PASSIVE', undef, undef, 'sunken'], | ||
| 278 | -maxhistory => ['PASSIVE', undef, undef, 32], | ||
| 279 | -notation => ['METHOD', undef, undef, 'hex'], | ||
| 280 | -notationselect => ['METHOD', undef, undef, 0], | ||
| 281 | -sliderheight => ['METHOD', 'sliderHeight', 'SliderHeight', 200], | ||
| 282 | -updatecall => ['CALLBACK', undef, undef, sub {}], | ||
| 283 | DEFAULT => [ $self ], | ||
| 284 | ); | ||
| 285 | |||
| 286 | $self->Delegates( | ||
| 287 | DEFAULT => $self, | ||
| 288 | ); | ||
| 289 | $self->after(300, ['PostConfig', $self]); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub ChannelUpdate { | ||
| 293 | my ($self, $channel) = @_; | ||
| 294 | return if $self->ConfigMode; | ||
| 295 | if ($self->IsHSV($channel)) { | ||
| 296 | $self->ChannelUpdateHSV; | ||
| 297 | } elsif ($self->IsCMY($channel)) { | ||
| 298 | $self->ChannelUpdateCMY; | ||
| 299 | } elsif ($self->IsRGB($channel)) { | ||
| 300 | $self->ChannelUpdateRGB; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | sub ChannelUpdateCMY { | ||
| 305 | my $self = shift; | ||
| 306 | |||
| 307 | my $max = $self->maxChannelValue; | ||
| 308 | my $pool = $self->{VARPOOL}; | ||
| 309 | my $cvar = $pool->{'Cyan'}; | ||
| 310 | my $cyan = $$cvar; | ||
| 311 | my $mvar = $pool->{'Magenta'}; | ||
| 312 | my $magenta = $$mvar; | ||
| 313 | my $yvar = $pool->{'Yellow'}; | ||
| 314 | my $yellow = $$yvar; | ||
| 315 | |||
| 316 | my $red = $max - $cyan; | ||
| 317 | my $green = $max - $magenta; | ||
| 318 | my $blue = $max - $yellow; | ||
| 319 | my $hex = $self->rgb2hex($red, $green, $blue); | ||
| 320 | $self->UpdateRGB($hex); | ||
| 321 | $self->UpdateHSV($hex); | ||
| 322 | $self->UpdateCall($hex); | ||
| 323 | } | ||
| 324 | |||
| 325 | sub ChannelUpdateHSV { | ||
| 326 | my $self = shift; | ||
| 327 | |||
| 328 | my $pool = $self->{VARPOOL}; | ||
| 329 | my $hvar = $pool->{'Hue'}; | ||
| 330 | my $hue = $$hvar; | ||
| 331 | my $svar = $pool->{'Saturation'}; | ||
| 332 | my $satur= $$svar; | ||
| 333 | my $vvar = $pool->{'Value'}; | ||
| 334 | my $value = $$vvar; | ||
| 335 | $value = 99.9999 if $value eq 100; | ||
| 336 | |||
| 337 | my ($red, $green, $blue) = $self->hsv2rgb($hue, $satur, $value); | ||
| 338 | my $hex = $self->rgb2hex($red, $green, $blue); | ||
| 339 | $self->UpdateRGB($hex); | ||
| 340 | $self->UpdateCMY($hex); | ||
| 341 | $self->UpdateCall($hex); | ||
| 342 | } | ||
| 343 | |||
| 344 | sub ChannelUpdateRGB { | ||
| 345 | my $self = shift; | ||
| 346 | |||
| 347 | my $depth = $self->colordepth; | ||
| 348 | my $mul = (2**$depth); | ||
| 349 | |||
| 350 | my $pool = $self->{VARPOOL}; | ||
| 351 | my $rvar = $pool->{'Red'}; | ||
| 352 | my $red = $$rvar; | ||
| 353 | my $gvar = $pool->{'Green'}; | ||
| 354 | my $green = $$gvar; | ||
| 355 | my $bvar = $pool->{'Blue'}; | ||
| 356 | my $blue = $$bvar; | ||
| 357 | my $hex = $self->rgb2hex($red, $green, $blue); | ||
| 358 | $self->UpdateCMY($hex); | ||
| 359 | $self->UpdateHSV($hex); | ||
| 360 | |||
| 361 | $self->UpdateCall($hex); | ||
| 362 | |||
| 363 | } | ||
| 364 | |||
| 365 | sub ClassInit { | ||
| 366 | my ($class,$mw) = @_; | ||
| 367 | $mw->bind($class, '<Escape>','pickCancel'); | ||
| 368 | return $class->SUPER::ClassInit($mw); | ||
| 369 | } | ||
| 370 | |||
| 371 | sub colordepth { | ||
| 372 | my ($self, $value) = @_; | ||
| 373 | my $valref = $self->{COLORDEPTH}; | ||
| 374 | if (defined $value) { | ||
| 375 | unless (exists $depthvalues{$value}) { | ||
| 376 | warn "invalid colordepth '$value'\n"; | ||
| 377 | return $$valref | ||
| 378 | } | ||
| 379 | my $oldmax = $self->maxChannelValue; | ||
| 380 | $$valref = $value; | ||
| 381 | my $radiovar = $self->{DEPTHVAR}; | ||
| 382 | $$radiovar = $value; | ||
| 383 | my $newmax = (2**$value) - 1; | ||
| 384 | my $varpool = $self->{VARPOOL}; | ||
| 385 | for (qw/Red Green Blue Cyan Magenta Yellow/) { | ||
| 386 | my $var = $varpool->{$_}; | ||
| 387 | my $oldval = $$var; | ||
| 388 | $self->Subwidget($_)->configure(-from => $newmax); | ||
| 389 | my $ratio = ($newmax + 1)/($oldmax + 1); | ||
| 390 | my $newval = $oldval * $ratio; | ||
| 391 | $$var = $newval; | ||
| 392 | } | ||
| 393 | $self->UpdateCall($self->compoundColor); | ||
| 394 | } | ||
| 395 | return $$valref; | ||
| 396 | } | ||
| 397 | |||
| 398 | =item B<colorDepth>I<($hexcolor)> | ||
| 399 | |||
| 400 | Returns the color depth of $hexcolor. | ||
| 401 | |||
| 402 | =cut | ||
| 403 | |||
| 404 | sub colorDepth { | ||
| 405 | my ($self, $color) = @_; | ||
| 406 | $color =~ s/^\#//; | ||
| 407 | my %valid = ( | ||
| 408 | 3 => 4, | ||
| 409 | 6 => 8, | ||
| 410 | 9 => 12, | ||
| 411 | 12 => 16 | ||
| 412 | ); | ||
| 413 | my $length = length($color); | ||
| 414 | return $valid{$length} if exists $valid{$length}; | ||
| 415 | warn "Invalid color '$color'\n"; | ||
| 416 | return undef | ||
| 417 | } | ||
| 418 | |||
| 419 | =item B<compoundColor> | ||
| 420 | |||
| 421 | Returns a hex color string based on the Red, Green and Blue channels. | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | sub compoundColor { | ||
| 426 | my $self = shift; | ||
| 427 | my $pool = $self->{VARPOOL}; | ||
| 428 | my $vred = $pool->{'Red'}; | ||
| 429 | my $red = $self->hexString($$vred); | ||
| 430 | my $vgreen = $pool->{'Green'}; | ||
| 431 | my $green = $self->hexString($$vgreen); | ||
| 432 | my $vblue = $pool->{'Blue'}; | ||
| 433 | my $blue = $self->hexString($$vblue); | ||
| 434 | return "#$red$green$blue"; | ||
| 435 | } | ||
| 436 | |||
| 437 | sub ConfigMode { | ||
| 438 | my $self = shift; | ||
| 439 | $self->{CONFIG} = shift if @_; | ||
| 440 | return $self->{CONFIG} | ||
| 441 | } | ||
| 442 | |||
| 443 | sub convert { | ||
| 444 | my ($self, $color) = @_; | ||
| 445 | my $not = $self->notationDetect($color); | ||
| 446 | return undef unless defined $not; | ||
| 447 | my $call = $convertcalls{$not}; | ||
| 448 | return &$call($self, $color); | ||
| 449 | } | ||
| 450 | |||
| 451 | sub convertBase { | ||
| 452 | my ($self, $val, $space) = @_; | ||
| 453 | if ($val =~ /^$space\((.+)\)$/) { | ||
| 454 | my $parstring = "$1, "; | ||
| 455 | my @vals; | ||
| 456 | for (1 .. 3) { | ||
| 457 | if ($parstring =~ s/^^([^,]+),\s*//) { | ||
| 458 | my $number = $1; | ||
| 459 | if (looks_like_number($number)) { | ||
| 460 | if (($space eq 'hsv') and ($_ eq 1)) { | ||
| 461 | $number = $self->numround($number, 1); | ||
| 462 | } else { | ||
| 463 | $number = $self->numround($number, 3); | ||
| 464 | } | ||
| 465 | push @vals, $number | ||
| 466 | } | ||
| 467 | } | ||
| 468 | } | ||
| 469 | return @vals | ||
| 470 | } | ||
| 471 | } | ||
| 472 | |||
| 473 | sub convertBaseX { | ||
| 474 | my ($self, $val, $space) = @_; | ||
| 475 | if ($val =~ /^$space\d+\((.+)\)$/) { | ||
| 476 | my $parstring = "$1, "; | ||
| 477 | my @vals; | ||
| 478 | for (1 .. 3) { | ||
| 479 | if ($parstring =~ s/^([^,]+),\s*//) { | ||
| 480 | my $number = $1; | ||
| 481 | push @vals, $number if $number =~ /^\d+$/ | ||
| 482 | } | ||
| 483 | } | ||
| 484 | return @vals | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | =item B<convertCMY>I<($string)> | ||
| 489 | |||
| 490 | Returns the hex formatted representation of a cmy formatted string. | ||
| 491 | |||
| 492 | =cut | ||
| 493 | |||
| 494 | sub convertCMY { | ||
| 495 | my ($self, $string) = @_; | ||
| 496 | my ($cyan, $magenta, $yellow) = $self->convertBase($string, 'cmy'); | ||
| 497 | my $max = $self->maxChannelValue; | ||
| 498 | my $red = int((1 - $cyan) * $max); | ||
| 499 | my $green = int((1 - $magenta) * $max); | ||
| 500 | my $blue = int((1 - $yellow) * $max); | ||
| 501 | return $self->rgb2hex($red, $green, $blue); | ||
| 502 | } | ||
| 503 | |||
| 504 | =item B<convertCMYx>I<($string)> | ||
| 505 | |||
| 506 | Returns the hex formatted representation of a cmyX formatted string. | ||
| 507 | |||
| 508 | =cut | ||
| 509 | |||
| 510 | sub convertCMYx { | ||
| 511 | my ($self, $string) = @_; | ||
| 512 | my ($cyan, $magenta, $yellow) = $self->convertBaseX($string, 'cmy'); | ||
| 513 | my $max = $self->maxChannelValue; | ||
| 514 | my $red = $max - $cyan; | ||
| 515 | my $green = $max - $magenta; | ||
| 516 | my $blue = $max - $yellow; | ||
| 517 | return $self->rgb2hex($red, $green, $blue); | ||
| 518 | } | ||
| 519 | |||
| 520 | =item B<convertDepth>(I<$string>, ?<I$depth>?) | ||
| 521 | |||
| 522 | Converts the depth of $string to $depth. | ||
| 523 | If $depth is not specified, B<-colordepth> is used | ||
| 524 | |||
| 525 | =cut | ||
| 526 | |||
| 527 | sub convertDepth { | ||
| 528 | my ($self, $string, $depth) = @_; | ||
| 529 | $depth = $self->colordepth unless defined $depth; | ||
| 530 | return $string if $self->colorDepth($string) eq $depth; | ||
| 531 | |||
| 532 | $string =~ s/^(\#|Ox)//; | ||
| 533 | |||
| 534 | my $length = length($string) / 3; | ||
| 535 | $_ = $string; | ||
| 536 | my ($r, $g, $b) = m/(\w{$length})(\w{$length})(\w{$length})/; | ||
| 537 | my $conv = $depth / 4; | ||
| 538 | for (\$r, \$g, \$b) { | ||
| 539 | my $tag = $_; | ||
| 540 | while (length($$tag) ne $conv) { | ||
| 541 | if (length($$tag) > $conv) { | ||
| 542 | $$tag =~ s/.$// | ||
| 543 | } else { | ||
| 544 | $$tag = $$tag . '0'; | ||
| 545 | } | ||
| 546 | } | ||
| 547 | } | ||
| 548 | return '#' . $r. $g . $b | ||
| 549 | } | ||
| 550 | |||
| 551 | =item B<convertHEX>I<($string)> | ||
| 552 | |||
| 553 | Returns the hex formatted representation of a hex formatted string. | ||
| 554 | So basically it does nothing but return it's input. | ||
| 555 | |||
| 556 | =cut | ||
| 557 | |||
| 558 | sub convertHEX { | ||
| 559 | my ($self, $string) = @_; | ||
| 560 | return $string | ||
| 561 | } | ||
| 562 | |||
| 563 | |||
| 564 | =item B<convertHSV>I<($string)> | ||
| 565 | |||
| 566 | Returns the hex formatted representation of a hsv formatted string. | ||
| 567 | |||
| 568 | =cut | ||
| 569 | |||
| 570 | sub convertHSV { | ||
| 571 | my ($self, $string) = @_; | ||
| 572 | my ($hue, $sat, $val) = $self->convertBase($string, 'hsv'); | ||
| 573 | return $self->rgb2hex($self->hsv2rgb($hue, $sat, $val)); | ||
| 574 | } | ||
| 575 | |||
| 576 | =item B<convertRGB>I<($string)> | ||
| 577 | |||
| 578 | Returns the hex formatted representation of a rgb formatted string. | ||
| 579 | |||
| 580 | =cut | ||
| 581 | |||
| 582 | sub convertRGB { | ||
| 583 | my ($self, $string) = @_; | ||
| 584 | my ($red, $green, $blue) = $self->convertBase($string, 'rgb'); | ||
| 585 | my $max = $self->maxChannelValue; | ||
| 586 | $red = int($red * $max); | ||
| 587 | $green = int($green * $max); | ||
| 588 | $blue = int($blue * $max); | ||
| 589 | return $self->rgb2hex($red, $green, $blue); | ||
| 590 | } | ||
| 591 | |||
| 592 | |||
| 593 | =item B<convertRGBx>I<($string)> | ||
| 594 | |||
| 595 | Returns the hex formatted representation of a rgbX formatted string. | ||
| 596 | |||
| 597 | =cut | ||
| 598 | |||
| 599 | sub convertRGBx { | ||
| 600 | my ($self, $string) = @_; | ||
| 601 | my ($red, $green, $blue) = $self->convertBaseX($string, 'rgb'); | ||
| 602 | return $self->rgb2hex($red, $green, $blue); | ||
| 603 | } | ||
| 604 | |||
| 605 | sub depthselect { | ||
| 606 | my ($self, $flag) = @_; | ||
| 607 | if (defined $flag) { | ||
| 608 | if ($flag) { | ||
| 609 | unless (defined $self->Subwidget('DepthSelect')) { | ||
| 610 | my $bpcframe = $self->Frame->pack( | ||
| 611 | -before => $self->Subwidget('Pick'), | ||
| 612 | -fill => 'x', | ||
| 613 | ); | ||
| 614 | $bpcframe->Label( | ||
| 615 | -anchor => 'e', | ||
| 616 | -justify => 'right', | ||
| 617 | -text => 'Depth:', | ||
| 618 | -width => 7, | ||
| 619 | )->pack(-side => 'left', -padx => 2, -pady => 2); | ||
| 620 | for (4, 8, 12, 16) { | ||
| 621 | my $depth = $_; | ||
| 622 | $bpcframe->Radiobutton( | ||
| 623 | -text => $depth, | ||
| 624 | -value => $depth, | ||
| 625 | -command => ['colordepth', $self, $depth], | ||
| 626 | -variable => $self->{DEPTHVAR}, | ||
| 627 | )->pack(-side => 'left', -padx => 2, -pady => 2); | ||
| 628 | } | ||
| 629 | $self->Advertise('DepthSelect', $bpcframe) | ||
| 630 | } | ||
| 631 | } else { | ||
| 632 | if (defined $self->Subwidget('DepthSelect')) { | ||
| 633 | $self->Subwidget('DepthSelect')->destroy; | ||
| 634 | $self->Advertise('DepthSelect', undef); | ||
| 635 | } | ||
| 636 | } | ||
| 637 | } | ||
| 638 | return defined $self->Subwidget('DepthSelect'); | ||
| 639 | } | ||
| 640 | |||
| 641 | =item B<getCMY> | ||
| 642 | |||
| 643 | Returns a cmy formatted representation of the current color. | ||
| 644 | |||
| 645 | =cut | ||
| 646 | |||
| 647 | sub getCMY { | ||
| 648 | my $self = shift; | ||
| 649 | return $self->notationCMY($self->getHEX); | ||
| 650 | } | ||
| 651 | |||
| 652 | =item B<getCMYx> | ||
| 653 | |||
| 654 | Returns a cmyX formatted representation of the current color. | ||
| 655 | |||
| 656 | =cut | ||
| 657 | |||
| 658 | sub getCMYx { | ||
| 659 | my $self = shift; | ||
| 660 | return $self->notationCMYx($self->getHEX); | ||
| 661 | } | ||
| 662 | |||
| 663 | =item B<getHEX> | ||
| 664 | |||
| 665 | Returns a hex formatted representation of the current color. | ||
| 666 | |||
| 667 | =cut | ||
| 668 | |||
| 669 | sub getHEX { | ||
| 670 | return $_[0]->{CURRENT}; | ||
| 671 | } | ||
| 672 | |||
| 673 | =item B<getHSV> | ||
| 674 | |||
| 675 | Returns a hsv formatted representation of the current color. | ||
| 676 | |||
| 677 | =cut | ||
| 678 | |||
| 679 | sub getHSV { | ||
| 680 | my $self = shift; | ||
| 681 | return $self->notationHSV($self->getHEX); | ||
| 682 | } | ||
| 683 | |||
| 684 | =item B<getRGB> | ||
| 685 | |||
| 686 | Returns a rgb formatted representation of the current color. | ||
| 687 | |||
| 688 | =cut | ||
| 689 | |||
| 690 | sub getRGB { | ||
| 691 | my $self = shift; | ||
| 692 | return $self->notationRGB($self->getHEX); | ||
| 693 | } | ||
| 694 | |||
| 695 | =item B<getRGBx> | ||
| 696 | |||
| 697 | Returns a rgbX formatted representation of the current color. | ||
| 698 | |||
| 699 | =cut | ||
| 700 | |||
| 701 | sub getRGBx { | ||
| 702 | my $self = shift; | ||
| 703 | return $self->notationRGBx($self->getHEX); | ||
| 704 | } | ||
| 705 | |||
| 706 | sub getXPM { | ||
| 707 | my ($self, $color) = @_; | ||
| 708 | |||
| 709 | # print "color $color\n"; | ||
| 710 | $color = $self->convert($color); | ||
| 711 | # print "converted $color\n"; | ||
| 712 | |||
| 713 | my $width = $self->cget('-colorwidth'); | ||
| 714 | my $height = $self->cget('-colorheight'); | ||
| 715 | #create rgb | ||
| 716 | my $sbg = $color; | ||
| 717 | $sbg =~ s/^#//; | ||
| 718 | my $bg = Convert::Color->new("rgb8:$sbg"); | ||
| 719 | my ($red, $green, $blue) = $bg->rgb; | ||
| 720 | #convert top rgb | ||
| 721 | my $tred = $self->offset($red, 0, 1.81); | ||
| 722 | my $tgreen = $self->offset($green, 0, 1.81); | ||
| 723 | my $tblue = $self->offset($blue, 0, 1.75); | ||
| 724 | |||
| 725 | #convert bottom rgb | ||
| 726 | my $bred = $self->offset($red, 255, 1.79); | ||
| 727 | my $bgreen = $self->offset($green, 255, 2); | ||
| 728 | my $bblue = $self->offset($blue, 255, 2); | ||
| 729 | |||
| 730 | #create xpm color definitions | ||
| 731 | my $t = Convert::Color->new("rgb:$tred,$tgreen,$tblue"); | ||
| 732 | my $b = Convert::Color->new("rgb:$bred,$bgreen,$bblue"); | ||
| 733 | my $bottom = '#' . $t->as_rgb8->hex; | ||
| 734 | my $top = '#' . $b->as_rgb8->hex; | ||
| 735 | my $fill = '#' . $bg->as_rgb8->hex; | ||
| 736 | |||
| 737 | #create the image | ||
| 738 | my $xpm = "/* XPM */ | ||
| 739 | static char * select_draft_xpm[] = { | ||
| 740 | \"$width $height 3 1\", | ||
| 741 | \" c $top\", | ||
| 742 | \". c $bottom\", | ||
| 743 | \"+ c $fill\", | ||
| 744 | "; | ||
| 745 | |||
| 746 | my $base = "\" "; | ||
| 747 | my $end = ".\",\n"; | ||
| 748 | |||
| 749 | #create top line | ||
| 750 | $xpm = "$xpm$base"; | ||
| 751 | for (1 .. $width - 2) { $xpm = "$xpm "} | ||
| 752 | $xpm = "$xpm$end"; | ||
| 753 | |||
| 754 | #create body | ||
| 755 | my $line = $base; | ||
| 756 | for (1 .. $width - 2) { $line = "$line+"} | ||
| 757 | $line = "$line$end"; | ||
| 758 | for (1 .. $height - 2) { $xpm = "$xpm$line" } | ||
| 759 | |||
| 760 | #create bottom line | ||
| 761 | $xpm = "$xpm$base"; | ||
| 762 | for (1 .. $width - 2) { $xpm = "$xpm."} | ||
| 763 | $xpm = "$xpm$end"; | ||
| 764 | |||
| 765 | return $self->Pixmap(-data => $xpm); | ||
| 766 | } | ||
| 767 | |||
| 768 | sub hex2cmy { | ||
| 769 | my ($self, $hex) = @_; | ||
| 770 | my ($red, $green, $blue) = $self->hex2rgb($hex); | ||
| 771 | my $max = $self->maxChannelValue; | ||
| 772 | return ($max - $red, $max- $green, $max - $blue) | ||
| 773 | } | ||
| 774 | |||
| 775 | sub hex2hsv { | ||
| 776 | my ($self, $hex) = @_; | ||
| 777 | return $self->rgb2hsv($self->hex2rgb($hex)) | ||
| 778 | } | ||
| 779 | |||
| 780 | =item B<hex2rgb>I<($hex)> | ||
| 781 | |||
| 782 | Converts $hex to Red, Green Blue values. | ||
| 783 | |||
| 784 | =cut | ||
| 785 | |||
| 786 | sub hex2rgb { | ||
| 787 | my ($self, $hex) = @_; | ||
| 788 | $hex =~ s/^(\#|Ox)//; | ||
| 789 | my $length = length($hex) / 3; | ||
| 790 | $_ = $hex; | ||
| 791 | my ($r, $g, $b) = m/(\w{$length})(\w{$length})(\w{$length})/; | ||
| 792 | my @rgb = (); | ||
| 793 | $rgb[0] = CORE::hex($r); | ||
| 794 | $rgb[1] = CORE::hex($g); | ||
| 795 | $rgb[2] = CORE::hex($b); | ||
| 796 | return @rgb | ||
| 797 | } | ||
| 798 | |||
| 799 | =item B<hexString>(I<$num>, ?I<$depth>?>) | ||
| 800 | |||
| 801 | Returns the hexadecimal notation of $num. | ||
| 802 | If $depth is not specified, B<-colordepth> is used | ||
| 803 | |||
| 804 | =cut | ||
| 805 | |||
| 806 | sub hexString { | ||
| 807 | my ($self, $num, $depth) = @_; | ||
| 808 | $depth = $self->colordepth unless defined $depth; | ||
| 809 | my $length = $depth / 4; | ||
| 810 | my $hex = substr(sprintf("0x%X", $num), 2); | ||
| 811 | while (length($hex) < $length) { $hex = "0$hex" } | ||
| 812 | return $hex | ||
| 813 | } | ||
| 814 | |||
| 815 | sub History { | ||
| 816 | my $self = shift; | ||
| 817 | return $self->{HISTORY}; | ||
| 818 | } | ||
| 819 | |||
| 820 | =item B<historyAdd>(I<$color>) | ||
| 821 | |||
| 822 | Adds color to the History list. | ||
| 823 | Saves the list and updates the history tab. | ||
| 824 | |||
| 825 | =cut | ||
| 826 | |||
| 827 | sub historyAdd { | ||
| 828 | my ($self, $color) = @_; | ||
| 829 | return unless $self->validate($color); | ||
| 830 | $self->historyLoad; | ||
| 831 | $self->historyNew($color); | ||
| 832 | $self->historySave; | ||
| 833 | } | ||
| 834 | |||
| 835 | =item B<historyClear> | ||
| 836 | |||
| 837 | Clears the history list. | ||
| 838 | |||
| 839 | =cut | ||
| 840 | |||
| 841 | sub historyClear { | ||
| 842 | my $history = $_[0]->History; | ||
| 843 | while (@$history) { pop @$history } | ||
| 844 | } | ||
| 845 | |||
| 846 | =item B<historyLoad> | ||
| 847 | |||
| 848 | Loads the history file if it is specified. | ||
| 849 | |||
| 850 | =cut | ||
| 851 | |||
| 852 | sub historyLoad { | ||
| 853 | my $self = shift; | ||
| 854 | my $file = $self->cget('-historyfile'); | ||
| 855 | return unless defined $file; | ||
| 856 | return unless -e $file; | ||
| 857 | if (open INFILE, "<", $file) { | ||
| 858 | $self->historyClear; | ||
| 859 | my $history = $self->History; | ||
| 860 | while (<INFILE>) { | ||
| 861 | my $line = $_; | ||
| 862 | chomp($line); | ||
| 863 | push @$history, $line; | ||
| 864 | } | ||
| 865 | close INFILE; | ||
| 866 | } else { | ||
| 867 | warn "can not load file '$file'"; | ||
| 868 | } | ||
| 869 | } | ||
| 870 | |||
| 871 | =item B<historyNew>(I<$color>) | ||
| 872 | |||
| 873 | Adds $color to the history list | ||
| 874 | |||
| 875 | =cut | ||
| 876 | |||
| 877 | sub historyNew { | ||
| 878 | my ($self, $color) = @_; | ||
| 879 | return unless $self->validate($color); | ||
| 880 | my $history = $self->History; | ||
| 881 | my ($pos) = grep { $history->[$_] eq $color } 0 .. @$history - 1; | ||
| 882 | splice(@$history, $pos, 1) if defined $pos; | ||
| 883 | unshift @$history, $color; | ||
| 884 | my $size = @$history; | ||
| 885 | pop @$history if $size > $self->cget('-maxhistory'); | ||
| 886 | } | ||
| 887 | |||
| 888 | =item B<historyReset> | ||
| 889 | |||
| 890 | Clears the history list. Then saves and updates. | ||
| 891 | |||
| 892 | =cut | ||
| 893 | |||
| 894 | sub historyReset { | ||
| 895 | my $self = shift; | ||
| 896 | $self->historyClear; | ||
| 897 | $self->historySave; | ||
| 898 | $self->historyUpdate; | ||
| 899 | } | ||
| 900 | |||
| 901 | =item B<historySave> | ||
| 902 | |||
| 903 | Saves the history list. | ||
| 904 | |||
| 905 | =cut | ||
| 906 | |||
| 907 | sub historySave{ | ||
| 908 | my $self = shift; | ||
| 909 | my $file = $self->cget('-historyfile'); | ||
| 910 | return unless defined $file; | ||
| 911 | my $history = $self->History; | ||
| 912 | return unless @$history; | ||
| 913 | if (open OUTFILE, ">", $file) { | ||
| 914 | for (@$history) { | ||
| 915 | my $color = $_; | ||
| 916 | print OUTFILE "$color\n"; | ||
| 917 | } | ||
| 918 | close OUTFILE; | ||
| 919 | } else { | ||
| 920 | warn "can not save file '$file'"; | ||
| 921 | } | ||
| 922 | } | ||
| 923 | |||
| 924 | sub historySelect { | ||
| 925 | my ($self, $item) = @_; | ||
| 926 | $self->UpdateCall($item); | ||
| 927 | $self->UpdateAll($item); | ||
| 928 | } | ||
| 929 | |||
| 930 | =item B<historyUpdate> | ||
| 931 | |||
| 932 | Updates the history tab. | ||
| 933 | |||
| 934 | =cut | ||
| 935 | |||
| 936 | sub historyUpdate { | ||
| 937 | my $self = shift; | ||
| 938 | $self->historyLoad; | ||
| 939 | my $history = $self->History; | ||
| 940 | my $column = 0; | ||
| 941 | my $row = 0; | ||
| 942 | my $numcolumns = $self->cget('-historycolumns'); | ||
| 943 | my $list = $self->Subwidget('History'); | ||
| 944 | $list->deleteAll; | ||
| 945 | for (@$history) { | ||
| 946 | my $color = $_; | ||
| 947 | next if $list->infoExists($color); | ||
| 948 | my $xpm = $self->getXPM($color); | ||
| 949 | my $e = $list->add($color, | ||
| 950 | # -text => $color, | ||
| 951 | -image => $xpm, | ||
| 952 | ); | ||
| 953 | unless (defined $self->{HAVECELLSIZE}) { | ||
| 954 | $self->Subwidget('History')->cellSize($e); | ||
| 955 | $self->{HAVECELLSIZE} = 1; | ||
| 956 | } | ||
| 957 | } | ||
| 958 | $list->refreshPurge; | ||
| 959 | } | ||
| 960 | |||
| 961 | sub hsv2rgb { | ||
| 962 | |||
| 963 | # The procedure below converts an HSB value to RGB. It takes hue, | ||
| 964 | # saturation, and value components (floating-point, 0-1.0) as arguments, | ||
| 965 | # and returns a list containing RGB components (integers, 0-65535) as | ||
| 966 | # result. The code here is a copy of the code on page 616 of | ||
| 967 | # "Fundamentals of Interactive Computer Graphics" by Foley and Van Dam. | ||
| 968 | |||
| 969 | my($self, $hue, $sat, $value) = @_; | ||
| 970 | my($v, $i, $f, $p, $q, $t); | ||
| 971 | |||
| 972 | my $depth = $self->colordepth; | ||
| 973 | my $mul = (2**$depth)/65536; | ||
| 974 | $hue = $hue / 360; | ||
| 975 | |||
| 976 | $v = int(65535 * $value); | ||
| 977 | my $ret = $v * $mul; | ||
| 978 | return ($ret, $ret, $ret) if $sat == 0; | ||
| 979 | $hue *= 6; | ||
| 980 | $hue = 0 if $hue >= 6; | ||
| 981 | $i = int($hue); | ||
| 982 | $f = $hue - $i; | ||
| 983 | $p = int(65535 * $value * (1 - $sat)); | ||
| 984 | $q = int(65535 * $value * (1 - ($sat * $f))); | ||
| 985 | $t = int(65535 * $value * (1 - ($sat * (1 - $f)))); | ||
| 986 | my @rgb = (); | ||
| 987 | @rgb = ($v, $t, $p) if $i == 0; | ||
| 988 | @rgb = ($q, $v, $p) if $i == 1; | ||
| 989 | @rgb = ($p, $v, $t) if $i == 2; | ||
| 990 | @rgb = ($p, $q, $v) if $i == 3; | ||
| 991 | @rgb = ($t, $p, $v) if $i == 4; | ||
| 992 | @rgb = ($v, $p, $q) if $i == 5; | ||
| 993 | |||
| 994 | #convert to the proper depth | ||
| 995 | my @r = (); | ||
| 996 | for (@rgb) { | ||
| 997 | push @r, int($_ * $mul) | ||
| 998 | } | ||
| 999 | return @r | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | sub IsCMY { | ||
| 1003 | my ($self, $channel) = @_; | ||
| 1004 | my %hsv = ( | ||
| 1005 | Cyan => 1, | ||
| 1006 | Magenta => 1, | ||
| 1007 | Yellow => 1, | ||
| 1008 | ); | ||
| 1009 | return exists $hsv{$channel}; | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | sub IsHSV { | ||
| 1013 | my ($self, $channel) = @_; | ||
| 1014 | my %hsv = ( | ||
| 1015 | Hue => 1, | ||
| 1016 | Saturation => 1, | ||
| 1017 | Value => 1, | ||
| 1018 | ); | ||
| 1019 | return exists $hsv{$channel}; | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | sub IsRGB { | ||
| 1023 | my ($self, $channel) = @_; | ||
| 1024 | my %hsv = ( | ||
| 1025 | Red => 1, | ||
| 1026 | Green => 1, | ||
| 1027 | Blue => 1, | ||
| 1028 | ); | ||
| 1029 | return exists $hsv{$channel}; | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | =item B<maxChannelValue> | ||
| 1033 | |||
| 1034 | Returns the maximum values for the Red, Green, Blue, Cyan, Magenta and Yellow channels, | ||
| 1035 | based on B<-colordepth>. | ||
| 1036 | |||
| 1037 | =cut | ||
| 1038 | |||
| 1039 | sub maxChannelValue { | ||
| 1040 | my $self = shift; | ||
| 1041 | my $ref = $self->{COLORDEPTH}; | ||
| 1042 | my $depth = $$ref; | ||
| 1043 | return (2**$depth) - 1 if $depth ne ''; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | sub notation { | ||
| 1047 | my $self = shift; | ||
| 1048 | my $nvar = $self->{NOTATION}; | ||
| 1049 | if (@_) { | ||
| 1050 | $$nvar = shift; | ||
| 1051 | $self->UpdateCall($self->getHEX); | ||
| 1052 | } | ||
| 1053 | return $$nvar | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | =item B<notationCMY>I<($hexcolor)> | ||
| 1057 | |||
| 1058 | Returns a cmy formatted string of a hex color. | ||
| 1059 | |||
| 1060 | =cut | ||
| 1061 | |||
| 1062 | sub notationCMY { | ||
| 1063 | my ($self, $hex) = @_; | ||
| 1064 | my ($cyan, $magenta, $yellow) = $self->hex2cmy($hex); | ||
| 1065 | my $max = $self->maxChannelValue; | ||
| 1066 | $cyan = $self->numround($cyan / $max, 3); | ||
| 1067 | $magenta = $self->numround($magenta / $max, 3); | ||
| 1068 | $yellow = $self->numround($yellow / $max, 3); | ||
| 1069 | return "cmy($cyan, $magenta, $yellow)" | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | =item B<notationCMYx>I<($hexcolor)> | ||
| 1073 | |||
| 1074 | Returns a cmyX formatted string of a hex color. | ||
| 1075 | |||
| 1076 | =cut | ||
| 1077 | |||
| 1078 | sub notationCMYx { | ||
| 1079 | my ($self, $hex) = @_; | ||
| 1080 | my ($cyan, $magenta, $yellow) = $self->hex2cmy($hex); | ||
| 1081 | my $depth = $self->cget('-colordepth'); | ||
| 1082 | return "cmy$depth($cyan, $magenta, $yellow)" | ||
| 1083 | } | ||
| 1084 | |||
| 1085 | sub notationCurrent { | ||
| 1086 | my ($self, $color) = @_; | ||
| 1087 | $color = $self->{CURRENT} unless defined $color; | ||
| 1088 | return unless $self->validate($color); | ||
| 1089 | $color = $self->convert($color); | ||
| 1090 | my $notationcall = $notationcalls{$self->cget('-notation')}; | ||
| 1091 | return &$notationcall($self, $color); | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | =item B<notationDetect>I<($color)> | ||
| 1095 | |||
| 1096 | Tries to detect the notation of I>$color> and returns | ||
| 1097 | what it finds. Returns undef if it does not detect a valid notation. | ||
| 1098 | |||
| 1099 | =cut | ||
| 1100 | |||
| 1101 | sub notationDetect { | ||
| 1102 | my ($self, $color) = @_; | ||
| 1103 | my $repeat = $self->colordepth / 4; | ||
| 1104 | return 'cmy' if $color =~ /^cmy\(.+\)$/; | ||
| 1105 | return 'cmyX' if $color =~ /^cmy\d+\(.+\)$/; | ||
| 1106 | return 'hex' if $color =~ /^#(?:[0-9a-fA-F]{3}){$repeat}$/; | ||
| 1107 | return 'hsv' if $color =~ /^hsv\(.+\)$/; | ||
| 1108 | return 'rgb' if $color =~ /^rgb\(.+\)$/; | ||
| 1109 | return 'rgbX' if $color =~ /^rgb\d+\(.+\)$/; | ||
| 1110 | return undef | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | =item B<notationHEX>I<($hexcolor)> | ||
| 1114 | |||
| 1115 | Returns a hex formatted string of a hex color. | ||
| 1116 | So basically it just returns it's input. | ||
| 1117 | |||
| 1118 | =cut | ||
| 1119 | |||
| 1120 | sub notationHEX { | ||
| 1121 | my ($self, $hex) = @_; | ||
| 1122 | return $hex; | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | =item B<notationHSV>I<($hexcolor)> | ||
| 1126 | |||
| 1127 | Returns a hsc formatted string of a hex color. | ||
| 1128 | |||
| 1129 | =cut | ||
| 1130 | |||
| 1131 | sub notationHSV { | ||
| 1132 | my ($self, $hex) = @_; | ||
| 1133 | my ($hue, $saturation, $value) = $self->hex2hsv($hex); | ||
| 1134 | $hue = $self->numround($hue, 1); | ||
| 1135 | $saturation = $self->numround($saturation, 3); | ||
| 1136 | $value = $self->numround($value, 3); | ||
| 1137 | return "hsv($hue, $saturation, $value)" | ||
| 1138 | } | ||
| 1139 | |||
| 1140 | =item B<notationRGB>I<($hexcolor)> | ||
| 1141 | |||
| 1142 | Returns a rgb formatted string of a hex color. | ||
| 1143 | |||
| 1144 | =cut | ||
| 1145 | |||
| 1146 | sub notationRGB { | ||
| 1147 | my ($self, $hex) = @_; | ||
| 1148 | my ($red, $green, $blue) = $self->hex2rgb($hex); | ||
| 1149 | my $max = $self->maxChannelValue; | ||
| 1150 | $red = $self->numround($red / $max, 3); | ||
| 1151 | $green = $self->numround($green / $max, 3); | ||
| 1152 | $blue = $self->numround($blue / $max, 3); | ||
| 1153 | return "rgb($red, $green, $blue)" | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | =item B<notationRGBx>I<($hexcolor)> | ||
| 1157 | |||
| 1158 | Returns a rgbX formatted string of a hex color. | ||
| 1159 | |||
| 1160 | =cut | ||
| 1161 | |||
| 1162 | sub notationRGBx { | ||
| 1163 | my ($self, $hex) = @_; | ||
| 1164 | my ($red, $green, $blue) = $self->hex2rgb($hex); | ||
| 1165 | my $depth = $self->cget('-colordepth'); | ||
| 1166 | return "rgb$depth($red, $green, $blue)" | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | sub notationselect { | ||
| 1170 | my ($self, $flag) = @_; | ||
| 1171 | if (defined $flag) { | ||
| 1172 | if ($flag) { | ||
| 1173 | unless (defined $self->Subwidget('NotationSelect')) { | ||
| 1174 | my $fmframe = $self->Frame->pack( | ||
| 1175 | -before => $self->Subwidget('Pick'), | ||
| 1176 | -fill => 'x', | ||
| 1177 | ); | ||
| 1178 | $fmframe->Label( | ||
| 1179 | -anchor => 'e', | ||
| 1180 | -justify => 'right', | ||
| 1181 | -text => 'Format:', | ||
| 1182 | -width => 7, | ||
| 1183 | )->pack(-side => 'left', -padx => 2, -pady => 2); | ||
| 1184 | my $var = ''; | ||
| 1185 | my @menuitems; | ||
| 1186 | for ('cmy', 'cmyX', 'hex', 'hsv', 'rgb', 'rgbX') { | ||
| 1187 | my $t = $_; | ||
| 1188 | push @menuitems, ['command' => $t, | ||
| 1189 | -command => sub { | ||
| 1190 | $var = $t; | ||
| 1191 | $self->configure(-notation => $t) | ||
| 1192 | }, | ||
| 1193 | ]; | ||
| 1194 | |||
| 1195 | } | ||
| 1196 | my $mb = $fmframe->Menubutton( | ||
| 1197 | -anchor => 'w', | ||
| 1198 | -textvariable => $self->{NOTATION}, | ||
| 1199 | )->pack(-side => 'left', -expand => 1, -fill => 'x', -padx => 2, -pady => 2); | ||
| 1200 | $mb->configure(-menu => $mb->Menu( | ||
| 1201 | -tearoff => 0, | ||
| 1202 | -menuitems => \@menuitems, | ||
| 1203 | )); | ||
| 1204 | |||
| 1205 | $self->Advertise('NotationSelect', $fmframe) | ||
| 1206 | } | ||
| 1207 | } else { | ||
| 1208 | if (defined $self->Subwidget('NotationSelect')) { | ||
| 1209 | $self->Subwidget('NotationSelect')->destroy; | ||
| 1210 | $self->Advertise('NotationSelect', undef); | ||
| 1211 | } | ||
| 1212 | } | ||
| 1213 | } | ||
| 1214 | return defined $self->Subwidget('NotationSelect'); | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | sub numround { | ||
| 1218 | my ($self, $number, $decimals) = @_; | ||
| 1219 | my $mult = 10 ** $decimals; | ||
| 1220 | $number = $number * $mult; | ||
| 1221 | $number = round($number); | ||
| 1222 | $number = $number / $mult; | ||
| 1223 | return $number; | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | sub offset { | ||
| 1227 | my ($self, $base, $factor, $div) = @_; | ||
| 1228 | $div = 2 unless defined $div; | ||
| 1229 | $base = $base * 255; | ||
| 1230 | my $new = ($base + $factor)/$div; | ||
| 1231 | $new = $new/255; | ||
| 1232 | return 1 if $new > 1; | ||
| 1233 | return $new | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | sub pickActivate { | ||
| 1237 | my $self = shift; | ||
| 1238 | return if $self->pickInProgress; | ||
| 1239 | my $bindsave = $self->bind('<Button-1>'); | ||
| 1240 | $self->{'_bindsave'} = $bindsave; | ||
| 1241 | $self->{'_cursorsave'} = $self->toplevel->cget('-cursor'); | ||
| 1242 | $self->bind('<Button-1>', [$self, 'pickRelease', Ev('X'), Ev('Y')]); | ||
| 1243 | $self->{'_BE_grabinfo'} = $self->grabSave; | ||
| 1244 | $self->grabGlobal; | ||
| 1245 | $self->toplevel->configure(-cursor => 'crosshair'); | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | sub pickCancel { | ||
| 1249 | my $self = shift; | ||
| 1250 | return unless $self->pickInProgress; | ||
| 1251 | my $bindsave = delete $self->{'_bindsave'}; | ||
| 1252 | $self->bind('<Button-1>', $bindsave); | ||
| 1253 | my $cursor = delete $self->{'_cursorsave'}; | ||
| 1254 | $self->toplevel->configure(-cursor => $cursor); | ||
| 1255 | $self->grabRelease; | ||
| 1256 | if (ref $self->{'_BE_grabinfo'} eq 'CODE') { | ||
| 1257 | $self->{'_BE_grabinfo'}->(); | ||
| 1258 | delete $self->{'_BE_grabinfo'}; | ||
| 1259 | } | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | sub pickInProgress { | ||
| 1263 | return exists $_[0]->{'_bindsave'}; | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | sub pickRelease { | ||
| 1267 | my ($self, $x, $y) = @_; | ||
| 1268 | return unless $self->pickInProgress; | ||
| 1269 | my $img = screenshot; | ||
| 1270 | my $color = $img->getpixel(x => $x, y=> $y); | ||
| 1271 | my $red = $self->hexString($color->red, 8); | ||
| 1272 | my $green = $self->hexString($color->green, 8); | ||
| 1273 | my $blue = $self->hexString($color->blue, 8); | ||
| 1274 | my $hex = $self->convertDepth("#" . $red . $green . $blue); | ||
| 1275 | $self->pickCancel; | ||
| 1276 | $self->UpdateCall($hex); | ||
| 1277 | $self->UpdateAll($hex); | ||
| 1278 | } | ||
| 1279 | |||
| 1280 | sub PostConfig { | ||
| 1281 | my $self = shift; | ||
| 1282 | $self->historyLoad; | ||
| 1283 | $self->historyUpdate; | ||
| 1284 | $self->ConfigMode(0); | ||
| 1285 | $self->sliderheight($self->sliderheight); | ||
| 1286 | my $file = $self->cget('-historyfile'); | ||
| 1287 | } | ||
| 1288 | |||
| 1289 | =item B<put>(I<$color>) | ||
| 1290 | |||
| 1291 | Changes all sliders to match $color | ||
| 1292 | |||
| 1293 | =cut | ||
| 1294 | |||
| 1295 | sub put { | ||
| 1296 | my ($self, $color) = @_; | ||
| 1297 | return unless $self->validate($color); | ||
| 1298 | my $hex = $self->convert($color); | ||
| 1299 | $self->{CURRENT} = $hex; | ||
| 1300 | $self->UpdateAll($hex); | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | =item B<rgb2hex>(I<$red>, I<$green>, I<$blue>, ?I<$depth>?) | ||
| 1304 | |||
| 1305 | Converts the red, green and blue values to a hexstring. | ||
| 1306 | If depth is not specified, B<-colordepth> is used. | ||
| 1307 | |||
| 1308 | =cut | ||
| 1309 | |||
| 1310 | sub rgb2hex { | ||
| 1311 | my ($self, $red, $green, $blue, $depth) = @_; | ||
| 1312 | $red = $self->hexString($red, $depth); | ||
| 1313 | $green = $self->hexString($green, $depth); | ||
| 1314 | $blue = $self->hexString($blue, $depth); | ||
| 1315 | return '#' . $red . $green . $blue; | ||
| 1316 | } | ||
| 1317 | |||
| 1318 | sub rgb2hsv { | ||
| 1319 | |||
| 1320 | # The procedure below converts an RGB value to HSB. It takes red, green, | ||
| 1321 | # and blue components (0-65535) as arguments, and returns a list | ||
| 1322 | # containing HSB components (floating-point, 0-1) as result. The code | ||
| 1323 | # here is a copy of the code on page 615 of "Fundamentals of Interactive | ||
| 1324 | # Computer Graphics" by Foley and Van Dam. | ||
| 1325 | |||
| 1326 | my($self, $red, $green, $blue) = @_; | ||
| 1327 | my($max, $min, $sat, $range, $hue, $rc, $gc, $bc); | ||
| 1328 | |||
| 1329 | #convert to 16 bit; | ||
| 1330 | my $depth = $self->colordepth; | ||
| 1331 | my $mul = 65535/(2**$depth); | ||
| 1332 | my @r = (); | ||
| 1333 | for ($red, $green, $blue) { | ||
| 1334 | push @r, int($_ * $mul) | ||
| 1335 | } | ||
| 1336 | ($red, $green, $blue) = @r; | ||
| 1337 | |||
| 1338 | $max = ($red > $green) ? (($blue > $red) ? $blue : $red) : | ||
| 1339 | (($blue > $green) ? $blue : $green); | ||
| 1340 | $min = ($red < $green) ? (($blue < $red) ? $blue : $red) : | ||
| 1341 | (($blue < $green) ? $blue : $green); | ||
| 1342 | $range = $max - $min; | ||
| 1343 | if ($max == 0) { | ||
| 1344 | $sat = 0; | ||
| 1345 | } else { | ||
| 1346 | $sat = $range / $max; | ||
| 1347 | } | ||
| 1348 | if ($sat == 0) { | ||
| 1349 | $hue = 0; | ||
| 1350 | } else { | ||
| 1351 | $rc = ($max - $red) / $range; | ||
| 1352 | $gc = ($max - $green) / $range; | ||
| 1353 | $bc = ($max - $blue) / $range; | ||
| 1354 | $hue = ($max == $red)?(0.166667*($bc - $gc)): | ||
| 1355 | (($max == $green)?(0.166667*(2 + $rc - $bc)): | ||
| 1356 | (0.166667*(4 + $gc - $rc))); | ||
| 1357 | } | ||
| 1358 | $hue += 1 if $hue < 0; | ||
| 1359 | return ($hue * 360, $sat, $max/65535); | ||
| 1360 | |||
| 1361 | } | ||
| 1362 | |||
| 1363 | sub sliderheight { | ||
| 1364 | my ($self, $height) = @_; | ||
| 1365 | if (defined $height) { | ||
| 1366 | $self->{SLIDERHEIGHT} = $height; | ||
| 1367 | unless ($self->ConfigMode) { | ||
| 1368 | for (qw/Red Green Blue Cyan Magenta Yellow Hue Saturation Value/) { | ||
| 1369 | # for (qw/Red Green Blue/) { | ||
| 1370 | $self->Subwidget($_)->configure('-length' => $height); | ||
| 1371 | } | ||
| 1372 | } | ||
| 1373 | } | ||
| 1374 | return $self->{SLIDERHEIGHT}; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | sub UpdateAll { | ||
| 1378 | my ($self, $value) = @_; | ||
| 1379 | return unless defined $value; | ||
| 1380 | $self->UpdateCMY($value); | ||
| 1381 | $self->UpdateHSV($value); | ||
| 1382 | $self->UpdateRGB($value); | ||
| 1383 | } | ||
| 1384 | |||
| 1385 | sub UpdateCall { | ||
| 1386 | my ($self, $value) = @_; | ||
| 1387 | return if $self->ConfigMode; | ||
| 1388 | $self->{CURRENT} = $value; | ||
| 1389 | my $text = $self->notationCurrent($value); | ||
| 1390 | $self->Callback('-updatecall', $text) if defined $text; | ||
| 1391 | } | ||
| 1392 | |||
| 1393 | sub UpdateCMY { | ||
| 1394 | my ($self, $value) = @_; | ||
| 1395 | my ($cyan, $magenta, $yellow) = $self->hex2cmy($value); | ||
| 1396 | my $max = $self->maxChannelValue; | ||
| 1397 | my $pool = $self->{VARPOOL}; | ||
| 1398 | my $cvar = $pool->{'Cyan'}; | ||
| 1399 | $$cvar = $cyan; | ||
| 1400 | my $mvar = $pool->{'Magenta'}; | ||
| 1401 | $$mvar = $magenta; | ||
| 1402 | my $yvar = $pool->{'Yellow'}; | ||
| 1403 | $$yvar = $yellow; | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | sub UpdateHSV { | ||
| 1407 | my ($self, $val) = @_; | ||
| 1408 | my ($red, $green, $blue) = $self->hex2rgb($val); | ||
| 1409 | |||
| 1410 | my ($hue, $saturation, $value) = $self->rgb2hsv($red, $green, $blue); | ||
| 1411 | |||
| 1412 | my $pool = $self->{VARPOOL}; | ||
| 1413 | my $hvar = $pool->{'Hue'}; | ||
| 1414 | $$hvar = $hue; | ||
| 1415 | my $svar = $pool->{'Saturation'}; | ||
| 1416 | $$svar = $saturation; | ||
| 1417 | my $vvar = $pool->{'Value'}; | ||
| 1418 | $$vvar = $value; | ||
| 1419 | } | ||
| 1420 | |||
| 1421 | sub UpdateRGB { | ||
| 1422 | my ($self, $value) = @_; | ||
| 1423 | my ($red, $green, $blue) = $self->hex2rgb($value); | ||
| 1424 | my $max = $self->maxChannelValue; | ||
| 1425 | my $pool = $self->{VARPOOL}; | ||
| 1426 | my $rvar = $pool->{'Red'}; | ||
| 1427 | $$rvar = $red; | ||
| 1428 | my $gvar = $pool->{'Green'}; | ||
| 1429 | $$gvar = $green; | ||
| 1430 | my $bvar = $pool->{'Blue'}; | ||
| 1431 | $$bvar = $blue; | ||
| 1432 | } | ||
| 1433 | |||
| 1434 | =item B<validate>(?I<$color>?) | ||
| 1435 | |||
| 1436 | Returns true if $color is a valid color. | ||
| 1437 | |||
| 1438 | =cut | ||
| 1439 | |||
| 1440 | sub validate { | ||
| 1441 | my ($self, $val) = @_; | ||
| 1442 | my $not = $self->notationDetect($val); | ||
| 1443 | return 0 unless defined $not; | ||
| 1444 | return 1 if $not eq 'hex'; | ||
| 1445 | my $call = $validatecalls{$not}; | ||
| 1446 | return &$call($self, $val) | ||
| 1447 | } | ||
| 1448 | |||
| 1449 | sub validateCMY { | ||
| 1450 | my ($self, $val) = @_; | ||
| 1451 | return $self->validateSpace($val, 'cmy'); | ||
| 1452 | } | ||
| 1453 | |||
| 1454 | sub validateCMYx { | ||
| 1455 | my ($self, $val) = @_; | ||
| 1456 | return $self->validateSpaceX($val, 'cmy'); | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | sub validateHSV { | ||
| 1460 | my ($self, $val) = @_; | ||
| 1461 | return 0 unless $val =~ /^hsv\((.+)\)$/; | ||
| 1462 | my $parstring = "$1, "; | ||
| 1463 | for (1 .. 3) { | ||
| 1464 | if ($parstring =~ s/^([^,]+),\s*//) { | ||
| 1465 | return 0 unless looks_like_number($1); | ||
| 1466 | return 0 if $1 < 0; | ||
| 1467 | if ($_ eq 1) { | ||
| 1468 | return 0 if $1 > 360 | ||
| 1469 | } else { | ||
| 1470 | return 0 if $1 > 1 | ||
| 1471 | } | ||
| 1472 | } else { | ||
| 1473 | return 0 | ||
| 1474 | } | ||
| 1475 | } | ||
| 1476 | return 1 | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | sub validateRGB { | ||
| 1480 | my ($self, $val) = @_; | ||
| 1481 | return $self->validateSpace($val, 'rgb'); | ||
| 1482 | } | ||
| 1483 | |||
| 1484 | sub validateRGBx { | ||
| 1485 | my ($self, $val) = @_; | ||
| 1486 | return $self->validateSpaceX($val, 'rgb'); | ||
| 1487 | } | ||
| 1488 | |||
| 1489 | sub validateSpace { | ||
| 1490 | my ($self, $val, $space) = @_; | ||
| 1491 | return 0 unless $val =~ /^$space\((.+)\)$/; | ||
| 1492 | my $parstring = "$1, "; | ||
| 1493 | for (1 .. 3) { | ||
| 1494 | if ($parstring =~ s/^([^,]+),\s*//) { | ||
| 1495 | my $number = $1; | ||
| 1496 | return 0 unless looks_like_number($number); | ||
| 1497 | return 0 if $1 < 0; | ||
| 1498 | return 0 if $1 > 1 | ||
| 1499 | } else { | ||
| 1500 | return 0 | ||
| 1501 | } | ||
| 1502 | } | ||
| 1503 | return 1 | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | sub validateSpaceX { | ||
| 1507 | my ($self, $val, $space) = @_; | ||
| 1508 | return 0 unless $val =~ /^$space(\d+)\((.+)\)$/; | ||
| 1509 | my $depth = $1; | ||
| 1510 | my $parstring = "$2, "; | ||
| 1511 | return 0 unless $depth eq $self->cget('-colordepth'); | ||
| 1512 | for (1 .. 3) { | ||
| 1513 | if ($parstring =~ s/^([^,]+),\s*//) { | ||
| 1514 | my $number = $1; | ||
| 1515 | return 0 unless $number =~ /^\d+$/; | ||
| 1516 | return 0 if $number > ((2**$depth) - 1) | ||
| 1517 | } else { | ||
| 1518 | return 0 | ||
| 1519 | } | ||
| 1520 | } | ||
| 1521 | return 1 | ||
| 1522 | } | ||
| 1523 | |||
| 1524 | =back | ||
| 1525 | |||
| 1526 | =head1 LICENSE | ||
| 1527 | |||
| 1528 | Same as Perl. | ||
| 1529 | |||
| 1530 | =head1 AUTHOR | ||
| 1531 | |||
| 1532 | Hans Jeuken (hanje at cpan dot org) | ||
| 1533 | |||
| 1534 | =cut | ||
| 1535 | |||
| 1536 | =head1 BUGS | ||
| 1537 | |||
| 1538 | Switching color depth straight after initialization gives unwanted results. | ||
| 1539 | |||
| 1540 | Cancelling a pick operation only works in the context of L<Tk::ColorEntry>. | ||
| 1541 | |||
| 1542 | =cut | ||
| 1543 | |||
| 1544 | 1; | ||
| 1545 | __END__ |