Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

allocating/deallocating colors with GD module.

by Anonymous Monk
on Jun 24, 2013 at 17:07 UTC ( #1040456=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm making some graphics wherein I want to loop through the RedGreenBlue color-components. Sometimes I can simply write something like:

$x = 20; $white=$img->colorAllocate(255,$x*2,255); $img->line(0,0,50,50,$white); $white = $img->colorAllocate(255,$x*10,150); $img->line(50,50,150,250,$white);

And then it works. Sometimes this doesn't work so i try to deallocate f.e.:

$blue = $img->colorAllocate(0,0,25); $img->colorDeallocate($blue); $blue = $img->colorAllocate(0,0,25*$teller);

But this doesn't work either. Can someone tell me how to do this? Thank you.

Comment on allocating/deallocating colors with GD module.
Select or Download Code
Re: allocating/deallocating colors with GD module.
by Loops (Hermit) on Jun 24, 2013 at 17:10 UTC

    It would be helpful to give a bit more detail about what you mean when you say it doesn't work. What specifically doesn't work? Are you sure that your calculated values are within bounds?

Re: allocating/deallocating colors with GD module.
by moritz (Cardinal) on Jun 24, 2013 at 17:16 UTC

    From the little information you have given us, I'd guess that the arithmetic expressions in the arguments to colorAllocate return values outside the range 0..255.

    But please provide a runnable, minimal example that demonstrates how stuff doesn't work.

      I'm making a julia fractal, i've added the code:

      use GD; use Math::Complex; $img = new GD::Image(400,400); $black = $img->colorAllocate(0,0,0); $bl = $img->colorAllocate(0,0,50); $blue = $img->colorAllocate(0,0,50); $c = cplx(-0.70,-0.38); for($x=-200;$x<=200;$x++){ for($y=-200;$y<=200;$y++){ $z = cplx($x/100,$y/100); $teller = 0; che(); } } binmode STDOUT; print $img->png; sub che(){ $zz = $z**2 + $c; if(abs($zz)>2){ if($teller<=3){ $img->setPixel(200+$x,200+$y,$blue);} else{ $img->colorDeallocate($bl); $bl = $img->colorAllocate(0,0,25*$teller); $img->setPixel(200+$x,200+$y,$bl);} } elsif(abs($zz<2)){ if($teller<8){ $teller++; $z = $zz; che(); } } }

      So after 3 iterations i want the blue component($bl) to be 25 * $teller, so a maximum of 200. But on the picture the blue becomes brighter(must be 100) only once. As if it deallocated once(it was 50 to start with, as you can see at the beginning) and not every time that part of code is executed. So my question is: can you deallocate a color and then allocate it again like in my program. And is this really necessary, because like i said in my original question sometimes you can simply allocate it with different rgb-components without deallocating it first. Thank you.

Re: allocating/deallocating colors with GD module.
by BrowserUk (Pope) on Jun 24, 2013 at 21:13 UTC

    First, it seems a little weird to reallocate $white to some other color; and especially weird that neither of them are actually white.

    Secondly, if you deallocate a color, you remove it from the images color table, which means any pixels drawn in that color would now be displayed in whatever new color you allocate to that entry, which is going to produce weird results.

    If you insist on using paletted images, you should chose all your colors, allocate them to different variables and then use them. Not reallocate or deallocate them.

    But paletted images are just a pain in the butt and relics of a bygone era. You'd be much better off using TrueColor (24-bits per perl) images, then you can forget about allocating colors completely and just use the rgb value converted to a suitable integer. I use the following simple function for this:

    sub rgb2n { unpack 'N', pack 'CCCC', 0, @_ }

    See ColorRamp1785 for usage.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thank you for your explanation. Best regards.

Re: allocating/deallocating colors with GD module.
by Anonymous Monk on Jul 21, 2013 at 08:13 UTC

    This works faster:

    use GD; $img = new GD::Image(400,400); $blue = $img->colorAllocateAlpha(10,255,60,45); $black = $img->colorAllocate(0,0,0); $c = -0.70; $i = -0.38; for($x=-200;$x<200;$x++){ for($y=-200;$y<200;$y++){ $rx = $x/100; $ry = $y/100; $teller = 0; lab: $a = $rx**2 + (($ry**2)*-1) +$c; $b = (2*$rx*$ry)+$i; $abs = sqrt($a**2 + $b**2); if($abs>2){ $img->setPixel(200+$x,200+$y,$black);} else{ if($teller<20){ $teller++; $rx = $a; $ry = $b; goto lab; } } } } binmode STDOUT; print $img->png;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040456]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-10-22 07:57 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (114 votes), past polls