in reply to Perl/Tk rgb2hex - hex2rgb converter
=head1 NAME Wx::HexOrRgb - a Wx Dialog/App for converting color from/to hex/rgb =head1 SYNOPSIS perl -MWx::HexOrRgb -e Wx::HexOrRgb::App->new()->MainLoop() # or use Wx::HexOrRgb; Wx::HexOrRgb::App->new()->MainLoop(); # or even require Wx::HexOrRgb; system $^X, $INC{'Wx/HexOrRgb.pm'}; # or the oneliner version (quotes may vary ;) perl -mWx::HexOrRgb -e"system $^X, $INC{q{Wx/HexOrRgb.pm}};" # -m is equivalent to use Wx::HexOrRgb(); in case you was wonderin +g =head1 DESCRIPTION Run it as a standalone app, or embed it easily into any wxPerl application, cause you never know when it might come in handy to convert between rgb and hex You probably got Wx::HexOrRgb from http://perlmonks.org/index.pl?node_id=194461 $Id: HexOrRgb.pm,v 0.05 2002/09/1 16:21:00 _ Exp $ =cut package Wx::HexOrRgb; use Wx qw( :everything ); use Wx::Event qw( EVT_RIGHT_DOWN EVT_SLIDER EVT_BUTTON ); use base qw( Wx::Frame ); use vars qw( $VERSION ); use strict; $VERSION = 0.05; sub new { my $class = shift; my $SIZE = [500,160]; my $self = $class->SUPER::new( undef, -1, "Wx::HexOrRgb - you know what it is ;)", [ 50, 50], $SIZE, wxSIMPLE_BORDER # as perl boo_radley's instruction | wxTHICK_FRAME # allows for resizability -- i don't want taht | wxSYSTEM_MENU | wxCAPTION | wxSYSTEM_MENU | wxMINIMIZE_BOX | wxCLIP_CHILDREN, # removes flicker (windows only) ); $self->SetIcon( Wx::GetWxPerlIcon() ); my $p = Wx::Panel->new( $self, -1, [50,50], $SIZE, ); $self->GUI($p); ## and we construct the dialog EVT_BUTTON( $self, $self->ID('HEX2RGB_BUTTON'), \&OnHEX ); EVT_RIGHT_DOWN( $self, \&OnAbout ); EVT_RIGHT_DOWN( $p, \&OnAbout ); EVT_SLIDER( $self, $self->ID('R_SLIDER'), sub { my( $self, $event ) = @_; my $pos = $event->GetInt; $self->OnRGB($pos ); }); EVT_SLIDER( $self, $self->ID('G_SLIDER'), sub { my( $self, $event ) = @_; my $pos = $event->GetInt; $self->OnRGB(undef, $pos ); }); EVT_SLIDER( $self, $self->ID('B_SLIDER'), sub { my( $self, $event ) = @_; my $pos = $event->GetInt; $self->OnRGB(undef, undef, $pos ); }); for my $key( keys %{ $self->ID() } ) { EVT_RIGHT_DOWN( $self->FindWindow( $self->ID($key) ), \&OnAbo +ut ); } my( $MainSizer ) = Wx::BoxSizer->new( Wx::wxHORIZONTAL ); $MainSizer->Add( $p, 1, wxGROW ); $self->SetSizer( $MainSizer ); $self->SetAutoLayout( 1 ); ## ;) $self->Layout(); ##force layout of the children anew $MainSizer->Fit( $self ); $MainSizer->SetSizeHints( $self ); #die Wx::GetColourFromUser( $self ); return $self; } #### THE EVENT HANDLERS sub OnRGB { my( $self, $r, $g, $b ) = @_; $r ||= $self->FindWindow( $self->ID('R_SLIDER') )->GetValue; $g ||= $self->FindWindow( $self->ID('G_SLIDER') )->GetValue; $b ||= $self->FindWindow( $self->ID('B_SLIDER') )->GetValue; my $rgb = $self->FindWindow( $self->ID('RGB_PANEL') ); $rgb->Refresh; # or Clear ;) $rgb->SetBackgroundColour( new Wx::Colour( $r, $g, $b ) ); $self->FindWindow( $self->ID('HEX_TEXT') )->SetValue( sprintf '#%02X%02X%02X', $r, $g, $b ); } sub OnHEX { my( $self, $evt ) = @_; $_ = $self->FindWindow( $self->ID('HEX_TEXT') )->GetValue; my( $r, $g, $b ) = m/\#?([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{2})/i; unless( $r and $g and $b ) { Wx::MessageBox( "Sorry mate, missing a hex value someplace. ($_) Need 6 you know.", "EEEEEEEEK! Now how'd that happen?!?!?", wxICON_ERROR | wxOK, $self, ); return(); }; for ( $r, $g, $b ) { $_ = hex $_; } $self->FindWindow( $self->ID('R_SLIDER') )->SetValue( $r ); $self->FindWindow( $self->ID('G_SLIDER') )->SetValue( $g ); $self->FindWindow( $self->ID('B_SLIDER') )->SetValue( $b ); my $rgb = $self->FindWindow( $self->ID('RGB_PANEL') ); $rgb->Refresh; # or Clear ;) $rgb->SetBackgroundColour( new Wx::Colour( $r, $g, $b ) ); } sub OnAbout { my( $self, $event ) = @_; # display a simple about box (i just keep copying and pasting this) Wx::MessageBox( qq[ ${\__PACKAGE__} $VERSION Created by podmaster of perlmonks.org fame running on wxPerl $Wx::VERSION ${\wxVERSION_STRING()} This program is released under the same terms as perl itsel +f (if you don't know what that means, visit http://perl.com ) To learn more about wxPerl visit http://wxperl.sf.net/ ], "About ${\__PACKAGE__} $VERSION", # TITLE wxOK | wxICON_INFORMATION, $self ); } ##### GGGUI GENERATORs sub BOXS { my( $self, $parent, $str, $orient ) = @_; return Wx::StaticBoxSizer->new( Wx::StaticBox->new( $parent, $self->ID(time.{}.rand), # so I can register OnAbo +ut $str, ,), $orient, ,); } sub ID { ## ALL KEYS ARE UPPERCASED my($self, $key, $dontCreate ) = @_; return $self->{"\0ID_"} if not defined $key; $self->{"\0I"} ||=6660; # the perpetual ID incrementor $key = uc($key); if(exists $self->{"\0ID_"}->{$key} ) { return $self->{"\0ID_"}->{$key}; } else { return 0 if $dontCreate; return $self->{"\0ID_"}->{$key} = ++$self->{"\0I"}; } } sub GUI { my( $self, $parent ) = @_; my $RSizer = $self->BOXS($parent, "v. Red", wxVERTICAL); my $RScroll = Wx::Slider->new( $parent, $self->ID('R_SLIDER'), 255, 0, 255, [-1,-1], [-1,-1], wxSL_HORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS # not nice imho ;( ## wxSB_HORIZONTAL ## no different thant wxSL_HORIZONTAL ); $RSizer->Add( $RScroll, 1, wxGROW | wxALIGN_CENTRE, 0); my $GSizer = $self->BOXS($parent, "v. Green", wxVERTICAL); my $GScroll = Wx::Slider->new( $parent, $self->ID('G_SLIDER'), 255, 0, 255, [-1,-1], [-1,-1], wxSL_HORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS # not nice imho ;( ); $GSizer->Add( $GScroll, 1, wxGROW | wxALIGN_CENTRE, 0); my $BSizer = $self->BOXS($parent, "v. Blue", wxVERTICAL); my $BScroll = Wx::Slider->new( $parent, $self->ID('B_SLIDER'), 255, 0, 255, [-1,-1], [-1,-1], wxSL_HORIZONTAL | wxSL_AUTOTICKS | wxSL_LABELS # not nice imho ;( ); $BSizer->Add( $BScroll, 1, wxGROW | wxALIGN_CENTRE, 0); my $Hex2RGB = Wx::Button->new( $parent, $self->ID('HEX2RGB_BUTTON'), "HEX TO RGB", wxDefaultPosition, wxDefaultSize, wxNO_BORDER ); my $HexText = Wx::TextCtrl->new( $parent, $self->ID('HEX_TEXT'), '#FFFFFF', [-1,-1], [-1,-1], ); my $StaticLine = new Wx::StaticLine( $parent, $self->ID('HEX_LINE'), [-1,-1], [-1,-1], wxLI_HORIZONTAL | wxSIMPLE_BORDER | wxCLIP_CHILDREN ); warn $_.' '.$StaticLine->GetBackgroundColour->$_ for qw/Red Green +Blue/; warn $_.' '.$StaticLine->GetForegroundColour->$_ for qw/Red Green +Blue/; $StaticLine->Show(1); # won't cut it, but a string will, imagine t +hat?? $StaticLine->Show('string'); ## the following don't do anything, WTF??? $StaticLine->Refresh(); $StaticLine->Clear(); $StaticLine->SetBackgroundColour( new Wx::Colour(0,0,255) ); # RED $StaticLine->SetForegroundColour( new Wx::Colour(0,0,255) ); warn $_.' '.$StaticLine->GetBackgroundColour->$_ for qw/Red Green +Blue/; warn $_.' '. $StaticLine->GetForegroundColour->$_ for qw/Red Green + Blue/; my $ButtonSizer = Wx::BoxSizer->new( wxHORIZONTAL ); $ButtonSizer->Add( $Hex2RGB, 0, wxCENTRE | wxALIGN_LEFT | wxALL, 5 + ); $ButtonSizer->Add( $StaticLine, 1, wxCENTRE | wxGROW, 5 ); $ButtonSizer->Add( $HexText, 0, wxCENTRE | wxALIGN_RIGHT| wxALL, 5 + ); my $RGBSizer = Wx::BoxSizer->new( wxVERTICAL ); $RGBSizer->Add( $RSizer, 1, wxGROW | wxALIGN_CENTRE, 5 ); $RGBSizer->Add( $GSizer, 1, wxGROW | wxALIGN_CENTRE, 5 ); $RGBSizer->Add( $BSizer, 1, wxGROW | wxALIGN_CENTRE, 5 ); $RGBSizer->Add( $ButtonSizer, 1, wxGROW | wxALIGN_CENTRE, 5 ); my $RGBPanel = Wx::Panel->new( $parent, $self->ID('RGB_PANEL'), [-1, -1], [100, -1], ); $RGBPanel->SetBackgroundColour( new Wx::Colour(255, 255, 255) ); # + white my $RGBPanelSizer = $self->BOXS($parent, "h. RGB", wxHORIZONTAL); $RGBPanelSizer->Add( $RGBPanel, 1, wxGROW | wxALIGN_CENTRE, 5 ); my $RootSizer = Wx::BoxSizer->new( wxHORIZONTAL ); $RootSizer->Add( $RGBSizer, 1, wxGROW | wxALIGN_CENTRE, 5 ); $RootSizer->Add( $RGBPanelSizer, 0, wxGROW | wxALIGN_CENTRE, 5 ); $parent->SetAutoLayout( 1 ); $parent->SetSizer( $RootSizer ); # $RootSizer->Fit( $parent ); # cause I don't want it fitted $RootSizer->SetSizeHints( $parent ); return $RootSizer; } package Wx::HexOrRgb::App; use strict; use Wx; use base qw(Wx::App); sub OnInit { my( $self ) = @_; my( $frame ) = new Wx::HexOrRgb(); $frame->Show(1); $frame->Refresh(); 1; } package main; # if this file is invoked directly (not use'd), run the app unless( caller() ) { Wx::HexOrRgb::App->new()->MainLoop(); } __END__ =head1 AUTHOR podmaster - http://perlmonks.org/index.pl?node=podmaster =head1 LICENSE Copyright D.H ( podmaster ) http://crazyinsomniac.perlmonk.org 2002, All rights reserved. This program is released under the same terms as perl itself (if you don't know what that means, visit http://perl.com ) =cut http://perlmonks.org/index.pl?node_id=155288;#Holy Getopt::Long, Pod:: +Usage Man!
____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: ( Wx::HexOrRgb ) Re: Perl/Tk rgb2hex - hex2rgb converter
by PodMaster (Abbot) on Sep 04, 2002 at 04:49 UTC |