1Tk::Canvas::GradientColUosre(r3)Contributed Perl DocumenTtka:t:iCoannvas::GradientColor(3)
2
3
4
6 Tk::Canvas::GradientColor - To create a Canvas widget with background
7 gradient color.
8
10 #!/usr/bin/perl
11 use strict;
12 use warnings;
13
14 use Tk;
15 use Tk::Canvas::GradientColor;
16
17 my $mw = MainWindow->new(
18 -title => 'Tk::Canvas::GradientColor',
19 -background => 'white',
20 );
21
22 my $canvas = $mw->GradientColor(
23 -width => 400,
24 -height => 400,
25 )->pack(qw/ -fill both -expand 1 /);
26
27 $mw->update;
28 sleep 3;
29
30 # Change color
31 $canvas->set_gradientcolor(
32 -start_color => '#000000',
33 -end_color => '#00CDFF',
34 );
35
36 $mw->update;
37 sleep 3;
38
39 # Change type
40 $canvas->set_gradientcolor(
41 -start => 50,
42 -end => 100,
43 -type => 'mirror_vertical'
44 );
45
46 MainLoop();
47
49 Tk::Canvas::GradientColor is an extension of the Canvas widget. It is
50 an easy way to build a canvas widget with gradient background color.
51
53 -background -borderwidth -closeenough
54 -confine -cursor -height
55 -highlightbackground -highlightcolor
56 -highlightthickness -insertbackground -insertborderwidth
57 -insertofftime -insertontime -insertwidth -relief
58 -scrollregion -selectbackground -selectborderwidth -selectforeground
59 -takefocus -width -xscrollcommand -xscrollincrement
60 -yscrollcommand -yscrollincrement
61
63 The Canvas method creates a widget object. This object supports the
64 configure and cget methods described in Tk::options which can be used
65 to enquire and modify the options described above.
66
67 disabled_gradientcolor
68 $canvas_bg->disabled_gradientcolor
69 Disabled background gradient color. The canvas widget will have the
70 background color set by -background option.
71
72 $canvas_bg->disabled_gradientcolor;
73
74 enabled_gradientcolor
75 $canvas_bg->enabled_gradientcolor
76 Enabled background gradient color. Background gradient color is
77 activated by default. Use this method if disabled_gradientcolor
78 method is called.
79
80 $canvas_bg->enabled_gradientcolor;
81
82 get_gradientcolor
83 $canvas_bg->get_gradientcolor
84 Return a hash reference which contains the options to create the
85 background gradient color.
86
87 my $ref_gradient_options = $canvas_bg->get_gradientcolor;
88
89 set_gradientcolor
90 $canvas_bg->set_gradientcolor(?options)
91 · -type
92
93 8 types are available : linear_horizontal, linear_vertical,
94 mirror_horizontal, mirror_vertical, radial, losange,
95 corner_right and corner_left.
96
97 -type => 'corner_left',
98
99 Default : linear_horizontal
100
101 · -start_color
102
103 First color of gradient color.
104
105 -start_color => 'red',
106
107 Default : #8BC2F5
108
109 · -end_color
110
111 Last color of gradient color.
112
113 -end_color => '#FFCD9F',
114
115 Default : white
116
117 · -start
118
119 -start => 50, # Must be >= 0, <= 100 and start < end
120
121 Use it for linear_horizontal and linear_vertical type. The
122 first color starts at 'start' percent width of canvas. The
123 easy way to understand is to test the example in this
124 documentation.
125
126 Ex : width canvas = 1000px, start = 50 : the first part of
127 canvas has the background color of start_color and the gradient
128 color start at 500px.
129
130 Default : 0
131
132 Use it for mirror_horizontal and mirror_vertical type. The
133 first color starts at 'start' percent width of canvas. The
134 easy way to understand is to test the example in this
135 documentation.
136
137 Ex : width canvas = 1000px, start = 50 : the background
138 gradient color begins at 50 percent in two directions.
139
140 Default : 50
141
142 · -end
143
144 -end => 80, # Must be >= 0, <= 100 and end > start
145
146 Use it for linear_horizontal and linear_vertical type. The last
147 color finishes at 'end' percent width of canvas. The easy way
148 to understand is to test the example in this documentation.
149
150 Default : 100
151
152 Use it for mirror_horizontal and mirror_vertical type. The last
153 color finishes at 'end' percent width of canvas and opposite.
154 The easy way to understand is to test the example in this
155 documentation.
156
157 Default : 100
158
159 · -number_color
160
161 Number of colors between first and last color to create the
162 gradient.
163
164 -number_color => 200,
165
166 Default : 100
167
168 rgb_to_hex
169 $canvas_bg->rgb_to_hex($red, $green, $blue)
170 Return hexa code of rgb color.
171
172 my $color = $canvas_bg->rgb_to_hex(200, 102, 65); # return #C86641
173
174 hex_to_rgb
175 $canvas_bg->hex_to_rgb(string)
176 Return an array with red, green an blue code rgb color.
177
178 my ( $red, $green, $blue ) = $canvas_bg->hex_to_rgb('#C86641'); # return 200, 102, 65
179 my ( $red, $green, $blue ) = $canvas_bg->hex_to_rgb('gray'); # return 190, 190, 190
180
182 An example to test the configuration of the widget:
183
184 #!/usr/bin/perl
185 use strict;
186 use warnings;
187
188 use Tk;
189 use Tk::Canvas::GradientColor;
190 use Tk::BrowseEntry;
191
192 my $mw = MainWindow->new(
193 -title => 'gradient color with canvas',
194 -background => 'snow',
195 );
196
197 my $canvas = $mw->GradientColor(
198 -background => '#005500',
199 -width => 500,
200 -height => 500,
201 )->pack(qw/ -fill both -expand 1 /);
202
203 my %arg_gradient = (
204 -type => undef,
205 -start_color => '#A780C1',
206 -end_color => 'white',
207 -start => undef,
208 -end => undef,
209 -number_color => undef,
210 );
211
212 # configure start color
213 my $bouton_color1 = $canvas->Button(
214 -text => 'select color start',
215 -command => sub {
216 $arg_gradient{-start_color} = $canvas->chooseColor( -title => 'select color start' );
217 $canvas->set_gradientcolor(%arg_gradient);
218 },
219 );
220
221 # configure end color
222 my $bouton_color2 = $canvas->Button(
223 -text => 'select color end',
224 -command => sub {
225 $arg_gradient{-end_color} = $canvas->chooseColor( -title => 'select color end' );
226 $canvas->set_gradientcolor(%arg_gradient);
227 },
228 );
229
230 my $type = $canvas->BrowseEntry(
231 -label => 'Type gradient color',
232 -choices => [
233 qw/ linear_horizontal linear_vertical mirror_horizontal mirror_vertical radial losange corner_right corner_left/
234 ],
235 -background => 'white',
236 -state => 'readonly',
237 -disabledbackground => 'yellow',
238 -browsecmd => sub {
239 my ( $widget, $selection ) = @_;
240 $arg_gradient{-type} = $selection;
241 $canvas->set_gradientcolor(%arg_gradient);
242 },
243 );
244
245 my $start_num = $canvas->Scale(
246 -background => 'white',
247 -label => 'Start',
248 -from => 0,
249 -to => 100,
250 -variable => 0,
251 -orient => 'horizontal',
252 -sliderlength => 10,
253 -command => sub {
254 my $selection = shift;
255 $arg_gradient{-start} = $selection;
256 $canvas->set_gradientcolor(%arg_gradient);
257 },
258 );
259
260 my $end_num = $canvas->Scale(
261 -background => 'white',
262 -label => 'End',
263 -from => 0,
264 -to => 100,
265 -variable => '100',
266 -orient => 'horizontal',
267 -sliderlength => 10,
268 -command => sub {
269 my $selection = shift;
270 $arg_gradient{-end} = $selection;
271 $canvas->set_gradientcolor(%arg_gradient);
272 },
273 );
274 my $num = 100;
275 my $entry_number_color = $canvas->BrowseEntry(
276 -label => 'Number color',
277 -choices => [qw/ 2 3 4 5 10 50 100 150 200 250 300 400 500 750 1000 1500 2000 2500/],
278 -state => 'readonly',
279 -disabledbackground => 'yellow',
280 -background => 'white',
281 -variable => \$num,
282 -browsecmd => sub {
283 my ( $widget, $selection ) = @_;
284 $arg_gradient{-number_color} = $selection;
285 $canvas->set_gradientcolor(%arg_gradient);
286 },
287 );
288
289 my $disabled_gradientcolor = $canvas->Button(
290 -text => 'disabled_gradientcolor',
291 -command => sub { $canvas->disabled_gradientcolor; },
292 );
293 my $enabled_gradientcolor = $canvas->Button(
294 -text => 'enabled_gradientcolor',
295 -command => sub { $canvas->enabled_gradientcolor; },
296 );
297
298 $canvas->createWindow( 100, 100, -window => $bouton_color1 );
299 $canvas->createWindow( 400, 100, -window => $bouton_color2 );
300 $canvas->createWindow( 100, 150, -window => $start_num );
301 $canvas->createWindow( 100, 200, -window => $end_num );
302 $canvas->createWindow( 350, 150, -window => $entry_number_color );
303 $canvas->createWindow( 350, 200, -window => $type );
304 $canvas->createWindow( 100, 350, -window => $disabled_gradientcolor );
305 $canvas->createWindow( 400, 350, -window => $enabled_gradientcolor );
306
307 MainLoop;
308
310 Djibril Ousmanou, "<djibel at cpan.org>"
311
313 Please report any bugs or feature requests to
314 "bug-Tk-Canvas-GradientColor at rt.cpan.org", or through the web
315 interface at
316 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tk-Canvas-GradientColor>.
317 I will be notified, and then you'll automatically be notified of
318 progress on your bug as I make changes.
319
321 See Tk::Canvas for details of the standard options.
322
324 You can find documentation for this module with the perldoc command.
325
326 perldoc Tk::Canvas::GradientColor
327
328 You can also look for information at:
329
330 · RT: CPAN's request tracker
331
332 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tk-Canvas-GradientColor>
333
334 · AnnoCPAN: Annotated CPAN documentation
335
336 <http://annocpan.org/dist/Tk-Canvas-GradientColor>
337
338 · CPAN Ratings
339
340 <http://cpanratings.perl.org/d/Tk-Canvas-GradientColor>
341
342 · Search CPAN
343
344 <http://search.cpan.org/dist/Tk-Canvas-GradientColor/>
345
348 Copyright 2014 Djibril Ousmanou, all rights reserved.
349
350 This program is free software; you can redistribute it and/or modify it
351 under the same terms as Perl itself.
352
353
354
355perl v5.30.1 2020-01-30 Tk::Canvas::GradientColor(3)