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

Goodly Monks:

I've been using GD and GD::Graph successfully for some time, but I've recently run into a bug, either in my understanding or in one of the utilities. ;-)
use GD; use GD::Graph::linespoints; use GD::Graph::bars3d; @colors = ('red', 'green', 'blue', 'gold', 'magenta'); @colorvals = ([255,0,0], [0,204,0],[0,0,255],[255,204,0],[255,0,255] +); . . . $linechart = GD::Graph::linespoints->new(640, 640) or die "no char +t!\n"; $linechart->set( # dclrs => [@colors], line_width => 3, x_label => 'Domain of ' . uc($fdat{domain}) . 's', y_label => 'Mean Value in ' . $ulong, title => "Test: '" . $fdat{testn} . "' Compared by +" . uc($fdat{compare}), x_labels_vertical => 1) or die $linechart->error; $linechart->set_x_axis_font(gdSmallFont); $linechart->set_y_axis_font(gdSmallFont); $gd = $linechart->plot(\@gdata) or die $linechart->error; . . . $legend = new GD::Image($gdwidth,$gdheight); $legend->transparent($legend->colorAllocate(255,255,255)); $legend->rectangle(0,0,$gdwidth-1,$gdheight-1,$legend->colorAlloca +te(0,0,0)); for ($cn=0;$cn<@comps;$cn++) { . . . $thiscolor = $legend->colorAllocate($colorvals[$cn][0],$colorval +s[$cn][1],$colorvals[$cn][2]); $legend->string(gdSmallFont,2,5+($cn*18),$rowdata,$thiscolor); }
I see two behaviors that are undesirable. If I uncomment the dclrs line in the first set() command, sometimes the whole graph will fail claiming 'Illegal modulus zero in line 443 of GD.pm'. I've tried setting GD::Image to truecolor status, changing my colors to websafe, etc., but as soon as I try setting that palette myself, it bombs erratically. Often, a simple refresh will clear it, too.

The second bug that shows up seems to be related. In this case, I'm crating a GD image directly, without using GD::Graph. If I have five lines with different colors, all color allocation gets turned off and everything comes out in black.

Does anybody have experience with such a problem?

Replies are listed 'Best First'.
Re: GD and colors
by waswas-fng (Curate) on Jun 28, 2005 at 17:05 UTC
    Few things, its not line 443 of GD.pm, its line 443 of Graph.PM. And from the GD::Graph POD...
    =item dclrs (short for datacolours) This controls the colors for the bars, lines, markers, or pie slices. This should be a reference to an array of colour names as defined in L<GD::Graph::colour> (S<C<perldoc GD::Graph::colour>> for the names av +ailable). $graph->set( dclrs => [ qw(green pink blue cyan) ] );
    This makes your color choices fail because later on in Graph you there is a line (at line # 443) that does a:
    _rgb($self->{dclrs}[$_[0] % @{$self->{dclrs}} - 1]);
    Update:

    If you want to specifiy colors outside of the redefined list in GD::Graph::colours, you must use:
    add_colour(colourname => [$r, $g, $b]) or add_colour('#7fe310') Self-explanatory. Exported with the :colours tag.


    -Waswas
      Note to self: R{more of}TFM! Thank you!!!