Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Color Gradient Generator

by YuckFoo (Abbot)
on Nov 22, 2005 at 20:45 UTC ( #510896=CUFP: print w/ replies, xml ) Need Help??

This program uses a (quarter of a) sine wave to vary red, green, and blue values from 0 to 1 over the range of the gradient. Offsets for each component are used to determine the starting point of the wave.


#!/usr/bin/perl use strict; use CGI qw(:standard *table :nodebug); use Data::Dumper; #------------------- # Configuration my $NUM_STEPS = 32; # Number of colors in gradient my $WRAPPING = 1; # Makes the end match the beginning my $OUT_HTML = 1; # Output as HTML, otherwise Dumper #------------------- my $PI = atan2(0, -1); my $USE_STEPS = $WRAPPING ? $NUM_STEPS : $NUM_STEPS * 2; my $EACH_STEP = $PI / $USE_STEPS; my $offs = make_offsets_random(); my $grad = make_gradient($offs); if ($OUT_HTML) { output_html($offs, $grad); } else { output_dump($offs, $grad); } #----------------------------------------------------------- sub make_gradient { my $offs = shift; my @grad; for my $i (0..$NUM_STEPS-1) { push @grad, make_rgb($i, $offs); } return \@grad; } #----------------------------------------------------------- sub make_rgb { my $i = shift; my $offs = shift; my $r = ($i + $offs->{r}) % $USE_STEPS; my $g = ($i + $offs->{g}) % $USE_STEPS; my $b = ($i + $offs->{b}) % $USE_STEPS; return sprintf "%2.2x%2.2x%2.2x", int(sin($r * $EACH_STEP) * 255), int(sin($g * $EACH_STEP) * 255), int(sin($b * $EACH_STEP) * 255); } #----------------------------------------------------------- sub make_offsets_random { return { r => int(rand() * $USE_STEPS), g => int(rand() * $USE_STEPS), b => int(rand() * $USE_STEPS), }; } #----------------------------------------------------------- sub output_html { my $offs = shift; my $grad = shift; print header(); print start_html(); print start_table({-cellspacing=>0}); my @rows; push @rows, Tr(td("Offsets $offs->{r} $offs->{g} $offs->{b}")); for my $i (@$grad) { push @rows, Tr(td({-bgcolor=>$i}, $i)); } print join("\n", @rows); print end_table(); print end_html(); } #----------------------------------------------------------- sub output_dump { my $offs = shift; my $grad = shift; print Dumper $offs; print Dumper $grad; }

Comment on Color Gradient Generator
Download Code
Replies are listed 'Best First'.
Re: Color Gradient Generator (HTML::Rainbow)
by grinder (Bishop) on Nov 23, 2005 at 11:14 UTC

    There is, of course, a CPAN module that does this: see HTML::Rainbow. :-)

    • another intruder with the mooring in the heart of the Perl

Re: Color Gradient Generator
by hasimir44 (Sexton) on Dec 12, 2005 at 09:43 UTC
    I had to change:
    for my $i (@$grad) { push @rows, Tr(td({-bgcolor=>$i}, $i)); }
    for my $i (@$grad) { push @rows, Tr(td({-bgcolor=>"#$i"}, $i)); }

    and it works great. This is going to be fun to play with.. There seems to be a module for everything, but it's also cool to see it laid out like this..

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2016-02-07 15:53 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (254 votes), past polls