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

Hey Monks!

I'm using the GD mods and creating some web images. I would like to do a gradient on some of them, but really have no clue as to the methods of doing this. If someone knows of some examples or a good book which works with this subject could you let me know? Or even just the calculation involved in going from one color to another in a clean fashion might be enough to get me started.

Thanks in advance

Glenn H.

Replies are listed 'Best First'.
(jeffa) Re: Gradient Images
by jeffa (Bishop) on Mar 23, 2002 at 08:20 UTC
Re: Gradient Images
by YuckFoo (Abbot) on Mar 23, 2002 at 20:14 UTC
    webadept,

    A simple way is just to interpolate (separately) the red, green, and blue components of the first and last colors, by determining how much they should change at each step of the gradient.

    YuckFoo

    #!/usr/bin/perl use strict; # Desired number of steps my $steps = 16; # RGB components of random beginning and ending colors my @beg = (int(rand(256)), int(rand(256)), int(rand(256))); my @end = (int(rand(256)), int(rand(256)), int(rand(256))); # RGB rate of change from beginning to ending color my @delta = ( ($end[0] - $beg[0]) / ($steps-1), ($end[1] - $beg[1]) / ($steps-1), ($end[2] - $beg[2]) / ($steps-1), ); # Calculate colors using beginning color and rate of change for my $i (0..$steps-1) { printf("%3d %3d %3d %3d\n", $i, $beg[0] + $i * $delta[0], $beg[1] + $i * $delta[1], $beg[2] + $i * $delta[2]); }
Re: Gradient Images
by grinder (Bishop) on Mar 23, 2002 at 18:10 UTC
    And here is another variant: RGBgradient.

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Gradient Images
by webadept (Pilgrim) on Mar 24, 2002 at 06:27 UTC
    Thanks to all of you.. I think I can work with these and get started.

    Glenn H.