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

Hi folks,

I'm having some trouble allocating colors with GD. Right now, I have the following code:
my %angles; #I get angles from a file my ($key1, $key2, @colors); my ($i, $j) = 0; my $red_val= 0; my $yellow_val = 90; my $green_val = 180; my $angle; my $im = new GD::Image (150, 1500); foreach $key1 (sort (keys %angles)) { foreach $key2 (sort (keys %{$angles{$key1}})) { $angle = ${$angles{$key1}}{$key2} if ($angle <= $yellow_val) { $colors[$i]->[$j] = $im->colorAllocate(255, 255*(($ang +le-$red_val)/($yellow_val-$red_val)), 0); } else { $colors[$i]->[$j] = $im->colorAllocate(255*(($green_v +al - $angle)/($yellow_val - $red_val)), 255*(1-(1-($angle-$green_val) +/($yellow_val-$green_val)))*.5, 0); } $j++; } $j = 0; $i++; }
My problem is that when I allocate all these colors (%angles contains about 1000 values) the $im turns a funny color, and I can't print anything. What I'm trying to do is print 1000 of these circles out onto a file and display it. Thanks for any help you can give!

Replies are listed 'Best First'.
Re: Allocating colors with GD
by leriksen (Curate) on May 19, 2003 at 03:12 UTC
    Could be an issue with the color map - investigate GD::Image->trueColor(1) or GD::Image->new(h,w,1) note also, GD::colorAllocate returns -1 if no color is allocated - check your return values - always (regardless whether your working in Perl/C/Java/BrainF*ck/whatever)

    You could try allocating more and more colors until colorAllocate starts returning -1 to check the size of your color map - though I think it also depends on the color maps your apps run, leading some apps to declare private color maps

      Some versions of GD (don't know if newer or older) seem not to have the class method trueColor(), so there an error arises. Therefor, I always pack the trueColor call into an eval:
      eval { GD::Image->trueColor(1) }; if ($@) { # e.g. write to log that this method is not existing }

      Best regards,
      perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"

Re: Allocating colors with GD
by Anonymous Monk on May 19, 2003 at 21:44 UTC
    Yeah, it sounds like a color table problem. You probably are in a 256-size 8-bit-palette color-space and are trying to allocate 1000 unique colors. GD by default creates palette-based images. You need to either
    1. create a TrueColor (24-bit RGB color) image, which requires a newer libgd and GD.pm. See GD.pm home manual under new(), trueColor() and newTrueColor() and search for palette.
    2. or use colorResolve and colorClosest to map your 1000 objects into the 256 available palette slotes. Rather than use colorAllocation, I use colorResolve(@rgb)||colorClosest(@rgb) in my mk_color() utility function. Just don't let the table fill with 256 shades of brick red first.
    The prior-comment about eval'ing trueColor to catch failure to make portable scripts take advantage of new features is right on target for your problem. (It's also a generalizable strategy for compatibility and extension for other modules and features, too.)

    Good luck, enjoy -- I'm having fun with GD, hope you will too.

    Bill n1vux
    I had a .sig when they wasted usenet bandwidth, now I make it up as I go along.