hesco has asked for the wisdom of the Perl Monks concerning the following question:

I'm back to work on my thermometer graph generation code. Our launch date on this project is now bearing down and I need to wrap this up. Its mostly working now, but I'm working to add a few bells and whistles to the module. I had asked some questions that got me this far about two weeks ago: Seeking GD::Graph like goal-thermometer graph.

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:

Usage: GD::Image::colorAllocate(image, r, g, b) at /home/hesco/sb/GD-G +raph-Thermometer/lib/GD/Graph/Thermometer.pm line 133.
and looks like this:

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; }
I'm invoking this module as follows:

#!/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;
And finally the output of my Data::Dumper invocation:

$ ./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:

Not an ARRAY reference at /home/hesco/sb/GD-Graph-Thermometer/lib/GD/G +raph/Thermometer.pm line 133.
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:

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); }
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.

Thank you all for your thoughts and input.

if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: GD::Image::colorAllocate errors
by jettero (Monsignor) on Jan 03, 2007 at 13:48 UTC

    It might help to try to kinda cut down the volume of code to the relevant parts. I suppose it's hard to know what's relevant though. Personally, I think it's this.

    $text_color = $image->colorAllocate($custom_colors->{text_color});

    According to the GD docs, colorAllocate takes three args, not one. Presumably $custom_colors->{text_color} is a scalar arrayref. If that's the case, you'll want to explode it over the callstack like so.

    $text_color = $image->colorAllocate(@{ $custom_colors->{text_color} } +);

    If, on the other hand, your text color is a webified color (e.g., "#ffff00"), you'd do something like this instead

    if( $custom_colors->{text_color} =~ m/#(..)(..)(..)/ ) { $text_color = $image->colorAllocate(map {hex($_)} ($1, $2, $3) ) }

    -Paul

Re: GD::Image::colorAllocate errors
by polettix (Vicar) on Jan 03, 2007 at 15:07 UTC
    When posting errors referring to a line, I'd at least put a comment about where the line is. Just call it "clue exchange" :)
    And for some strange reason, my system gives me no perldoc for GD::Image.
    The documentation is in the main GD module, don't ask me why. You'll find a section devoted to GD::Image and, in this section, a subsection describing Color Control.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: GD::Image::colorAllocate errors
by Anonymous Monk on Jan 03, 2007 at 13:44 UTC
    You're only passing one argument (r), example
    my @FOO = ( 1, 2, 3 ); my $foo = [ 1, 2, 3]; warn @FOO; die $foo; __END__ 123 at - line 5. ARRAY(0x224f5c)
Re: GD::Image::colorAllocate errors
by Anonymous Monk on Jan 03, 2007 at 13:47 UTC
    $text_color = $image->colorAllocate( @{ $custom_colors->{text_color} } );