Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: 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.

ColorFoo

#!/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; }

In reply to Color Gradient Generator by YuckFoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-16 17:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found