Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Decorated string, v1.1

by l3nz (Friar)
on Nov 25, 2003 at 14:30 UTC ( [id://309908]=CUFP: print w/replies, xml ) Need Help??

This is an upgrade of the same idea expressed in pg's decorated string, with a couple of improvements:
  • You can change font size, too
  • Better handling of sequences with whitespaces
  • No more FONT tags but stuff
  • Random variations of font size
The code is taken from pg's version, so you'll notice some obvious similarities.

use strict; use warnings; my $text =<<"__TEXT__"; This is a sample text going from red to blue, from 10 to 20 points in +size and with 5 points random variation. __TEXT__ print decorate( $text, [255,0,0], [0,0,255], 10, 20, 5); sub decorate { my ($str, $cl, $cr, $szl, $szr, $randomsize) = @_; my $s; my $lenStr = length( $str ); for ( my $i = 0; $i < $lenStr; $i++) { my $c = substr($str, $i, 1); if ($c eq ' ') { $s .= "&nbsp;\n"; } else { my $red = interpolate( $cl->[0], $cr->[0], $lenStr, $i, 0 +); my $grn = interpolate( $cl->[1], $cr->[1], $lenStr, $i, 0 +); my $blu = interpolate( $cl->[2], $cr->[2], $lenStr, $i, 0 +); my $siz = interpolate( $szl, $szr, $lenStr, $i, $rando +msize ); $s .= sprintf("<span style=\"font-size: %dpt; color: #%02x +%02x%02x\">$c</span>", $siz, $red, $grn, $blu, $c); } } return $s; } sub interpolate { my ($from, $to, $nSteps, $thisStep, $randomShift) = @_; my $realStep = $thisStep - $randomShift + rand($randomShift*2); my $val = $from + (($to-$from)/$nSteps) * $realStep; if ( $val < min($from,$to) ) { $val = min($from, $to) }; if ( $val > max($from,$to) ) { $val = max($from, $to) }; return $val; } sub min() { my ($a, $b) = @_; return ($a < $b) ? $a : $b; } sub max() { my ($a, $b) = @_; return ($a > $b) ? $a : $b; }

Replies are listed 'Best First'.
Re: Decorated string, v1.1
by pg (Canon) on Nov 25, 2003 at 19:34 UTC

    What a joy it is, to see an ugly duck now becoming a beauty!

      It's not much of a beauty, but I liked your idea!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://309908]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found