This is the new and improved version of the converter with a streamlined interface (using sliders).

#!/usr/local/bin/perl -w use strict; use diagnostics; use Tk; use Tk::Dialog; my $mw = MainWindow->new; $mw->geometry("500x225+400+400"); $mw->minsize(qw(500 225)); $mw->maxsize(qw(500 225)); $mw->title("Color Viewer"); my $leftFrm = $mw->Frame(-width => 250, -height => 225, -borderwidth => 2, -relief => 'sunken') ->pack(-side => 'left', -fill => 'y'); my $rightFrm = $mw->Frame(-width => 300, -height => 225, -borderwidth => 2, -relief => 'sunken') ->pack(-side => 'left'); my $redscale = $leftFrm->Scale(-label => 'Red (R)', -orient => 'horizontal', -from => 0, -to => 255, -width => 10, -sliderlength => 15, -length => 200, -foreground => 'red', -command => \&rgb2hex)->pack(-side => 'top'); my $greenscale = $leftFrm->Scale(-label => 'Green (G)', -orient => 'horizontal', -from => 0, -to => 255, -width => 10, -sliderlength => 15, -length => 200, -foreground => 'darkgreen', -command => \&rgb2hex)->pack(-side => 'top'); my $bluescale = $leftFrm->Scale(-label => 'Blue (B)', -orient => 'horizontal', -from => 0, -to => 255, -width => 10, -sliderlength => 15, -length => 200, -foreground => 'blue', -command => \&rgb2hex)->pack(-side => 'top'); my $hexLabel1 = $leftFrm->Label(-text => 'WWW Hex Code', -pady => 8) ->pack(-side => 'top'); my $leftFrm1 = $leftFrm->Frame->pack(-fill => 'x'); my $hexValueEnt = $leftFrm1->Entry(-width => 10) ->pack(-side => 'left', pady => 2, padx => 2); $hexValueEnt->insert(0, "#000000"); my $btnHexToRGB = $leftFrm1->Button(-text => 'Hex -> RGB', -activebackground => 'purple', -highlightbackground => 'purple', -command => \&hex2rgb) ->pack(-side => 'left', pady => 2, padx => 2); # Add a dialog widget my $dialog = $mw->Dialog; MainLoop; sub rgb2hex { my $red = $redscale->get(); my $green = $greenscale->get(); my $blue = $bluescale->get(); my $color = sprintf("#%02X%02X%02X", $red, $green, $blue); $hexValueEnt->delete(0, 'end'); $hexValueEnt->insert(0, $color); setcolor(); } # Converts the hex value to rgb sub hex2rgb { my $hex = $hexValueEnt->get(); if ($hex =~ m/^\#[0-9a-fA-F]+$/ and length($hex) == 7) { my @rgb = split(//, $hex); my $red = hex($rgb[1].$rgb[2]); $redscale->set($red); my $green = hex($rgb[3].$rgb[4]); $greenscale->set($green); my $blue = hex($rgb[5].$rgb[6]); $bluescale->set($blue); setcolor(); } else { $dialog->configure(-title => 'Invalid Entry', -text => 'Invali +d hex code value.'); $dialog->Show; } } # Resets the background color of the bottom frame sub setcolor { $rightFrm->configure(-background => $hexValueEnt->get()); } __DATA__

This is the original script.

#!/usr/local/bin/perl -w use strict; use diagnostics; use Tk; use Tk::Dialog; sub rgbtohex; # Create main window my $mw = MainWindow->new; $mw->geometry("250x175+400+400"); $mw->minsize(qw(250 175)); $mw->maxsize(qw(250 175)); $mw->title("Hex<-->RGB Converter"); # Create a main frame for the window. my $mainFrame = $mw->Frame(-width => 250, -height => 100, -borderwidth => 2, -relief => 'raised')->pack(-side => 'top', -fill=>'x'); # Create a bottom frame to display the converted color. my $bottomFrm = $mw->Frame( -width =>250, -height => 100, -borderwidth => 2, -relief => 'ridge', -background => 'black')->pack(-side => 'top'); # Add child frames to the main frame my $leftFrm = $mainFrame->Frame(-height =>100, -borderwidth => 2, -relief => 'raised')->pack(-side => 'left', -fill=>'y'); my $midFrm = $mainFrame->Frame(-height =>100) ->pack(-side => 'left', -fill=>'y', -padx => 8, -pady => 8); my $rightFrm = $mainFrame->Frame(-height =>100) ->pack(-side => 'left', -fill=>'y'); # Add the label and entry box for red value my $leftFrm1 = $leftFrm->Frame->pack(-fill=>'x', pady => 4, padx => 4) +; my $lblRed = $leftFrm1->Label(-text => 'R:', -foreground => 'red') ->pack(-side => 'left'); my $redEnt = $leftFrm1->Entry(-width => 8, -background => 'white') ->pack(-side => 'left'); $redEnt->insert(0, 0); # Add the label and entry box for green value my $leftFrm2 = $leftFrm->Frame->pack(-fill=>'x', pady => 4, padx => 4) +; my $lblGreen = $leftFrm2->Label(-text => 'G:', -foreground => 'darkgre +en') ->pack(-side => 'left'); my $greenEnt = $leftFrm2->Entry(-width => 8, -background => 'white') ->pack(-side => 'left'); $greenEnt->insert(0, 0); # Add the label and entry box for blue value my $leftFrm3 = $leftFrm->Frame->pack( -fill=>'x', pady => 4, padx => 4 +); my $lblBlue = $leftFrm3->Label(-text => 'B:', -foreground => 'blue') ->pack(-side => 'left'); my $blueEnt = $leftFrm3->Entry(-width => 8, -background => 'white') ->pack(-side => 'left'); $blueEnt->insert(0, 0); # Add the "convert" buttons my $midFrm1 = $midFrm->Frame->pack(-expand => 1, -anchor => 'center'); my $convertToHex = $midFrm1->Button(-text => '>>', -activebackground => 'purple', -highlightbackground => 'purple', -command => \&rgb2hex)->pack; my $convertToRGB = $midFrm1->Button(-text => '<<', -activebackground => 'purple', -highlightbackground => 'purple', -command => \&hex2rgb)->pack; # Add the label and entry box for the hex value my $lblHex = $rightFrm->Label(-text => 'Hex Code:')->pack(-side => 'le +ft'); my $hexEnt = $rightFrm->Entry(-width => 8, -background => 'white')->pack(-side => 'left'); $hexEnt->insert(0, "#000000"); # Add a dialog widget my $dialog = $mw->Dialog; MainLoop; # Converts the three rgb numbers into hex sub rgb2hex { my $red = $redEnt->get(); my $green = $greenEnt->get(); my $blue = $blueEnt->get(); my $pattern = "^[0-9]+\$"; my $showerror = 0; if (($red =~ m/$pattern/) and ($green =~ m/$pattern/) and ($blue = +~ m/$pattern/)) { if(($red >= 0 and $red <= 255) and ($green >= 0 and $green <= +255) and ($blue >= 0 and $blue <= 255)) { my $hex = sprintf("#%02X%02X%02X", $red, $green, $bl +ue); $hexEnt->delete(0, 'end'); $hexEnt->insert(0, $hex); setcolor(); } else { $showerror = 1; } } else { $showerror = 1; } if($showerror) { $dialog->configure(-title => 'Invalid Entry', -text => 'Invali +d RGB values. Each value must be between 0 and 255.'); $dialog->Show; } } # Converts the hex value to rgb sub hex2rgb { my $hex = $hexEnt->get(); if ($hex =~ m/^\#[0-9a-fA-F]+$/ and length($hex) == 7) { my @rgb = split(//, $hex); my $red = hex($rgb[1].$rgb[2]); $redEnt->delete(0, 'end'); $redEnt->insert(0, $red); my $green = hex($rgb[3].$rgb[4]); $greenEnt->delete(0, 'end'); $greenEnt->insert(0, $green); my $blue = hex($rgb[5].$rgb[6]); $blueEnt->delete(0, 'end'); $blueEnt->insert(0, $blue); setcolor(); } else { $dialog->configure(-title => 'Invalid Entry', -text => 'Invali +d hex code value.'); $dialog->Show; } } # Resets the background color of the bottom frame sub setcolor { $bottomFrm->configure(-background => $hexEnt->get()); } __DATA__

In reply to Perl/Tk rgb2hex - hex2rgb converter by Lysander

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.