in reply to Re: color selection image map
in thread color selection image map

It turns out that ImageMagick makes this amazingly easy. They have a built in 'netscape' color cube image that just builds the grid. Here's the small chunk of code that does that:

use Image::Magick; # I actually use a wrapper here to set the env. my $status; my $image = Image::Magick->new(); $status = $image->Read("netscape:"); warn $status if ($status); $status = $image->Scale('256x256+256'); warn $status if ($status); $status = $image->Write('background.gif'); ('map:../../htdocs/background.map'); warn $status if ($status); $status = $image->Write('html:../../htdocs/background.html'); warn $status if ($status);

You can also create the associated map, but I just used standard Perl CGI to generate a page using the image I created and field the image map params coming back. It looks at the coordinates and again uses ImageMagick to query the image's color at the location, convert that to hex and show the page again.

Here's the CGI code ... a little fugly but I can clean it up later, right? 8-) This may not work as is -- there was another proprietary piece I can really put out for public consumption.

#!/usr/local/bin/perl use CGI qw/:standard/; use Image::Magick; use URI::Escape; my $color; my $hex_color = '#ffffff'; my $esc_color; my $status; sub hex_color { my ($number, $hex); (shift @_) =~ /^(\d+),(\d+),(\d+)/; foreach $number ($1, $2, $3) { $hex .= sprintf("%02X", 0xff & $number); } return '#' . $hex; } if (param()) { if (defined(param('hex_text'))) { $hex_color = param('hex_value'); $hex_color = '#' . $hex_color if ($hex_color !~ /^\#/); } else { my ($x, $y) = (param('color.x'), param('color.y')); my $image = Image::Magick->new(); $status = $image->read("../htdocs/background.gif"); print STDERR "$status\n" if ($status); $color = $image->Get("pixel[$x, $y]"); $hex_color = hex_color($color); } } $esc_color = uri_escape($hex_color); my $instructions = <<"END_TEXT"; <P><I>Click the color map or enter a specific hex color in the text bo +x.</I> </P> END_TEXT print header, start_html({-bgcolor => substr($hex_color, 1)}, 'Background Color Tester'), h1({-align => 'center'}, "Background Color Tester"), h2({-align => 'center'}, "Hex Color Value Is: $hex_color"), h3({-align => 'center'}, $instructions), start_form(), table({-align => 'center'}, Tr({-align => 'center'}, td({-align => 'center'}, image_button(-name => 'color', -src => '/background.gif', -width => 256, -height => 171, -align => 'MIDDLE', ), ), ), ), table({-align => 'center'}, Tr({-align => 'center'}, td({-align => 'center'}, textfield(-name => 'hex_value', -size => 7, ), ), td({-align => 'center'}, submit (-value => 'Hex Value', -name => 'hex_text', ), ), ), ), end_form(), end_html();

Replies are listed 'Best First'.
Re: Re: Re: color selection image map
by steves (Curate) on May 24, 2002 at 12:47 UTC

    I also implemented a version that's entirely client side image maps and JavaScript, which actually fits the need a little better. No Perl in that one, but I thought I'd mention it as a final follow-up. I know all the evils of JavaScript, but in this case it makes sense ...