blib/lib/Image/Simple/Gradient.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Image::Simple::Gradient;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use GD;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use GD::Simple;
6 use Moose::Util::TypeConstraints;
7  
8  
9 subtype 'Email'
10 => as 'Str'
11 => where { Email::Valid->address($_) }
12 => message { "$_ is not a valid email address" };
13  
14 subtype 'Rgbhex'
15 => as 'Str'
16 => where { $_ =~ m/^((\d|\w){6})$/ }
17 => message { "$_ is not a valid RBG Hex. ie. FFFFFF" };
18  
19 subtype 'Direction'
20 => as 'Str'
21 => where { $_ =~ m/^(up|down|left|right)$/i }
22 => message { "$_ is not a valid direction. Use up, down, left or right" };
23  
24 has [qw( width height )] => ( is => 'ro', isa => 'Int', required => 1, );
25 has [qw( color_begin color_end )] => ( is => 'ro', isa => 'Rgbhex', required => 1, );
26 has direction => (
27 is => 'ro',
28 isa => 'Direction',
29 required => 1,
30 );
31  
32 sub BUILD {
33 my ( $self, $params ) = @_;
34 }
35  
36 sub mkramp
37 {
38 my ( $self, $im, $steps, $r0, $g0, $b0, $r1, $g1, $b1 ) = @_;
39 my $dr = ( $r1 - $r0 ) / $steps;
40 my $dg = ( $g1 - $g0 ) / $steps;
41 my $db = ( $b1 - $b0 ) / $steps;
42 my @ramp = ();
43  
44 my $r = $r0;
45 my $g = $g0;
46 my $b = $b0;
47 for ( my $i = 0 ; $i < $steps ; $i++ ) {
48 @ramp = ( @ramp, $im->colorAllocate( $r, $g, $b ) );
49 $r += $dr;
50 $g += $dg;
51 $b += $db;
52 }
53 return @ramp;
54 }
55  
56 sub render_gradient {
57 my ( $self ) = @_;
58 my $width = $self->width;
59 my $height = $self->height;
60  
61 my $from = $self->color_begin;
62 my $to = $self->color_end;
63 my $dir = $self->direction;
64  
65 $from =~ s/(..)(..)(..)/$1\|$2\|$3/;
66 my ( $from_r, $from_g, $from_b ) = split( /\|/, $from );
67  
68 $to =~ s/(..)(..)(..)/$1\|$2\|$3/;
69 my ( $to_r, $to_g, $to_b ) = split( /\|/, $to );
70  
71 $from_r = hex($from_r);
72 $from_g = hex($from_g);
73 $from_b = hex($from_b);
74 $to_r = hex($to_r);
75 $to_g = hex($to_g);
76 $to_b = hex($to_b);
77  
78 my $steps = 1;
79 if ( $dir =~ m/down/i ) { $steps = $height; }
80 if ( $dir =~ m/up/i ) { $steps = $height; }
81 if ( $dir =~ m/left/i ) { $steps = $width; }
82 if ( $dir =~ m/right/i ) { $steps = $width; }
83  
84 # create a new image
85 GD::Image->trueColor(1);
86 my $im = new GD::Image( $width, $height );
87  
88 my @ramp =
89 $self->mkramp( $im, $steps, $from_r, $from_g, $from_b, $to_r, $to_g,
90 $to_b );
91  
92 if ( $dir eq "down" ) {
93 for ( my $i = 0 ; $i < $height ; $i++ ) {
94 $im->line( 0, $i, $width, $i, $ramp[$i] );
95 }
96 }
97 elsif ( $dir eq "up" ) {
98 for ( my $i = 0 ; $i < $height ; $i++ ) {
99 $im->line( 0, $i, $width, $i, $ramp[ $height - $i - 1 ] );
100 }
101 }
102 elsif ( $dir eq "left" ) {
103 for ( my $i = 0 ; $i < $width ; $i++ ) {
104 $im->line( $i, 0, $i, $height, $ramp[$i] );
105 }
106 }
107 elsif ( $dir eq "right" ) {
108 for ( my $i = 0 ; $i < $width ; $i++ ) {
109 $im->line( $i, 0, $i, $height, $ramp[ $width - $i - 1 ] );
110 }
111 }
112  
113 return $im->jpeg(100);
114 }
115  
116  
117 =head1 NAME
118  
119 Image::Simple::Gradient create simple gradients for your perl web / software application.
120  
121 =head1 VERSION
122  
123 Version 0.05
124  
125 =cut
126  
127 our $VERSION = '0.05';
128  
129  
130 =head1 SYNOPSIS
131  
132 Create simple gradient images with this module.
133 If you are looking for a way to render a gradient image going from one color to another color, this is the module for it.
134 Its useful when your designer needs an easy simple way to generate gradient colors for a webpage or application software.
135  
136 direction can be: up, down, left, right.
137 height and width: in pixels.
138 color_begin and color_end are rgb hex values with 6 digits. ex: FF0000
139  
140 use Image::Simple::Gradient;
141  
142 my $im = Image::Simple::Gradient->new({
143 color_begin => 'FF0000',
144 color_end => '0000FF',
145 direction => 'up',
146 height => 100,
147 width => 200,
148 });
149 my $im = $image->render_gradient();
150  
151 if (open FH, "> my_gradient.jpg") {
152 binmode FH;
153 my $IO = fileno(FH);
154 print FH $im, $filename;
155 }
156  
157  
158  
159 =head1 EXPORT
160  
161 A list of functions that can be exported. You can delete this section
162 if you don't export anything, such as for a purely object-oriented module.
163  
164 =head1 SUBROUTINES/METHODS
165  
166  
167 =over
168  
169 =item new( HASH_REF );
170  
171 A hash reference must be passed on construction.
172  
173 Follow the example:
174  
175 my $image = Image::Simple::Gradient->new({
176 color_begin => 'FF0000',
177 color_end => '0000FF',
178 direction => 'up',
179 height => 100,
180 width => 200,
181 });
182  
183  
184 =item render_gradient();
185  
186 Renders the image and returns a jpg. ie:
187  
188 my $im = $image->render_gradient();
189  
190 =head1 MODULE AUTHOR
191  
192 Hernan Lopes, C<< <hernanlopes at gmail.com> >>
193  
194 =head1 ORIGINAL AUTHOR
195  
196 Michal Guerquin, C<< <michalg at gmail.com> >>
197  
198 =head1 BUGS
199  
200 Please report any bugs or feature requests to C<bug-image-simple-gradient at rt.cpan.org>, or through
201 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Image-Simple-Gradient>. I will be notified, and then you'll
202 automatically be notified of progress on your bug as I make changes.
203  
204 =head1 SUPPORT
205  
206 You can find documentation for this module with the perldoc command.
207  
208 perldoc Image::Simple::Gradient
209  
210  
211 You can also look for information at:
212  
213 =over 4
214  
215 =item * RT: CPAN's request tracker
216  
217 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Image-Simple-Gradient>
218  
219 =item * AnnoCPAN: Annotated CPAN documentation
220  
221 L<http://annocpan.org/dist/Image-Simple-Gradient>
222  
223 =item * CPAN Ratings
224  
225 L<http://cpanratings.perl.org/d/Image-Simple-Gradient>
226  
227 =item * Search CPAN
228  
229 L<http://search.cpan.org/dist/Image-Simple-Gradient/>
230  
231 =back
232  
233  
234 =head1 ACKNOWLEDGEMENTS
235  
236  
237 =head1 LICENSE AND COPYRIGHT
238  
239 Copyright 2010 Hernan Lopes.
240  
241 This program is free software; you can redistribute it and/or modify it
242 under the terms of either: the GNU General Public License as published
243 by the Free Software Foundation; or the Artistic License.
244  
245 See http://dev.perl.org/licenses/ for more information.
246  
247  
248 =cut
249 __PACKAGE__->meta->make_immutable;
250  
251 1; # End of Image::Simple::Gradient
↑ Summary