Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | package Pastel::GradientStop; |
2 | @ISA = qw (Pastel::Mixin::Mixin); | |
3 | use Carp; | |
4 | use Pastel::Mixin::Mixin; | |
5 | ||
6 | use strict; | |
7 | ||
8 | # Perl module for Pastel::Color | |
9 | # Cared for by Malay<curiouser@ccmb.ap.nic.in> | |
10 | # Copyright 2001, Malay Kumar Basu | |
11 | # You may distribute this module under the same terms as perl itself | |
12 | ||
13 | =head1 NAME | |
14 | ||
15 | Pastel::GradientStop | |
16 | ||
17 | Each of these gradient paint classes holds several of GradientStop classes. | |
18 | The GradientStop classes represents the "stop" element under Linear | |
19 | or radialGradient element in SVG. | |
20 | ||
21 | =head1 DESCRIPTION | |
22 | ||
23 | There are only two fields in Pastel::GradientStop: "offset" and "color". | |
24 | The values of "offset" can vary from 0 to 100%, basically reflect at what | |
25 | point along the GradientVector the gradient stops. The "color" is supplied | |
26 | as Pastel::Color. | |
27 | ||
28 | =head1 SYNOPSIS | |
29 | ||
30 | #Create a Gradient object | |
31 | my $linearGradient = Pastel::LinearGradient->new(...); | |
32 | my $color1 = Pastel::Color->red(); | |
33 | ||
34 | ||
35 | #Create the first stop | |
36 | my $firstGradientStop = Pastel::GradientStop->new(-offset=>5, | |
37 | -color=>$color1); | |
38 | ||
39 | #the same thing | |
40 | # my $firstGradientStop = Pastel::GradientStop->new(5, $color1); | |
41 | ||
42 | # Add the stop to gradient | |
43 | $linearGradient->add_stop($firstGradientStop); | |
44 | ||
45 | # Set the gradient to graphics object | |
46 | $graphics->set_paint($linearGradient); | |
47 | ||
48 | =head1 CONSTRUCTOR | |
49 | ||
50 | =head3 Pastel::GradientStop->new(-offset=>$int, -color=>$color) | |
51 | ||
52 | $int is integer between 0 and 100, $color is a Pastel::Color object. | |
53 | ||
54 | =head3 Pastel::GradientStop->new($int, $color) | |
55 | ||
56 | Orthodox parameter styles. | |
57 | ||
58 | =cut | |
59 | ||
60 | sub new { | |
61 | my $class = shift; | |
62 | my $self = {}; | |
63 | bless $self, ref($class) || $class; | |
64 | $self->_init(@_); | |
65 | return $self; | |
66 | } | |
67 | ||
68 | sub _init { | |
69 | my ($self, @args) = @_; | |
70 | ||
71 | if ( (@args < 2) || (@args >4)){ | |
72 | croak "Parameter problems in Pastel::GradientStop::new\n"; | |
73 | } | |
74 | ||
75 | my ($offset, $color) = $self->_rearrange(["OFFSET","COLOR"],@args); | |
76 | ||
77 | if(!defined($offset) || !defined($color) ){ | |
78 | croak "Parameter problem in Pastel::GradientStop::new\n"; | |
79 | } | |
80 | ||
81 | if( ($offset <0) || ($offset >100)){ | |
82 | croak "Offset value in Pastel::GradientStop::new is out of range\n"; | |
83 | } | |
84 | ||
85 | if (! $color->isa("Pastel::Color") ){ | |
86 | croak "Wrong color parameter in Pastel::GradientStop\n"; | |
87 | } | |
88 | ||
89 | $self->{offset} = $offset; | |
90 | $self->{color} = $color; | |
91 | ||
92 | return $self; | |
93 | } | |
94 | ||
95 | sub to_svg { | |
96 | my $self = shift; | |
97 | my $s = ""; | |
98 | $s .= "<stop offset=\"". $self->get_offset()."\%\" stop-color=\"". | |
99 | $self->get_color(). "\" stop-opacity=\"". $self->get_opacity(). "\" />"; | |
100 | return $s; | |
101 | } | |
102 | ||
103 | sub get_offset { | |
104 | return $_[0]->{offset}; | |
105 | } | |
106 | ||
107 | sub get_color { | |
108 | return $_[0]->{color}->to_hex(); | |
109 | } | |
110 | ||
111 | sub get_opacity { | |
112 | if ( $_[0]->{color}->get_alpha() ){ | |
113 | return $_[0]->{color}->get_alpha(); | |
114 | } else { | |
115 | return 1; | |
116 | } | |
117 | } | |
118 | ||
119 | 1; | |
120 | ||
121 | ||
122 | ||
123 | ||
124 | ||
125 | ||
126 | ||
127 |