annotate GD/Simple.pm @ 0:e94de0ea3351 draft default tip

Uploaded
author dereeper
date Wed, 11 Sep 2013 09:08:15 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1 package GD::Simple;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
2
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
3 =head1 NAME
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
4
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
5 GD::Simple - Simplified interface to GD library
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
6
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
7 =head1 SYNOPSIS
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
8
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
9 use GD::Simple;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
10
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
11 # create a new image
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
12 $img = GD::Simple->new(400,250);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
13
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
14 # draw a red rectangle with blue borders
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
15 $img->bgcolor('red');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
16 $img->fgcolor('blue');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
17 $img->rectangle(10,10,50,50);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
18
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
19 # draw an empty rectangle with green borders
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
20 $img->bgcolor(undef);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
21 $img->fgcolor('green');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
22 $img->rectangle(30,30,100,100);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
23
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
24 # move to (80,80) and draw a green line to (100,190)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
25 $img->moveTo(80,80);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
26 $img->lineTo(100,190);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
27
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
28 # draw a solid orange ellipse
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
29 $img->moveTo(110,100);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
30 $img->bgcolor('orange');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
31 $img->fgcolor('orange');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
32 $img->ellipse(40,40);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
33
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
34 # draw a black filled arc
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
35 $img->moveTo(150,150);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
36 $img->fgcolor('black');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
37 $img->arc(50,50,0,100,gdNoFill|gdEdged);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
38
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
39 # draw a string at (10,180) using the default
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
40 # built-in font
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
41 $img->moveTo(10,180);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
42 $img->string('This is very simple');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
43
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
44 # draw a string at (280,210) using 20 point
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
45 # times italic, angled upward 90 degrees
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
46 $img->moveTo(280,210);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
47 $img->font('Times:italic');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
48 $img->fontsize(20);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
49 $img->angle(-90);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
50 $img->string('This is very fancy');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
51
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
52 # some turtle graphics
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
53 $img->moveTo(300,100);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
54 $img->penSize(3,3);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
55 $img->angle(0);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
56 $img->line(20); # 20 pixels going to the right
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
57 $img->turn(30); # set turning angle to 30 degrees
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
58 $img->line(20); # 20 pixel line
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
59 $img->line(20);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
60 $img->line(20);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
61 $img->turn(-90); # set turning angle to -90 degrees
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
62 $img->line(50); # 50 pixel line
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
63
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
64 # draw a cyan polygon edged in blue
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
65 my $poly = new GD::Polygon;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
66 $poly->addPt(150,100);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
67 $poly->addPt(199,199);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
68 $poly->addPt(100,199);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
69 $img->bgcolor('cyan');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
70 $img->fgcolor('blue');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
71 $img->penSize(1,1);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
72 $img->polygon($poly);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
73
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
74 # convert into png data
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
75 print $img->png;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
76
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
77 =head1 DESCRIPTION
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
78
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
79 GD::Simple is a subclass of the GD library that shortens many of the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
80 long GD method calls by storing information about the pen color, size
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
81 and position in the GD object itself. It also adds a small number of
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
82 "turtle graphics" style calls for those who prefer to work in polar
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
83 coordinates. In addition, the library allows you to use symbolic
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
84 names for colors, such as "chartreuse", and will manage the colors for
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
85 you.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
86
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
87 =head2 The Pen
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
88
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
89 GD::Simple maintains a "pen" whose settings are used for line- and
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
90 shape-drawing operations. The pen has the following properties:
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
91
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
92 =over 4
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
93
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
94 =item fgcolor
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
95
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
96 The pen foreground color is the color of lines and the borders of
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
97 filled and unfilled shapes.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
98
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
99 =item bgcolor
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
100
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
101 The pen background color is the color of the contents of filled
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
102 shapes.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
103
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
104 =item pensize
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
105
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
106 The pen size is the width of the pen. Larger sizes draw thicker
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
107 lines.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
108
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
109 =item position
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
110
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
111 The pen position is its current position on the canvas in (X,Y)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
112 coordinates.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
113
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
114 =item angle
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
115
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
116 When drawing in turtle mode, the pen angle determines the current
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
117 direction of lines of relative length.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
118
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
119 =item turn
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
120
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
121 When drawing in turtle mode, the turn determines the clockwise or
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
122 counterclockwise angle that the pen will turn before drawing the next
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
123 line.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
124
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
125 =item font
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
126
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
127 The font to use when drawing text. Both built-in bitmapped fonts and
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
128 TrueType fonts are supported.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
129
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
130 =item fontsize
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
131
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
132 The size of the font to use when drawing with TrueType fonts.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
133
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
134 =back
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
135
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
136 One sets the position and properties of the pen and then draws. As
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
137 the drawing progresses, the position of the pen is updated.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
138
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
139 =head2 Methods
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
140
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
141 GD::Simple introduces a number of new methods, a few of which have the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
142 same name as GD::Image methods, and hence change their behavior. In
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
143 addition to these new methods, GD::Simple objects support all of the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
144 GD::Image methods. If you make a method call that isn't directly
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
145 supported by GD::Simple, it refers the request to the underlying
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
146 GD::Image object. Hence one can load a JPEG image into GD::Simple and
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
147 declare it to be TrueColor by using this call, which is effectively
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
148 inherited from GD::Image:
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
149
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
150 my $img = GD::Simple->newFromJpeg('./myimage.jpg',1);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
151
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
152 The rest of this section describes GD::Simple-specific methods.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
153
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
154 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
155
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
156 use strict;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
157 use GD;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
158 use GD::Group;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
159 use Math::Trig;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
160 use Carp 'croak';
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
161
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
162 our @ISA = 'Exporter';
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
163 our @EXPORT = @GD::EXPORT;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
164 our @EXPORT_OK = @GD::EXPORT_OK;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
165 our $AUTOLOAD;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
166
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
167 my %COLORS;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
168 my $IMAGECLASS = 'GD::Image';
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
169 my $TRANSPARENT;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
170
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
171 sub AUTOLOAD {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
172 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
173 my($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
174 return if $func_name eq 'DESTROY';
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
175
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
176 if (ref $self && exists $self->{gd}) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
177 $self->{gd}->$func_name(@_);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
178 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
179 my @result = $IMAGECLASS->$func_name(@_);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
180 if (UNIVERSAL::isa($result[0],'GD::Image')) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
181 return $self->new($result[0]);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
182 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
183 return @result;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
184 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
185 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
186 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
187
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
188 =over 4
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
189
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
190 =item $img = GD::Simple->new($x,$y [,$truecolor])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
191
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
192 =item $img = GD::Simple->new($gd)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
193
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
194 Create a new GD::Simple object. There are two forms of new(). In the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
195 first form, pass the width and height of the desired canvas, and
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
196 optionally a boolean flag to request a truecolor image. In the second
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
197 form, pass a previously-created GD::Image object.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
198
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
199 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
200
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
201 # dual-purpose code - beware
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
202 sub new {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
203 my $pack = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
204
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
205 unshift @_,(100,100) if @_ == 0;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
206
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
207 if (@_ >= 2) { # traditional GD::Image->new() call
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
208 my $gd = $IMAGECLASS->new(@_);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
209 my $self = $pack->new($gd);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
210 $self->clear;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
211 return $self;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
212 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
213
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
214 if (@_ == 1) { # initialize from existing image
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
215 my $gd = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
216 my $self = bless {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
217 gd => $gd,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
218 xy => [0,0],
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
219 font => gdSmallFont,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
220 fontsize => 9,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
221 turningangle => 0,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
222 angle => 0,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
223 pensize => 1,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
224 },$pack;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
225 $self->{bgcolor} = $self->translate_color(255,255,255);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
226 $self->{fgcolor} = $self->translate_color(0,0,0);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
227 return $self;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
228 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
229 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
230
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
231 =item GD::Simple->class('GD');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
232
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
233 =item GD::Simple->class('GD::SVG');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
234
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
235 Select whether new() should use GD or GD::SVG internally. Call
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
236 GD::Simple->class('GD::SVG') before calling new() if you wish to
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
237 generate SVG images.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
238
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
239 If future GD subclasses are created, this method will subport them.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
240
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
241 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
242
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
243 sub class {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
244 my $pack = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
245 if (@_) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
246 $IMAGECLASS = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
247 eval "require $IMAGECLASS; 1" or die $@;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
248 $IMAGECLASS = "$IMAGECLASS\:\:Image"
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
249 if $IMAGECLASS eq 'GD::SVG';
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
250 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
251 $IMAGECLASS;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
252 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
253
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
254 =item $img->moveTo($x,$y)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
255
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
256 This call changes the position of the pen without drawing. It moves
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
257 the pen to position ($x,$y) on the drawing canvas.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
258
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
259 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
260
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
261 sub moveTo {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
262 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
263 croak 'Usage GD::Simple->moveTo($x,$y)' unless @_ == 2;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
264 my ($x,$y) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
265 $self->{xy} = [$x,$y];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
266 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
267
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
268 =item $img->move($dx,$dy)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
269
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
270 =item $img->move($dr)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
271
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
272 This call changes the position of the pen without drawing. When called
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
273 with two arguments it moves the pen $dx pixels to the right and $dy
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
274 pixels downward. When called with one argument it moves the pen $dr
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
275 pixels along the vector described by the current pen angle.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
276
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
277 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
278
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
279 sub move {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
280 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
281 if (@_ == 1) { # polar coordinates -- this is r
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
282 $self->{angle} += $self->{turningangle};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
283 my $angle = deg2rad($self->{angle});
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
284 $self->{xy}[0] += $_[0] * cos($angle);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
285 $self->{xy}[1] += $_[0] * sin($angle);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
286 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
287 elsif (@_ == 2) { # cartesian coordinates
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
288 $self->{xy}[0] += $_[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
289 $self->{xy}[1] += $_[1];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
290 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
291 croak 'Usage GD::Simple->move($dx,$dy) or move($r)';
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
292 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
293 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
294
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
295 =item $img->lineTo($x,$y)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
296
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
297 The lineTo() call simultaneously draws and moves the pen. It draws a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
298 line from the current pen position to the position defined by ($x,$y)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
299 using the current pen size and color. After drawing, the position of
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
300 the pen is updated to the new position.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
301
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
302 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
303
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
304 sub lineTo {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
305 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
306 croak 'Usage GD::Simple->lineTo($x,$y)' unless @_ == 2;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
307 $self->gd->line($self->curPos,@_,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
308 $self->moveTo(@_);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
309 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
310
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
311 =item $img->line($dx,$dy)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
312
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
313 =item $img->line($dr)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
314
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
315 The line() call simultaneously draws and moves the pen. When called
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
316 with two arguments it draws a line from the current position of the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
317 pen to the position $dx pixels to the right and $dy pixels down. When
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
318 called with one argument, it draws a line $dr pixels long along the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
319 angle defined by the current pen angle.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
320
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
321 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
322
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
323 sub line {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
324 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
325 croak 'Usage GD::Simple->line($dx,$dy) or line($r)' unless @_ >= 1;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
326 my @curPos = $self->curPos;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
327 $self->move(@_);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
328 my @newPos = $self->curPos;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
329 $self->gd->line(@curPos,@newPos,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
330 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
331
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
332 =item $img->clear
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
333
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
334 This method clears the canvas by painting over it with the current
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
335 background color.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
336
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
337 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
338
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
339 sub clear {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
340 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
341 $self->gd->filledRectangle(0,0,$self->getBounds,$self->bgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
342 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
343
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
344 =item $img->rectangle($x1,$y1,$x2,$y2)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
345
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
346 This method draws the rectangle defined by corners ($x1,$y1),
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
347 ($x2,$y2). The rectangle's edges are drawn in the foreground color and
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
348 its contents are filled with the background color. To draw a solid
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
349 rectangle set bgcolor equal to fgcolor. To draw an unfilled rectangle
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
350 (transparent inside), set bgcolor to undef.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
351
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
352 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
353
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
354 sub rectangle {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
355 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
356 croak 'Usage GD::Simple->rectangle($x1,$y1,$x2,$y2)' unless @_ == 4;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
357 my $gd = $self->gd;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
358 my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
359 $gd->filledRectangle(@_,$bg) if defined $bg;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
360 $gd->rectangle(@_,$fg) if defined $fg && (!defined $bg || $bg != $fg);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
361 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
362
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
363 =item $img->ellipse($width,$height)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
364
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
365 This method draws the ellipse centered at the current location with
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
366 width $width and height $height. The ellipse's border is drawn in the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
367 foreground color and its contents are filled with the background
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
368 color. To draw a solid ellipse set bgcolor equal to fgcolor. To draw
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
369 an unfilled ellipse (transparent inside), set bgcolor to undef.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
370
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
371 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
372
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
373 sub ellipse {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
374 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
375 croak 'Usage GD::Simple->ellipse($width,$height)' unless @_ == 2;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
376 my $gd = $self->gd;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
377 my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
378 $gd->filledEllipse($self->curPos,@_,$bg) if defined $bg;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
379 $gd->ellipse($self->curPos,@_,$fg) if defined $fg && (!defined $bg || $bg != $fg);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
380 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
381
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
382 =item $img->arc($cx,$cy,$width,$height,$start,$end [,$style])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
383
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
384 This method draws filled and unfilled arcs. See L<GD> for a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
385 description of the arguments. To draw a solid arc (such as a pie
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
386 wedge) set bgcolor equal to fgcolor. To draw an unfilled arc, set
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
387 bgcolor to undef.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
388
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
389 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
390
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
391 sub arc {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
392 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
393 croak 'Usage GD::Simple->arc($width,$height,$start,$end,$style)' unless @_ >= 4;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
394 my ($width,$height,$start,$end,$style) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
395 my $gd = $self->gd;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
396 my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
397 my ($cx,$cy) = $self->curPos;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
398
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
399 if ($bg) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
400 my @args = ($cx,$cy,$width,$height,$start,$end,$bg);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
401 push @args,$style if defined $style;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
402 $gd->filledArc(@args);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
403 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
404 my @args = ($cx,$cy,$width,$height,$start,$end,$fg);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
405 $gd->arc(@args);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
406 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
407 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
408
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
409 =item $img->polygon($poly)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
410
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
411 This method draws filled and unfilled polygon using the current
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
412 settings of fgcolor for the polygon border and bgcolor for the polygon
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
413 fill color. See L<GD> for a description of creating polygons. To draw
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
414 a solid polygon set bgcolor equal to fgcolor. To draw an unfilled
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
415 polygon, set bgcolor to undef.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
416
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
417 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
418
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
419 sub polygon {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
420 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
421 croak 'Usage GD::Simple->polygon($poly)' unless @_ == 1;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
422 my $gd = $self->gd;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
423 my ($bg,$fg) = ($self->bgcolor,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
424 $gd->filledPolygon(@_,$bg) if defined $bg;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
425 $gd->openPolygon(@_,$fg) if defined $fg && (!defined $bg || $bg != $fg);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
426 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
427
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
428 =item $img->polyline($poly)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
429
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
430 This method draws polygons without closing the first and last vertices
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
431 (similar to GD::Image->unclosedPolygon()). It uses the fgcolor to draw
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
432 the line.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
433
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
434 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
435
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
436 sub polyline {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
437 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
438 croak 'Usage GD::Simple->polyline($poly)' unless @_ == 1;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
439 my $gd = $self->gd;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
440 my $fg = $self->fgcolor;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
441 $gd->unclosedPolygon(@_,$fg);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
442 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
443
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
444 =item $img->string($string)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
445
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
446 This method draws the indicated string starting at the current
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
447 position of the pen. The pen is moved to the end of the drawn string.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
448 Depending on the font selected with the font() method, this will use
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
449 either a bitmapped GD font or a TrueType font. The angle of the pen
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
450 will be consulted when drawing the text. For TrueType fonts, any angle
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
451 is accepted. For GD bitmapped fonts, the angle can be either 0 (draw
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
452 horizontal) or -90 (draw upwards).
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
453
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
454 For consistency between the TrueType and GD font behavior, the string
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
455 is always drawn so that the current position of the pen corresponds to
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
456 the bottom left of the first character of the text. This is different
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
457 from the GD behavior, in which the first character of bitmapped fonts
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
458 hangs down from the pen point.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
459
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
460 This method returns a polygon indicating the bounding box of the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
461 rendered text. If an error occurred (such as invalid font
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
462 specification) it returns undef and an error message in $@.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
463
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
464 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
465
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
466 sub string {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
467 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
468 my $string = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
469 my $font = $self->font;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
470 my @bounds;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
471 if (ref $font && $font->isa('GD::Font')) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
472 my ($x,$y) = $self->curPos;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
473 if ($self->angle == -90) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
474 $x -= $font->height;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
475 $y -= $font->width;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
476 $self->gd->stringUp($font,$x,$y,$string,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
477 $self->{xy}[1] -= length($string) * $font->width;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
478 @bounds = ( ($self->{xy}[0],$y), ($x,$y), ($x,$self->{xy}[1]-$font->width), ($self->{xy}[0],$self->{xy}[1]-$font->width) );
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
479 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
480 $y -= $font->height;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
481 $self->gd->string($font,$x,$y,$string,$self->fgcolor);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
482 $self->{xy}[0] += length($string) * $font->width;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
483 @bounds = ( ($x,$self->{xy}[1]), ($self->{xy}[0],$self->{xy}[1]), ($self->{xy}[0],$y), ($x,$y) );
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
484 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
485 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
486 else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
487 $self->useFontConfig(1);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
488 @bounds = $self->stringFT($self->fgcolor,$font,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
489 $self->fontsize,-deg2rad($self->angle), # -pi * $self->angle/180,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
490 $self->curPos,$string);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
491 return unless @bounds;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
492 my ($delta_x,$delta_y) = $self->_string_width(@bounds);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
493 $self->{xy}[0] += $delta_x;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
494 $self->{xy}[1] += $delta_y;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
495 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
496 my $poly = GD::Polygon->new;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
497 while (@bounds) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
498 $poly->addPt(splice(@bounds,0,2));
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
499 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
500 return $poly;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
501 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
502
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
503 =item $metrics = $img->fontMetrics
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
504
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
505 =item ($metrics,$width,$height) = GD::Simple->fontMetrics($font,$fontsize,$string)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
506
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
507 This method returns information about the current font, most commonly
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
508 a TrueType font. It can be invoked as an instance method (on a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
509 previously-created GD::Simple object) or as a class method (on the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
510 'GD::Simple' class).
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
511
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
512 When called as an instance method, fontMetrics() takes no arguments
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
513 and returns a single hash reference containing the metrics that
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
514 describe the currently selected font and size. The hash reference
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
515 contains the following information:
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
516
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
517 xheight the base height of the font from the bottom to the top of
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
518 a lowercase 'm'
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
519
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
520 ascent the length of the upper stem of the lowercase 'd'
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
521
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
522 descent the length of the lower step of the lowercase 'j'
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
523
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
524 lineheight the distance from the bottom of the 'j' to the top of
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
525 the 'd'
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
526
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
527 leading the distance between two adjacent lines
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
528
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
529 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
530
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
531 # return %$fontmetrics
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
532 # keys: 'ascent', 'descent', 'lineheight', 'xheight', 'leading'
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
533 sub fontMetrics {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
534 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
535
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
536 unless (ref $self) { #class invocation -- create a scratch
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
537 $self = $self->new;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
538 $self->font(shift) if defined $_[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
539 $self->fontsize(shift) if defined $_[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
540 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
541
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
542 my $font = $self->font;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
543 my $metrics;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
544
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
545 if (ref $font && $font->isa('GD::Font')) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
546 my $height = $font->height;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
547 $metrics = {ascent => 0,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
548 descent => 0,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
549 lineheight => $height,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
550 xheight => $height,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
551 leading => 0};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
552 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
553 else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
554 $self->useFontConfig(1);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
555 my @mbounds = GD::Image->stringFT($self->fgcolor,$font,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
556 $self->fontsize,0,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
557 0,0,'m');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
558 my $xheight = $mbounds[3]-$mbounds[5];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
559 my @jbounds = GD::Image->stringFT($self->fgcolor,$font,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
560 $self->fontsize,0,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
561 0,0,'j');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
562 my $ascent = $mbounds[7]-$jbounds[7];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
563 my $descent = $jbounds[3]-$mbounds[3];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
564
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
565 my @mmbounds = GD::Image->stringFT($self->fgcolor,$font,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
566 $self->fontsize,0,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
567 0,0,"m\nm");
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
568 my $twolines = $mmbounds[3]-$mmbounds[5];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
569 my $lineheight = $twolines - 2*$xheight;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
570 my $leading = $lineheight - $ascent - $descent;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
571 $metrics = {ascent => $ascent,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
572 descent => $descent,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
573 lineheight => $lineheight,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
574 xheight => $xheight,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
575 leading => $leading};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
576 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
577
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
578 if ((my $string = shift) && wantarray) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
579 my ($width,$height) = $self->stringBounds($string);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
580 return ($metrics,abs($width),abs($height));
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
581 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
582 return $metrics;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
583 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
584
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
585 =item ($delta_x,$delta_y)= $img->stringBounds($string)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
586
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
587 This method indicates the X and Y offsets (which may be negative) that
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
588 will occur when the given string is drawn using the current font,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
589 fontsize and angle. When the string is drawn horizontally, it gives
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
590 the width and height of the string's bounding box.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
591
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
592 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
593
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
594 sub stringBounds {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
595 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
596 my $string = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
597 my $font = $self->font;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
598 if (ref $font && $font->isa('GD::Font')) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
599 if ($self->angle == -90) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
600 return ($font->height,-length($string) * $font->width);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
601 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
602 return (length($string) * $font->width,$font->height);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
603 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
604 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
605 else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
606 $self->useFontConfig(1);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
607 my @bounds = GD::Image->stringFT($self->fgcolor,$font,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
608 $self->fontsize,-deg2rad($self->angle),
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
609 $self->curPos,$string);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
610 return $self->_string_width(@bounds);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
611 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
612 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
613
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
614 =item $delta_x = $img->stringWidth($string)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
615
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
616 This method indicates the width of the string given the current font,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
617 fontsize and angle. It is the same as ($img->stringBounds($string))[0]
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
618
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
619 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
620
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
621 sub stringWidth {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
622 return ((shift->stringBounds(@_))[0]);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
623 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
624
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
625
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
626 sub _string_width {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
627 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
628 my @bounds = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
629 my $delta_x = abs($bounds[2]-$bounds[0]);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
630 my $delta_y = abs($bounds[5]-$bounds[3]);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
631 my $angle = $self->angle % 360;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
632 if ($angle >= 0 && $angle < 90) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
633 return ($delta_x,$delta_y);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
634
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
635 } elsif ($angle >= 90 && $angle < 180) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
636 return (-$delta_x,$delta_y);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
637
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
638 } elsif ($angle >= 180 && $angle < 270) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
639 return (-$delta_x,-$delta_y);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
640
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
641 } elsif ($angle >= 270 && $angle < 360) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
642 return ($delta_x,-$delta_y);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
643 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
644 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
645
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
646 =item ($x,$y) = $img->curPos
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
647
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
648 Return the current position of the pen. Set the current position
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
649 using moveTo().
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
650
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
651 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
652
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
653 sub curPos { @{shift->{xy}}; }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
654
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
655 =item $font = $img->font([$newfont] [,$newsize])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
656
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
657 Get or set the current font. Fonts can be GD::Font objects, TrueType
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
658 font file paths, or fontconfig font patterns like "Times:italic" (see
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
659 L<fontconfig>). The latter feature requires that you have the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
660 fontconfig library installed and are using libgd version 2.0.33 or
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
661 higher.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
662
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
663 As a shortcut, you may pass two arguments to set the font and the
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
664 fontsize simultaneously. The fontsize is only valid when drawing with
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
665 TrueType fonts.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
666
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
667 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
668
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
669 sub font {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
670 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
671 $self->{font} = shift if @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
672 $self->{fontsize} = shift if @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
673 $self->{font};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
674 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
675
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
676 =item $size = $img->fontsize([$newfontsize])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
677
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
678 Get or set the current font size. This is only valid for TrueType
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
679 fonts.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
680
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
681 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
682
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
683 sub fontsize {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
684 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
685 $self->{fontsize} = shift if @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
686 $self->{fontsize};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
687 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
688
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
689 =item $size = $img->penSize([$newpensize])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
690
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
691 Get or set the current pen width for use during line drawing
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
692 operations.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
693
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
694 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
695
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
696 sub penSize {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
697 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
698 if (@_) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
699 $self->{pensize} = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
700 $self->gd->setThickness($self->{pensize});
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
701 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
702 $self->{pensize};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
703 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
704
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
705 =item $angle = $img->angle([$newangle])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
706
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
707 Set the current angle for use when calling line() or move() with a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
708 single argument.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
709
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
710 Here is an example of using turn() and angle() together to draw an
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
711 octagon. The first line drawn is the downward-slanting top right
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
712 edge. The last line drawn is the horizontal top of the octagon.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
713
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
714 $img->moveTo(200,50);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
715 $img->angle(0);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
716 $img->turn(360/8);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
717 for (1..8) { $img->line(50) }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
718
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
719 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
720
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
721 sub angle {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
722 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
723 $self->{angle} = shift if @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
724 $self->{angle};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
725 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
726
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
727 =item $angle = $img->turn([$newangle])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
728
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
729 Get or set the current angle to turn prior to drawing lines. This
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
730 value is only used when calling line() or move() with a single
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
731 argument. The turning angle will be applied to each call to line() or
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
732 move() just before the actual drawing occurs.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
733
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
734 Angles are in degrees. Positive values turn the angle clockwise.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
735
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
736 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
737
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
738 # degrees, not radians
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
739 sub turn {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
740 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
741 $self->{turningangle} = shift if @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
742 $self->{turningangle};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
743 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
744
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
745 =item $color = $img->fgcolor([$newcolor])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
746
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
747 Get or set the pen's foreground color. The current pen color can be
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
748 set by (1) using an (r,g,b) triple; (2) using a previously-allocated
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
749 color from the GD palette; or (3) by using a symbolic color name such
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
750 as "chartreuse." The list of color names can be obtained using
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
751 color_names(). The special color name 'transparent' will create a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
752 completely transparent color.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
753
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
754 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
755
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
756 sub fgcolor {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
757 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
758 $self->{fgcolor} = $self->translate_color(@_) if @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
759 $self->{fgcolor};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
760 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
761
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
762 =item $color = $img->bgcolor([$newcolor])
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
763
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
764 Get or set the pen's background color. The current pen color can be
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
765 set by (1) using an (r,g,b) triple; (2) using a previously-allocated
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
766 color from the GD palette; or (3) by using a symbolic color name such
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
767 as "chartreuse." The list of color names can be obtained using
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
768 color_names(). The special color name 'transparent' will create a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
769 completely transparent color.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
770
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
771 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
772
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
773 sub bgcolor {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
774 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
775 $self->{bgcolor} = $self->translate_color(@_) if @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
776 $self->{bgcolor};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
777 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
778
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
779 =item $index = $img->translate_color(@args)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
780
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
781 Translates a color into a GD palette or TrueColor index. You may pass
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
782 either an (r,g,b) triple or a symbolic color name. If you pass a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
783 previously-allocated index, the method will return it unchanged.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
784
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
785 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
786
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
787 sub translate_color {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
788 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
789 return unless defined $_[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
790 my ($r,$g,$b);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
791 if (@_ == 1 && $_[0] =~ /^-?\d+/) { # previously allocated index
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
792 return $_[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
793 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
794 elsif (@_ == 3) { # (rgb triplet)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
795 ($r,$g,$b) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
796 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
797 elsif (lc $_[0] eq 'transparent') {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
798 return $TRANSPARENT ||= $self->alphaColor('white',127);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
799 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
800 else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
801 $self->read_color_table unless %COLORS;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
802 die "unknown color" unless exists $COLORS{lc $_[0]};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
803 ($r,$g,$b) = @{$COLORS{lc $_[0]}};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
804 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
805 return $self->colorResolve($r,$g,$b);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
806 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
807
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
808 sub transparent {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
809 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
810 my $index = $self->translate_color(@_);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
811 $self->gd->transparent($index);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
812 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
813
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
814 =item $index = $img->alphaColor(@args,$alpha)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
815
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
816 Creates an alpha color. You may pass either an (r,g,b) triple or a
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
817 symbolic color name, followed by an integer indicating its
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
818 opacity. The opacity value ranges from 0 (fully opaque) to 127 (fully
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
819 transparent).
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
820
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
821 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
822
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
823 sub alphaColor {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
824 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
825 return unless defined $_[0];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
826 my ($r,$g,$b,$a);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
827 if (@_ == 4) { # (rgb triplet)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
828 ($r,$g,$b,$a) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
829 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
830 $self->read_color_table unless %COLORS;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
831 die "unknown color" unless exists $COLORS{lc $_[0]};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
832 ($r,$g,$b) = @{$COLORS{lc $_[0]}};
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
833 $a = $_[1];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
834 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
835 return $self->colorAllocateAlpha($r,$g,$b,$a);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
836 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
837
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
838 =item @names = GD::Simple->color_names
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
839
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
840 =item $translate_table = GD::Simple->color_names
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
841
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
842 Called in a list context, color_names() returns the list of symbolic
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
843 color names recognized by this module. Called in a scalar context,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
844 the method returns a hash reference in which the keys are the color
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
845 names and the values are array references containing [r,g,b] triples.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
846
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
847 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
848
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
849 sub color_names {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
850 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
851 $self->read_color_table unless %COLORS;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
852 return wantarray ? sort keys %COLORS : \%COLORS;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
853 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
854
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
855 =item $gd = $img->gd
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
856
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
857 Return the internal GD::Image object. Usually you will not need to
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
858 call this since all GD methods are automatically referred to this object.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
859
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
860 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
861
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
862 sub gd { shift->{gd} }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
863
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
864 sub read_color_table {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
865 my $class = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
866 while (<DATA>) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
867 chomp;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
868 last if /^__END__/;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
869 my ($name,$r,$g,$b) = split /\s+/;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
870 $COLORS{$name} = [hex $r,hex $g,hex $b];
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
871 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
872 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
873
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
874 sub setBrush {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
875 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
876 my $brush = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
877 if ($brush->isa('GD::Simple')) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
878 $self->gd->setBrush($brush->gd);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
879 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
880 $self->gd->setBrush($brush);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
881 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
882 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
883
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
884 =item ($red,$green,$blue) = GD::Simple->HSVtoRGB($hue,$saturation,$value)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
885
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
886 Convert a Hue/Saturation/Value (HSV) color into an RGB triple. The
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
887 hue, saturation and value are integers from 0 to 255.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
888
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
889 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
890
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
891 sub HSVtoRGB {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
892 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
893 @_ == 3 or croak "Usage: GD::Simple->HSVtoRGB(\$hue,\$saturation,\$value)";
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
894
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
895 my ($h,$s,$v)=@_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
896 my ($r,$g,$b,$i,$f,$p,$q,$t);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
897
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
898 if( $s == 0 ) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
899 ## achromatic (grey)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
900 return ($v,$v,$v);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
901 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
902 $h %= 255;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
903 $s /= 255; ## scale saturation from 0.0-1.0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
904 $h /= 255; ## scale hue from 0 to 1.0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
905 $h *= 360; ## and now scale it to 0 to 360
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
906
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
907 $h /= 60; ## sector 0 to 5
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
908 $i = $h % 6;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
909 $f = $h - $i; ## factorial part of h
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
910 $p = $v * ( 1 - $s );
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
911 $q = $v * ( 1 - $s * $f );
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
912 $t = $v * ( 1 - $s * ( 1 - $f ) );
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
913
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
914 if($i<1) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
915 $r = $v;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
916 $g = $t;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
917 $b = $p;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
918 } elsif($i<2){
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
919 $r = $q;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
920 $g = $v;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
921 $b = $p;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
922 } elsif($i<3){
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
923 $r = $p;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
924 $g = $v;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
925 $b = $t;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
926 } elsif($i<4){
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
927 $r = $p;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
928 $g = $q;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
929 $b = $v;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
930 } elsif($i<5){
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
931 $r = $t;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
932 $g = $p;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
933 $b = $v;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
934 } else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
935 $r = $v;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
936 $g = $p;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
937 $b = $q;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
938 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
939 return (int($r+0.5),int($g+0.5),int($b+0.5));
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
940 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
941
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
942 =item ($hue,$saturation,$value) = GD::Simple->RGBtoHSV($hue,$saturation,$value)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
943
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
944 Convert a Red/Green/Blue (RGB) value into a Hue/Saturation/Value (HSV)
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
945 triple. The hue, saturation and value are integers from 0 to 255.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
946
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
947 =back
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
948
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
949 =cut
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
950
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
951 sub RGBtoHSV {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
952 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
953 my ($r, $g ,$bl) = @_;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
954 my ($min,undef,$max) = sort {$a<=>$b} ($r,$g,$bl);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
955 return (0,0,0) unless $max > 0;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
956
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
957 my $v = $max;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
958 my $s = 255 * ($max - $min)/$max;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
959 my $h;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
960 my $range = $max - $min;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
961
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
962 if ($range == 0) { # all colors are equal, so monochrome
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
963 return (0,0,$max);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
964 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
965
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
966 if ($max == $r) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
967 $h = 60 * ($g-$bl)/$range;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
968 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
969 elsif ($max == $g) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
970 $h = 60 * ($bl-$r)/$range + 120;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
971 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
972 else {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
973 $h = 60 * ($r-$g)/$range + 240;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
974 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
975
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
976 $h = int($h*255/360 + 0.5);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
977
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
978 return ($h, $s, $v);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
979 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
980
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
981 sub newGroup {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
982 my $self = shift;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
983 return $self->GD::newGroup(@_);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
984 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
985
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
986 1;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
987
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
988 __DATA__
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
989 white FF FF FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
990 black 00 00 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
991 aliceblue F0 F8 FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
992 antiquewhite FA EB D7
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
993 aqua 00 FF FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
994 aquamarine 7F FF D4
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
995 azure F0 FF FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
996 beige F5 F5 DC
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
997 bisque FF E4 C4
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
998 blanchedalmond FF EB CD
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
999 blue 00 00 FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1000 blueviolet 8A 2B E2
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1001 brown A5 2A 2A
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1002 burlywood DE B8 87
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1003 cadetblue 5F 9E A0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1004 chartreuse 7F FF 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1005 chocolate D2 69 1E
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1006 coral FF 7F 50
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1007 cornflowerblue 64 95 ED
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1008 cornsilk FF F8 DC
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1009 crimson DC 14 3C
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1010 cyan 00 FF FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1011 darkblue 00 00 8B
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1012 darkcyan 00 8B 8B
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1013 darkgoldenrod B8 86 0B
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1014 darkgray A9 A9 A9
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1015 darkgreen 00 64 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1016 darkkhaki BD B7 6B
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1017 darkmagenta 8B 00 8B
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1018 darkolivegreen 55 6B 2F
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1019 darkorange FF 8C 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1020 darkorchid 99 32 CC
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1021 darkred 8B 00 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1022 darksalmon E9 96 7A
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1023 darkseagreen 8F BC 8F
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1024 darkslateblue 48 3D 8B
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1025 darkslategray 2F 4F 4F
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1026 darkturquoise 00 CE D1
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1027 darkviolet 94 00 D3
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1028 deeppink FF 14 100
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1029 deepskyblue 00 BF FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1030 dimgray 69 69 69
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1031 dodgerblue 1E 90 FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1032 firebrick B2 22 22
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1033 floralwhite FF FA F0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1034 forestgreen 22 8B 22
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1035 fuchsia FF 00 FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1036 gainsboro DC DC DC
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1037 ghostwhite F8 F8 FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1038 gold FF D7 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1039 goldenrod DA A5 20
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1040 gray 80 80 80
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1041 green 00 80 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1042 greenyellow AD FF 2F
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1043 honeydew F0 FF F0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1044 hotpink FF 69 B4
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1045 indianred CD 5C 5C
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1046 indigo 4B 00 82
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1047 ivory FF FF F0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1048 khaki F0 E6 8C
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1049 lavender E6 E6 FA
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1050 lavenderblush FF F0 F5
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1051 lawngreen 7C FC 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1052 lemonchiffon FF FA CD
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1053 lightblue AD D8 E6
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1054 lightcoral F0 80 80
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1055 lightcyan E0 FF FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1056 lightgoldenrodyellow FA FA D2
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1057 lightgreen 90 EE 90
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1058 lightgrey D3 D3 D3
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1059 lightpink FF B6 C1
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1060 lightsalmon FF A0 7A
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1061 lightseagreen 20 B2 AA
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1062 lightskyblue 87 CE FA
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1063 lightslategray 77 88 99
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1064 lightsteelblue B0 C4 DE
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1065 lightyellow FF FF E0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1066 lime 00 FF 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1067 limegreen 32 CD 32
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1068 linen FA F0 E6
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1069 magenta FF 00 FF
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1070 maroon 80 00 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1071 mediumaquamarine 66 CD AA
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1072 mediumblue 00 00 CD
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1073 mediumorchid BA 55 D3
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1074 mediumpurple 100 70 DB
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1075 mediumseagreen 3C B3 71
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1076 mediumslateblue 7B 68 EE
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1077 mediumspringgreen 00 FA 9A
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1078 mediumturquoise 48 D1 CC
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1079 mediumvioletred C7 15 85
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1080 midnightblue 19 19 70
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1081 mintcream F5 FF FA
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1082 mistyrose FF E4 E1
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1083 moccasin FF E4 B5
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1084 navajowhite FF DE AD
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1085 navy 00 00 80
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1086 oldlace FD F5 E6
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1087 olive 80 80 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1088 olivedrab 6B 8E 23
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1089 orange FF A5 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1090 orangered FF 45 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1091 orchid DA 70 D6
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1092 palegoldenrod EE E8 AA
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1093 palegreen 98 FB 98
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1094 paleturquoise AF EE EE
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1095 palevioletred DB 70 100
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1096 papayawhip FF EF D5
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1097 peachpuff FF DA B9
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1098 peru CD 85 3F
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1099 pink FF C0 CB
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1100 plum DD A0 DD
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1101 powderblue B0 E0 E6
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1102 purple 80 00 80
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1103 red FF 00 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1104 rosybrown BC 8F 8F
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1105 royalblue 41 69 E1
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1106 saddlebrown 8B 45 13
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1107 salmon FA 80 72
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1108 sandybrown F4 A4 60
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1109 seagreen 2E 8B 57
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1110 seashell FF F5 EE
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1111 sienna A0 52 2D
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1112 silver C0 C0 C0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1113 skyblue 87 CE EB
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1114 slateblue 6A 5A CD
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1115 slategray 70 80 90
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1116 snow FF FA FA
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1117 springgreen 00 FF 7F
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1118 steelblue 46 82 B4
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1119 tan D2 B4 8C
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1120 teal 00 80 80
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1121 thistle D8 BF D8
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1122 tomato FF 63 47
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1123 turquoise 40 E0 D0
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1124 violet EE 82 EE
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1125 wheat F5 DE B3
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1126 whitesmoke F5 F5 F5
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1127 yellow FF FF 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1128 yellowgreen 9A CD 32
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1129 gradient1 00 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1130 gradient2 0a ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1131 gradient3 14 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1132 gradient4 1e ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1133 gradient5 28 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1134 gradient6 32 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1135 gradient7 3d ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1136 gradient8 47 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1137 gradient9 51 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1138 gradient10 5b ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1139 gradient11 65 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1140 gradient12 70 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1141 gradient13 7a ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1142 gradient14 84 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1143 gradient15 8e ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1144 gradient16 99 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1145 gradient17 a3 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1146 gradient18 ad ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1147 gradient19 b7 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1148 gradient20 c1 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1149 gradient21 cc ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1150 gradient22 d6 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1151 gradient23 e0 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1152 gradient24 ea ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1153 gradient25 f4 ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1154 gradient26 ff ff 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1155 gradient27 ff f4 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1156 gradient28 ff ea 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1157 gradient29 ff e0 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1158 gradient30 ff d6 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1159 gradient31 ff cc 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1160 gradient32 ff c1 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1161 gradient33 ff b7 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1162 gradient34 ff ad 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1163 gradient35 ff a3 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1164 gradient36 ff 99 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1165 gradient37 ff 8e 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1166 gradient38 ff 84 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1167 gradient39 ff 7a 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1168 gradient40 ff 70 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1169 gradient41 ff 65 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1170 gradient42 ff 5b 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1171 gradient43 ff 51 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1172 gradient44 ff 47 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1173 gradient45 ff 3d 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1174 gradient46 ff 32 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1175 gradient47 ff 28 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1176 gradient48 ff 1e 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1177 gradient49 ff 14 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1178 gradient50 ff 0a 00
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1179 __END__
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1180
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1181 =head1 COLORS
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1182
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1183 This script will create an image showing all the symbolic colors.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1184
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1185 #!/usr/bin/perl
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1186
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1187 use strict;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1188 use GD::Simple;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1189
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1190 my @color_names = GD::Simple->color_names;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1191 my $cols = int(sqrt(@color_names));
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1192 my $rows = int(@color_names/$cols)+1;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1193
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1194 my $cell_width = 100;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1195 my $cell_height = 50;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1196 my $legend_height = 16;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1197 my $width = $cols * $cell_width;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1198 my $height = $rows * $cell_height;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1199
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1200 my $img = GD::Simple->new($width,$height);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1201 $img->font(gdSmallFont);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1202
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1203 for (my $c=0; $c<$cols; $c++) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1204 for (my $r=0; $r<$rows; $r++) {
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1205 my $color = $color_names[$c*$rows + $r] or next;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1206 my @topleft = ($c*$cell_width,$r*$cell_height);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1207 my @botright = ($topleft[0]+$cell_width,$topleft[1]+$cell_height-$legend_height);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1208 $img->bgcolor($color);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1209 $img->fgcolor($color);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1210 $img->rectangle(@topleft,@botright);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1211 $img->moveTo($topleft[0]+2,$botright[1]+$legend_height-2);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1212 $img->fgcolor('black');
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1213 $img->string($color);
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1214 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1215 }
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1216
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1217 print $img->png;
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1218
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1219 =head1 AUTHOR
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1220
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1221 The GD::Simple module is copyright 2004, Lincoln D. Stein. It is
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1222 distributed under the same terms as Perl itself. See the "Artistic
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1223 License" in the Perl source code distribution for licensing terms.
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1224
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1225 The latest versions of GD.pm are available at
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1226
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1227 http://stein.cshl.org/WWW/software/GD
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1228
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1229 =head1 SEE ALSO
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1230
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1231 L<GD>,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1232 L<GD::Polyline>,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1233 L<GD::SVG>,
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1234 L<Image::Magick>
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1235
e94de0ea3351 Uploaded
dereeper
parents:
diff changeset
1236 =cut