use constant LIMIT => 20; ## It's a constant. (Should do the same for 4) my @palette = Imager::Color->new( 0,0,0 ); ## Set the first color to black avoids the need for $div ... my ( $re_z, $im_z, iterations ) = (0) x 3; while ( 1 ) { my $saved = $re_z; ## It's just saving a value for use later in the calculation; $temp might also do. $re_z = $re_z * $re_z - $im_z * $im_z + $re_c; $im_z = 2 * $saved * $im_z + $im_c; ++$iterations; last if $re_z * $re_z + $im_z * $im_z > 4; ## Exit and use the iteration count to pick a color $iterations = 0, last if $iterations == LIMIT; ## beyond the limit, set the iterations to 0 to pick black. } $image->setpixel( x => $x, y => $y, color => $palette[ $iterations ] )