Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 aurelien 1
<?php
2
/*=======================================================================
3
// File:	JPGRAPH_GRADIENT.PHP
4
// Description:	Create a color gradient
5
// Created: 	2003-02-01
6
// Ver:		$Id: jpgraph_gradient.php 946 2007-10-19 22:14:00Z ljp $
7
//
8
// Copyright (c) Aditus Consulting. All rights reserved.
9
//========================================================================
10
*/
11
 
12
// Styles for gradient color fill
13
DEFINE("GRAD_VER",1);
14
DEFINE("GRAD_VERT",1);
15
DEFINE("GRAD_HOR",2);
16
DEFINE("GRAD_MIDHOR",3);
17
DEFINE("GRAD_MIDVER",4);
18
DEFINE("GRAD_CENTER",5);
19
DEFINE("GRAD_WIDE_MIDVER",6);
20
DEFINE("GRAD_WIDE_MIDHOR",7);
21
DEFINE("GRAD_LEFT_REFLECTION",8);
22
DEFINE("GRAD_RIGHT_REFLECTION",9);
23
DEFINE("GRAD_RAISED_PANEL",10);
24
DEFINE("GRAD_DIAGONAL",11);
25
 
26
//===================================================
27
// CLASS Gradient
28
// Description: Handles gradient fills. This is to be
29
// considered a "friend" class of Class Image.
30
//===================================================
31
class Gradient {
32
    var $img=null;
33
    var $numcolors=100;
34
//---------------
35
// CONSTRUCTOR
36
    function Gradient(&$img) {
37
	$this->img = &$img;
38
    }
39
 
40
 
41
    function SetNumColors($aNum) {
42
	$this->numcolors=$aNum;
43
    }
44
//---------------
45
// PUBLIC METHODS
46
    // Produce a gradient filled rectangle with a smooth transition between
47
    // two colors.
48
    // ($xl,$yt) 	Top left corner
49
    // ($xr,$yb)	Bottom right
50
    // $from_color	Starting color in gradient
51
    // $to_color	End color in the gradient
52
    // $style		Which way is the gradient oriented?
53
    function FilledRectangle($xl,$yt,$xr,$yb,$from_color,$to_color,$style=1) {
54
	switch( $style ) {
55
	    case GRAD_VER:
56
		$steps = ceil(abs($xr-$xl));
57
		$delta = $xr>=$xl ? 1 : -1;
58
		$this->GetColArray($from_color,$to_color,$steps,$colors,$this->numcolors);
59
		for( $i=0, $x=$xl; $i < $steps; ++$i ) {
60
		    $this->img->current_color = $colors[$i];
61
		    $this->img->Line($x,$yt,$x,$yb);
62
		    $x += $delta;
63
		}
64
		break;
65
 
66
	    case GRAD_HOR:
67
		$steps = ceil(abs($yb-$yt));
68
		$delta = $yb>=$yt ? 1 : -1;
69
		$this->GetColArray($from_color,$to_color,$steps,$colors,$this->numcolors);
70
		for($i=0,$y=$yt; $i < $steps; ++$i) {
71
		    $this->img->current_color = $colors[$i];
72
		    $this->img->Line($xl,$y,$xr,$y);
73
		    $y += $delta;
74
		}
75
		break;
76
 
77
	    case GRAD_MIDHOR:
78
		$steps = ceil(abs($yb-$yt)/2);
79
		$delta = $yb >= $yt ? 1 : -1;
80
		$this->GetColArray($from_color,$to_color,$steps,$colors,$this->numcolors);
81
		for($y=$yt, $i=0; $i < $steps;  ++$i) {
82
		    $this->img->current_color = $colors[$i];
83
		    $this->img->Line($xl,$y,$xr,$y);
84
		    $y += $delta;
85
		}
86
		--$i;
87
		if( abs($yb-$yt) % 2 == 1 ) --$steps;
88
		for($j=0; $j < $steps; ++$j, --$i) {
89
		    $this->img->current_color = $colors[$i];
90
		    $this->img->Line($xl,$y,$xr,$y);
91
		    $y += $delta;
92
		}
93
		$this->img->Line($xl,$y,$xr,$y);
94
		break;
95
 
96
	    case GRAD_MIDVER:
97
		$steps = ceil(abs($xr-$xl)/2);
98
		$delta = $xr>=$xl ? 1 : -1;
99
		$this->GetColArray($from_color,$to_color,$steps,$colors,$this->numcolors);
100
		for($x=$xl, $i=0; $i < $steps; ++$i) {
101
		    $this->img->current_color = $colors[$i];
102
		    $this->img->Line($x,$yb,$x,$yt);
103
		    $x += $delta;
104
		}
105
		--$i;
106
		if( abs($xr-$xl) % 2 == 1 ) --$steps;
107
		for($j=0; $j < $steps; ++$j, --$i) {
108
		    $this->img->current_color = $colors[$i];
109
		    $this->img->Line($x,$yb,$x,$yt);
110
		    $x += $delta;
111
		}
112
		$this->img->Line($x,$yb,$x,$yt);
113
		break;
114
 
115
	    case GRAD_WIDE_MIDVER:
116
		$diff = ceil(abs($xr-$xl));
117
		$steps = floor(abs($diff)/3);
118
		$firststep = $diff - 2*$steps ;
119
		$delta = $xr >= $xl ? 1 : -1;
120
		$this->GetColArray($from_color,$to_color,$firststep,$colors,$this->numcolors);
121
		for($x=$xl, $i=0; $i < $firststep; ++$i) {
122
		    $this->img->current_color = $colors[$i];
123
		    $this->img->Line($x,$yb,$x,$yt);
124
		    $x += $delta;
125
		}
126
		--$i;
127
		$this->img->current_color = $colors[$i];
128
		for($j=0; $j< $steps; ++$j) {
129
		    $this->img->Line($x,$yb,$x,$yt);
130
		    $x += $delta;
131
		}
132
 
133
		for($j=0; $j < $steps; ++$j, --$i) {
134
		    $this->img->current_color = $colors[$i];
135
		    $this->img->Line($x,$yb,$x,$yt);
136
		    $x += $delta;
137
		}
138
		break;
139
 
140
	    case GRAD_WIDE_MIDHOR:
141
		$diff = ceil(abs($yb-$yt));
142
		$steps = floor(abs($diff)/3);
143
		$firststep = $diff - 2*$steps ;
144
		$delta = $yb >= $yt? 1 : -1;
145
		$this->GetColArray($from_color,$to_color,$firststep,$colors,$this->numcolors);
146
		for($y=$yt, $i=0; $i < $firststep;  ++$i) {
147
		    $this->img->current_color = $colors[$i];
148
		    $this->img->Line($xl,$y,$xr,$y);
149
		    $y += $delta;
150
		}
151
		--$i;
152
		$this->img->current_color = $colors[$i];
153
		for($j=0; $j < $steps; ++$j) {
154
		    $this->img->Line($xl,$y,$xr,$y);
155
		    $y += $delta;
156
		}
157
		for($j=0; $j < $steps; ++$j, --$i) {
158
		    $this->img->current_color = $colors[$i];
159
		    $this->img->Line($xl,$y,$xr,$y);
160
		    $y += $delta;
161
		}
162
		break;
163
 
164
	    case GRAD_LEFT_REFLECTION:
165
		$steps1 = ceil(0.3*abs($xr-$xl));
166
		$delta = $xr>=$xl ? 1 : -1;
167
 
168
		$from_color = $this->img->rgb->Color($from_color);
169
		$adj = 1.4;
170
		$m = ($adj-1.0)*(255-min(255,min($from_color[0],min($from_color[1],$from_color[2]))));
171
		$from_color2 = array(min(255,$from_color[0]+$m),
172
				    min(255,$from_color[1]+$m), min(255,$from_color[2]+$m));
173
 
174
		$this->GetColArray($from_color2,$to_color,$steps1,$colors,$this->numcolors);
175
		$n = count($colors);
176
		for($x=$xl, $i=0; $i < $steps1 && $i < $n; ++$i) {
177
		    $this->img->current_color = $colors[$i];
178
		    $this->img->Line($x,$yb,$x,$yt);
179
		    $x += $delta;
180
		}
181
		$steps2 = max(1,round(0.08*abs($xr-$xl)));
182
		$this->img->SetColor($to_color);
183
		for($j=0; $j< $steps2; ++$j) {
184
		    $this->img->Line($x,$yb,$x,$yt);
185
		    $x += $delta;
186
		}
187
		$steps = abs($xr-$xl)-$steps1-$steps2;
188
		$this->GetColArray($to_color,$from_color,$steps,$colors,$this->numcolors);
189
		$n = count($colors);
190
		for($i=0; $i < $steps && $i < $n; ++$i) {
191
		    $this->img->current_color = $colors[$i];
192
		    $this->img->Line($x,$yb,$x,$yt);
193
		    $x += $delta;
194
		}
195
		break;
196
 
197
	    case GRAD_RIGHT_REFLECTION:
198
		$steps1 = ceil(0.7*abs($xr-$xl));
199
		$delta = $xr>=$xl ? 1 : -1;
200
 
201
		$this->GetColArray($from_color,$to_color,$steps1,$colors,$this->numcolors);
202
		$n = count($colors);
203
		for($x=$xl, $i=0; $i < $steps1 && $i < $n; ++$i) {
204
		    $this->img->current_color = $colors[$i];
205
		    $this->img->Line($x,$yb,$x,$yt);
206
		    $x += $delta;
207
		}
208
		$steps2 = max(1,round(0.08*abs($xr-$xl)));
209
		$this->img->SetColor($to_color);
210
		for($j=0; $j< $steps2; ++$j) {
211
		    $this->img->Line($x,$yb,$x,$yt);
212
		    $x += $delta;
213
		}
214
 
215
		$from_color = $this->img->rgb->Color($from_color);
216
		$adj = 1.4;
217
		$m = ($adj-1.0)*(255-min(255,min($from_color[0],min($from_color[1],$from_color[2]))));
218
		$from_color = array(min(255,$from_color[0]+$m),
219
				    min(255,$from_color[1]+$m), min(255,$from_color[2]+$m));
220
 
221
		$steps = abs($xr-$xl)-$steps1-$steps2;
222
		$this->GetColArray($to_color,$from_color,$steps,$colors,$this->numcolors);
223
		$n = count($colors);
224
		for($i=0; $i < $steps && $i < $n; ++$i) {
225
		    $this->img->current_color = $colors[$i];
226
		    $this->img->Line($x,$yb,$x,$yt);
227
		    $x += $delta;
228
		}
229
		break;
230
 
231
	    case GRAD_CENTER:
232
		$steps = ceil(min(($yb-$yt)+1,($xr-$xl)+1)/2);
233
		$this->GetColArray($from_color,$to_color,$steps,$colors,$this->numcolors);
234
		$dx = ($xr-$xl)/2;
235
		$dy = ($yb-$yt)/2;
236
		$x=$xl;$y=$yt;$x2=$xr;$y2=$yb;
237
		$n = count($colors);
238
		for($x=$xl, $i=0; $x < $xl+$dx && $y < $yt+$dy && $i < $n; ++$x, ++$y, --$x2, --$y2, ++$i) {
239
		    $this->img->current_color = $colors[$i];
240
		    $this->img->Rectangle($x,$y,$x2,$y2);
241
		}
242
		$this->img->Line($x,$y,$x2,$y2);
243
		break;
244
 
245
	    case GRAD_RAISED_PANEL:
246
		// right to left
247
		$steps1 = $xr-$xl;
248
		$delta = $xr>=$xl ? 1 : -1;
249
		$this->GetColArray($to_color,$from_color,$steps1,$colors,$this->numcolors);
250
		$n = count($colors);
251
		for($x=$xl, $i=0; $i < $steps1 && $i < $n; ++$i) {
252
		    $this->img->current_color = $colors[$i];
253
		    $this->img->Line($x,$yb,$x,$yt);
254
		    $x += $delta;
255
		}
256
 
257
		// left to right
258
		$xr -= 3;
259
		$xl += 3;
260
		$yb -= 3;
261
		$yt += 3;
262
		$steps2 = $xr-$xl;
263
		$delta = $xr>=$xl ? 1 : -1;
264
		for($x=$xl, $j=$steps2; $j >= 0; --$j) {
265
		    $this->img->current_color = $colors[$j];
266
		    $this->img->Line($x,$yb,$x,$yt);
267
		    $x += $delta;
268
		}
269
		break;
270
 
271
	    case GRAD_DIAGONAL:
272
		// use the longer dimension to determine the required number of steps.
273
		// first loop draws from one corner to the mid-diagonal and the second
274
		// loop draws from the mid-diagonal to the opposing corner.
275
		if($xr-$xl > $yb - $yt) {
276
		    // width is greater than height -> use x-dimension for steps
277
		    $steps = $xr-$xl;
278
		    $delta = $xr>=$xl ? 1 : -1;
279
		    $this->GetColArray($from_color,$to_color,$steps*2,$colors,$this->numcolors);
280
		    $n = count($colors);
281
 
282
		    for($x=$xl, $i=0; $i < $steps && $i < $n; ++$i) {
283
			$this->img->current_color = $colors[$i];
284
			$y = $yt+($i/$steps)*($yb-$yt)*$delta;
285
			$this->img->Line($x,$yt,$xl,$y);
286
			$x += $delta;
287
		    }
288
 
289
		    for($x=$xl, $i = 0; $i < $steps && $i < $n; ++$i) {
290
			$this->img->current_color = $colors[$steps+$i];
291
			$y = $yt+($i/$steps)*($yb-$yt)*$delta;
292
			$this->img->Line($x,$yb,$xr,$y);
293
			$x += $delta;
294
		    }
295
		} else {
296
		    // height is greater than width -> use y-dimension for steps
297
		    $steps = $yb-$yt;
298
		    $delta = $yb>=$yt ? 1 : -1;
299
		    $this->GetColArray($from_color,$to_color,$steps*2,$colors,$this->numcolors);
300
		    $n = count($colors);
301
 
302
		    for($y=$yt, $i=0; $i < $steps && $i < $n; ++$i) {
303
			$this->img->current_color = $colors[$i];
304
			$x = $xl+($i/$steps)*($xr-$xl)*$delta;
305
			$this->img->Line($x,$yt,$xl,$y);
306
			$y += $delta;
307
		    }
308
 
309
		    for($y=$yt, $i = 0; $i < $steps && $i < $n; ++$i) {
310
			$this->img->current_color = $colors[$steps+$i];
311
			$x = $xl+($i/$steps)*($xr-$xl)*$delta;
312
			$this->img->Line($x,$yb,$xr,$y);
313
			$x += $delta;
314
		    }
315
 
316
		}
317
		break;
318
 
319
	    default:
320
		JpGraphError::RaiseL(7001,$style);
321
//("Unknown gradient style (=$style).");
322
		break;
323
	}
324
    }
325
 
326
    // Fill a special case of a polygon with a flat bottom
327
    // with a gradient. Can be used for filled line plots.
328
    // Please note that this is NOT a generic gradient polygon fill
329
    // routine. It assumes that the bottom is flat (like a drawing
330
    // of a mountain)
331
    function FilledFlatPolygon($pts,$from_color,$to_color) {
332
	if( count($pts) == 0 ) return;
333
 
334
	$maxy=$pts[1];
335
	$miny=$pts[1];
336
	$n = count($pts) ;
337
	for( $i=0, $idx=0; $i < $n; $i += 2) {
338
	    $x = floor($pts[$i]);
339
	    $y = floor($pts[$i+1]);
340
	    $miny = min($miny,$y);
341
	    $maxy = max($maxy,$y);
342
	}
343
 
344
	$colors = array();
345
	$this->GetColArray($from_color,$to_color,abs($maxy-$miny)+1,$colors,$this->numcolors);
346
	for($i=$miny, $idx=0; $i <= $maxy; ++$i ) {
347
	    $colmap[$i] = $colors[$idx++];
348
	}
349
 
350
	$n = count($pts)/2 ;
351
	$idx = 0 ;
352
	while( $idx < $n-1 ) {
353
	    $p1 = array(round($pts[$idx*2]),round($pts[$idx*2+1]));
354
	    $p2 = array(round($pts[++$idx*2]),round($pts[$idx*2+1]));
355
 
356
	    // Find the largest rectangle we can fill
357
	    $y = max($p1[1],$p2[1]) ;
358
	    for($yy=$maxy; $yy > $y; --$yy) {
359
		$this->img->current_color = $colmap[$yy];
360
		$this->img->Line($p1[0],$yy,$p2[0]-1,$yy);
361
	    }
362
 
363
	    if( $p1[1] == $p2[1] ) continue;
364
 
365
	    // Fill the rest using lines (slow...)
366
	    $slope = ($p2[0]-$p1[0])/($p1[1]-$p2[1]);
367
	    $x1 = $p1[0];
368
	    $x2 = $p2[0]-1;
369
	    $start = $y;
370
	    if( $p1[1] > $p2[1] ) {
371
		while( $y >= $p2[1] ) {
372
		    $x1=$slope*($start-$y)+$p1[0];
373
		    $this->img->current_color = $colmap[$y];
374
		    $this->img->Line($x1,$y,$x2,$y);
375
		    --$y;
376
		}
377
	    }
378
	    else {
379
		while( $y >= $p1[1] ) {
380
		    $x2=$p2[0]+$slope*($start-$y);
381
		    $this->img->current_color = $colmap[$y];
382
		    $this->img->Line($x1,$y,$x2,$y);
383
		    --$y;
384
		}
385
	    }
386
	}
387
    }
388
 
389
//---------------
390
// PRIVATE METHODS
391
    // Add to the image color map the necessary colors to do the transition
392
    // between the two colors using $numcolors intermediate colors
393
    function GetColArray($from_color,$to_color,$arr_size,&$colors,$numcols=100) {
394
	if( $arr_size==0 ) return;
395
	// If color is given as text get it's corresponding r,g,b values
396
	$from_color = $this->img->rgb->Color($from_color);
397
	$to_color = $this->img->rgb->Color($to_color);
398
 
399
	$rdelta=($to_color[0]-$from_color[0])/$numcols;
400
	$gdelta=($to_color[1]-$from_color[1])/$numcols;
401
	$bdelta=($to_color[2]-$from_color[2])/$numcols;
402
	$colorsperstep	= $numcols/$arr_size;
403
	$prevcolnum	= -1;
404
	$from_alpha = $from_color[3];
405
	$to_alpha = $to_color[3];
406
	$adelta = ( $to_alpha - $from_alpha ) / $numcols ;
407
	for ($i=0; $i < $arr_size; ++$i) {
408
	    $colnum = floor($colorsperstep*$i);
409
	    if ( $colnum == $prevcolnum )
410
		$colors[$i]	= $colidx;
411
	    else {
412
		$r = floor($from_color[0] + $colnum*$rdelta);
413
		$g = floor($from_color[1] + $colnum*$gdelta);
414
		$b = floor($from_color[2] + $colnum*$bdelta);
415
		$alpha = $from_alpha + $colnum*$adelta;
416
		$colidx = $this->img->rgb->Allocate(sprintf("#%02x%02x%02x",$r,$g,$b),$alpha);
417
		$colors[$i] = $colidx;
418
	    }
419
	    $prevcolnum = $colnum;
420
	}
421
    }
422
} // Class
423
 
424
?>