| 1 | package Pastel::LinearGradient; |
| 2 | @ISA = qw(Pastel::GradientI Pastel::Mixin::Mixin); |
| 3 | use Carp; |
| 4 | use strict; |
| 5 | |
| 6 | { |
| 7 | my $id = 0; |
| 8 | sub _id { return $id++ ; } |
| 9 | } |
| 10 | |
| 11 | use constant USER_SPACE => "userSpaceOnUse"; |
| 12 | use constant BOUNDING_BOX => "objectBoundingBox"; |
| 13 | use constant PAD => "pad"; |
| 14 | use constant REFLECT => "reflect"; |
| 15 | use constant REPEAT => "repeat"; |
| 16 | use constant RADIAL => "radial"; |
| 17 | use constant LINEAR => "linear"; |
| 18 | |
| 19 | # Perl module for linear Gradientpaint |
| 20 | # Cared for by Malay<curiouser@ccmb.ap.nic.in> |
| 21 | # Copyright 2001, Malay Kumar Basu |
| 22 | # You may distribute this module under the same terms as perl itself |
| 23 | |
| 24 | |
| 25 | =head2 NAME |
| 26 | |
| 27 | Pastel::LinearGradient |
| 28 | |
| 29 | =head2 DESCRIPTION |
| 30 | |
| 31 | In SVG there are two different types of GradientPaint that you can use. |
| 32 | "Linear" and "Circular". Both of these are represented in Pastel by separate |
| 33 | modules. Pastel::LinearGradient represent a Gradient paint in liear mode. The |
| 34 | Gradient goes from one color to the next from one point to another on the |
| 35 | vector from the first point to the second. |
| 36 | |
| 37 | =head2 CONSTRAUCTOR |
| 38 | |
| 39 | =head3 Pastel::LinearGradient() |
| 40 | |
| 41 | =head3 Pastel::LinearGradient(-color1=>$color1, -color2=>$color2) |
| 42 | |
| 43 | =head3 Pastel::LinearGradient(-point1=>$point1, -color1=>$color1, -point2=>$point2, -color2=>$color2) |
| 44 | |
| 45 | =head3 Pastel::LinearGradient(-point1=>$point1, -color1=>$color1, -point2=>$point2, -color2=>$color2, -method=>Pastel::LinearGradient::PAD/REFLECT/REPEAT, -units=>Pastel::LinearGradient::USER_SPACE/BOUNDING_BOX) |
| 46 | |
| 47 | =head3 Pastel::LinearGradient(-point1=>$point1, -color1=>$color1, -point2=>$point2, -color2=>$color2, -method=>Pastel::LinearGradient::PAD/REFLECT/REPEAT) |
| 48 | |
| 49 | =cut |
| 50 | |
| 51 | sub new { |
| 52 | my $class = shift; |
| 53 | my $self = {}; |
| 54 | bless $self, ref($class) || $class; |
| 55 | $self->_init(@_); |
| 56 | return $self; |
| 57 | } |
| 58 | |
| 59 | sub _init { |
| 60 | my ($self, @args) = @_; |
| 61 | my($p1, $c1, $p2, $c2, $m, $u)=$self->_rearrange(["POINT1", |
| 62 | "COLOR1", |
| 63 | "POINT2", |
| 64 | "COLOR2", |
| 65 | "METHOD", |
| 66 | "UNITS"], @args); |
| 67 | |
| 68 | if ( defined($p1) ){ $self->{p1} = $p1; } |
| 69 | if ( defined($p2) ){ $self->{p2} = $p2; } |
| 70 | if ( defined($c1) ){ $self->{c1} = $c1; } |
| 71 | if ( defined($c2) ){ $self->{c2} = $c2; } |
| 72 | if ( defined($m) ) { $self->{method} = $m; } |
| 73 | if (defined ($u) ) { $self->{units} = $u; } |
| 74 | |
| 75 | $self->{id} = "LinearGradient". $self->_get_id(); |
| 76 | $self->{stops} = []; |
| 77 | |
| 78 | if( defined($self->{c1}) && defined($self->{c2})){ |
| 79 | my $stop1 = Pastel::GradientStop->new(0, $c1); |
| 80 | my $stop2 = Pastel::GradientStop->new(100, $c2); |
| 81 | |
| 82 | $self->add_stop($stop1); |
| 83 | $self->add_stop($stop2); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | return $self; |
| 89 | } |
| 90 | |
| 91 | sub get_point1 { |
| 92 | my $self = shift; |
| 93 | return (defined($self->{p1}))? $self->{p1}: undef; |
| 94 | } |
| 95 | |
| 96 | sub get_point2 { |
| 97 | my $self = shift; |
| 98 | return (defined($self->{p2}))? $self->{p2}: undef; |
| 99 | } |
| 100 | |
| 101 | sub get_spread_method { |
| 102 | my $self= shift; |
| 103 | return (defined($self->{method}))? $self->{method}: undef; |
| 104 | } |
| 105 | |
| 106 | sub get_units { |
| 107 | my $self = shift; |
| 108 | return (defined($self->{units}))? $self->{units}: undef; |
| 109 | } |
| 110 | sub to_svg { |
| 111 | my $self = shift; |
| 112 | |
| 113 | my $s = '<linearGradient id="'.$self->get_id().'" '; |
| 114 | if ( defined( $self->{p1} )){ |
| 115 | $s .= 'x1="'.$self->get_point1()->get_x().'" '; |
| 116 | $s .= 'y1="'.$self->get_point1()->get_y().'" '; |
| 117 | } |
| 118 | |
| 119 | if (defined($self->{p2})){ |
| 120 | $s.= 'x2="'.$self->get_point2()->get_x().'" '; |
| 121 | $s.= 'y2="'.$self->get_point2()->get_y().'" '; |
| 122 | } |
| 123 | |
| 124 | if (defined($self->{method})){ |
| 125 | $s .= 'spreadMethod="'.$self->get_method().'" '; |
| 126 | } |
| 127 | |
| 128 | if (defined($self->{units})){ |
| 129 | $s .= 'gradientUnits="'.$self->get_units().'" '; |
| 130 | } |
| 131 | |
| 132 | $s .= '>'; |
| 133 | |
| 134 | if(@{$self->{stops}} > 0){ |
| 135 | for( my $i = 0; $i < @{$self->{stops}}; $i++){ |
| 136 | $s .= ${$self->{stops}}[$i]->to_svg(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | $s .= '</linearGradient>'; |
| 141 | } |
| 142 | |
| 143 | sub get_ref { |
| 144 | my $self = shift; |
| 145 | return '#'.$self->get_id(); |
| 146 | } |
| 147 | |
| 148 | sub _get_id { |
| 149 | return $_[0]->_id(); |
| 150 | } |
| 151 | |
| 152 | sub get_id { |
| 153 | return $_[0]->{id}; |
| 154 | } |
| 155 | 1; |
| 156 | |
| 157 | |
| 158 | |