hesco has asked for the wisdom of the Perl Monks concerning the following question:
Tonight, though, I'm seeing a usage message when I try to feed a custom color into the mix. Only I don't see how my use of the ->colorAllocate() method does not confirm to the spec presented in the usage message. And for some strange reason, my system gives me no perldoc for GD::Image.
My code complains:
and looks like this:Usage: GD::Image::colorAllocate(image, r, g, b) at /home/hesco/sb/GD-G +raph-Thermometer/lib/GD/Graph/Thermometer.pm line 133.
I'm invoking this module as follows:package GD::Graph::Thermometer; use warnings; use strict; use Carp; use GD; use GD::Text::Align; use Data::Dumper; use constant PI => 3.14; sub new { my $self = shift; my $args = shift; . . . my $image = new GD::Image($args->{width},$args->{height}); my $colors = $self->_define_colors($image, { background_color => $args->{'background_color'}, outline_color => $args->{'outline_color'}, text_color => $args->{'text_color'}, mercury_color => $args->{'mercury_color'} }); . . . return; } sub _define_colors { my $self = shift; my $image = shift; my $custom_colors = shift; print STDERR Dumper($self,$image,$custom_colors); . . . my $text_color; if (defined($custom_colors->{text_color})) { $text_color = $image->colorAllocate($custom_colors->{text_color}); } else { $text_color = $self->_black($image); } . . . return $colors; }
And finally the output of my Data::Dumper invocation:#!/usr/bin/perl -w use strict; use warnings; use lib qw{/home/hesco/sb/GD-Graph-Thermometer/lib}; use GD::Graph::Thermometer; my $result = GD::Graph::Thermometer->new({ image_path => '/home/hesco/GD-Thermometer.png', goal => '80000', current => '50000', title => 'Funding the League in 2007 ($)', width => '100', height => '200', # background_color => '', text_color => [0,0,255], # outline_color => '', # mercury_color => '' }); 1;
$ ./render_thermometer.pl $VAR1 = 'GD::Graph::Thermometer'; $VAR2 = bless( do{\(my $o = 137557384)}, 'GD::Image' ); $VAR3 = { 'mercury_color' => undef, 'background_color' => undef, 'outline_color' => undef, 'text_color' => [ 0, 0, 255 ] }; Usage: GD::Image::colorAllocate(image, r, g, b) at /home/hesco/sb/GD-G +raph-Thermometer/lib/GD/Graph/Thermometer.pm line 133.
I'd certainly appreciate someone with a clue on how to use this method speaking up to show me how. I've tried to pass that array of rgb values in every which way I know how. But nothing seems to work.
Thanks,
-- Hugh
UPDATE
Success!
As suggested in the replies below, I had tried dereferencing the array to feed it into the ->colorAllocate method, without success. The error messages read:
Even when I tried my best to feed it an array reference. But here is the code which worked, inserting into the module itself, at line 133:Not an ARRAY reference at /home/hesco/sb/GD-Graph-Thermometer/lib/GD/G +raph/Thermometer.pm line 133.
That code, along with the proper arguments sent to my constructor, turned my text labels and title blue. Next I propogate what I learned to the other code that is similarly broken.my $text_color; if (defined($custom_colors->{text_color})) { $text_color = $image->colorAllocate( $custom_colors->{text_color}[0], $custom_colors->{text_color}[1], $custom_colors->{text_color}[2] ); } else { $text_color = $self->_black($image); }
Thank you all for your thoughts and input.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: GD::Image::colorAllocate errors
by jettero (Monsignor) on Jan 03, 2007 at 13:48 UTC | |
|
Re: GD::Image::colorAllocate errors
by polettix (Vicar) on Jan 03, 2007 at 15:07 UTC | |
|
Re: GD::Image::colorAllocate errors
by Anonymous Monk on Jan 03, 2007 at 13:44 UTC | |
|
Re: GD::Image::colorAllocate errors
by Anonymous Monk on Jan 03, 2007 at 13:47 UTC |