Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
<?php
2
/*=======================================================================
3
// File:	JPGRAPH_GRADIENT.PHP
4
// Description:	Create a color gradient
5
// Created: 	2003-02-01
6
// Author:	Johan Persson (johanp@aditus.nu)
7
// Ver:		$Id: jpgraph_gradient.php,v 1.1 2004/06/15 10:13:19 jpm Exp $
8
//
9
// License:	This code is released under QPL
10
// Copyright (C) 2003 Johan Persson
11
//========================================================================
12
*/
13
 
14
 
15
//===================================================
16
// CLASS Gradient
17
// Description: Handles gradient fills. This is to be
18
// considered a "friend" class of Class Image.
19
//===================================================
20
class Gradient {
21
    var $img=null;
22
//---------------
23
// CONSTRUCTOR
24
    function Gradient(&$img) {
25
	$this->img = $img;
26
    }
27
 
28
//---------------
29
// PUBLIC METHODS
30
    // Produce a gradient filled rectangle with a smooth transition between
31
    // two colors.
32
    // ($xl,$yt) 	Top left corner
33
    // ($xr,$yb)	Bottom right
34
    // $from_color	Starting color in gradient
35
    // $to_color	End color in the gradient
36
    // $style		Which way is the gradient oriented?
37
    function FilledRectangle($xl,$yt,$xr,$yb,$from_color,$to_color,$style=1) {
38
	switch( $style ) {
39
	    case 1:  // HORIZONTAL
40
		$steps = abs($xr-$xl);
41
		$delta = $xr>=$xl ? 1 : -1;
42
		$this->GetColArray($from_color,$to_color,$steps,$colors);
43
		for( $i=0, $x=$xl; $i<$steps; ++$i ) {
44
		    $this->img->current_color = $colors[$i];
45
		    $this->img->Line($x,$yt,$x,$yb);
46
		    $x += $delta;
47
		}
48
		break;
49
 
50
	    case 2: // VERTICAL
51
		$steps = abs($yb-$yt);
52
		$delta = $yb>=$yt ? 1 : -1;
53
		$this->GetColArray($from_color,$to_color,$steps,$colors);
54
		for($i=0,$y=$yt; $i<$steps; ++$i) {
55
		    $this->img->current_color = $colors[$i];
56
		    $this->img->Line($xl,$y,$xr,$y);
57
		    $y += $delta;
58
		}
59
		break;
60
 
61
	    case 3: // VERTICAL FROM MIDDLE
62
		$steps = abs($yb-$yt)/2;
63
		$delta = $yb>=$yt ? 1 : -1;
64
		$this->GetColArray($from_color,$to_color,$steps,$colors);
65
		for($y=$yt, $i=0; $i < $steps;  ++$i) {
66
		    $this->img->current_color = $colors[$i];
67
		    $this->img->Line($xl,$y,$xr,$y);
68
		    $y += $delta;
69
		}
70
		--$i;
71
		for($j=0; $j < $steps; ++$j, --$i) {
72
		    $this->img->current_color = $colors[$i];
73
		    $this->img->Line($xl,$y,$xr,$y);
74
		    $y += $delta;
75
		}
76
		$this->img->Line($xl,$y,$xr,$y);
77
		break;
78
 
79
	    case 4: // HORIZONTAL FROM MIDDLE
80
		$steps = abs($xr-$xl)/2;
81
		$delta = $xr>=$xl ? 1 : -1;
82
		$this->GetColArray($from_color,$to_color,$steps,$colors);
83
		for($x=$xl, $i=0; $i<$steps; ++$i) {
84
		    $this->img->current_color = $colors[$i];
85
		    $this->img->Line($x,$yb,$x,$yt);
86
		    $x += $delta;
87
		}
88
		--$i;
89
		for($j=0; $j<$steps; ++$j, --$i) {
90
		    $this->img->current_color = $colors[$i];
91
		    $this->img->Line($x,$yb,$x,$yt);
92
		    $x += $delta;
93
		}
94
		$this->img->Line($x,$yb,$x,$yt);
95
		break;
96
 
97
	    case 6: // HORIZONTAL WIDER MIDDLE
98
		$steps = abs($xr-$xl)/3;
99
		$delta = $xr>=$xl ? 1 : -1;
100
		$this->GetColArray($from_color,$to_color,$steps,$colors);
101
		for($x=$xl, $i=0; $i < $steps; ++$i) {
102
		    $this->img->current_color = $colors[$i];
103
		    $this->img->Line($x,$yb,$x,$yt);
104
		    $x += $delta;
105
		}
106
		--$i;
107
		$this->img->current_color = $colors[$i];
108
		for($j=0; $j< $steps; ++$j) {
109
		    $this->img->Line($x,$yb,$x,$yt);
110
		    $x += $delta;
111
		}
112
 
113
		for($j=0; $j<$steps; ++$j, --$i) {
114
		    $this->img->current_color = $colors[$i];
115
		    $this->img->Line($x,$yb,$x,$yt);
116
		    $x += $delta;
117
		}
118
		break;
119
 
120
 
121
	    case 8:  // LEFT REFLECTION
122
		$steps1 = round(0.3*abs($xr-$xl));
123
		$delta = $xr>=$xl ? 1 : -1;
124
 
125
		$this->GetColArray($from_color.':1.3',$to_color,$steps1,$colors);
126
		for($x=$xl, $i=0; $i < $steps1; ++$i) {
127
		    $this->img->current_color = $colors[$i];
128
		    $this->img->Line($x,$yb,$x,$yt);
129
		    $x += $delta;
130
		}
131
		$steps2 = max(1,round(0.08*abs($xr-$xl)));
132
		$this->img->SetColor($to_color);
133
		for($j=0; $j< $steps2; ++$j) {
134
		    $this->img->Line($x,$yb,$x,$yt);
135
		    $x += $delta;
136
		}
137
		$steps = abs($xr-$xl)-$steps1-$steps2;
138
		$this->GetColArray($to_color,$from_color,$steps,$colors);
139
		for($i=0; $i < $steps; ++$i) {
140
		    $this->img->current_color = $colors[$i];
141
		    $this->img->Line($x,$yb,$x,$yt);
142
		    $x += $delta;
143
		}
144
		break;
145
 
146
	    case 9:  // RIGHT REFLECTION
147
		$steps1 = round(0.7*abs($xr-$xl));
148
		$delta = $xr>=$xl ? 1 : -1;
149
 
150
		$this->GetColArray($from_color,$to_color,$steps1,$colors);
151
		for($x=$xl, $i=0; $i < $steps1; ++$i) {
152
		    $this->img->current_color = $colors[$i];
153
		    $this->img->Line($x,$yb,$x,$yt);
154
		    $x += $delta;
155
		}
156
		$steps2 = max(1,round(0.08*abs($xr-$xl)));
157
		$this->img->SetColor($to_color);
158
		for($j=0; $j< $steps2; ++$j) {
159
		    $this->img->Line($x,$yb,$x,$yt);
160
		    $x += $delta;
161
		}
162
		$steps = abs($xr-$xl)-$steps1-$steps2;
163
		$this->GetColArray($to_color,$from_color.':1.3',$steps,$colors);
164
		for($i=0; $i < $steps; ++$i) {
165
		    $this->img->current_color = $colors[$i];
166
		    $this->img->Line($x,$yb,$x,$yt);
167
		    $x += $delta;
168
		}
169
		break;
170
 
171
 
172
 
173
	    case 7: // VERTICAL WIDER MIDDLE
174
		$steps = abs($yb-$yt)/3;
175
		$delta = $yb>=$yt? 1 : -1;
176
		$this->GetColArray($from_color,$to_color,$steps,$colors);
177
		for($y=$yt, $i=0; $i<$steps;  ++$i) {
178
		    $this->img->current_color = $colors[$i];
179
		    $this->img->Line($xl,$y,$xr,$y);
180
		    $y += $delta;
181
		}
182
		--$i;
183
		$this->img->current_color = $colors[$i];
184
		for($j=0; $j< $steps; ++$j) {
185
		    $this->img->Line($xl,$y,$xr,$y);
186
		    $y += $delta;
187
		}
188
		for($j=0; $j<$steps; ++$j, --$i) {
189
		    $this->img->current_color = $colors[$i];
190
		    $this->img->Line($xl,$y,$xr,$y);
191
		    $y += $delta;
192
		}
193
		break;
194
 
195
	    case 5: // Rectangle
196
		$steps = floor(min(($yb-$yt)+1,($xr-$xl)+1)/2);
197
		$this->GetColArray($from_color,$to_color,$steps,$colors);
198
		$dx = ($xr-$xl)/2;
199
		$dy = ($yb-$yt)/2;
200
		$x=$xl;$y=$yt;$x2=$xr;$y2=$yb;
201
		for($x=$xl, $i=0; $x<$xl+$dx && $y<$yt+$dy ; ++$x, ++$y, --$x2, --$y2, ++$i) {
202
		    assert($i<count($colors));
203
		    $this->img->current_color = $colors[$i];
204
		    $this->img->Rectangle($x,$y,$x2,$y2);
205
		}
206
		$this->img->Line($x,$y,$x2,$y2);
207
		break;
208
 
209
	    default:
210
		die("JpGraph Error: Unknown gradient style (=$style).");
211
		break;
212
	}
213
    }
214
 
215
//---------------
216
// PRIVATE METHODS
217
    // Add to the image color map the necessary colors to do the transition
218
    // between the two colors using $numcolors intermediate colors
219
    function GetColArray($from_color,$to_color,$arr_size,&$colors,$numcols=100) {
220
	if( $arr_size==0 ) return;
221
	// If color is given as text get it's corresponding r,g,b values
222
	$from_color = $this->img->rgb->Color($from_color);
223
	$to_color = $this->img->rgb->Color($to_color);
224
 
225
	$rdelta=($to_color[0]-$from_color[0])/$numcols;
226
	$gdelta=($to_color[1]-$from_color[1])/$numcols;
227
	$bdelta=($to_color[2]-$from_color[2])/$numcols;
228
	$colorsperstep	= $numcols/$arr_size;
229
	$prevcolnum	= -1;
230
	for ($i=0; $i<$arr_size; ++$i) {
231
	    $colnum	= floor($colorsperstep*$i);
232
	    if ( $colnum == $prevcolnum )
233
		$colors[$i]	= $colidx;
234
	    else {
235
		$r = floor($from_color[0] + $colnum*$rdelta);
236
		$g = floor($from_color[1] + $colnum*$gdelta);
237
		$b = floor($from_color[2] + $colnum*$bdelta);
238
		$colidx = $this->img->rgb->Allocate(sprintf("#%02x%02x%02x",$r,$g,$b));
239
		$colors[$i]	= $colidx;
240
	    }
241
	    $prevcolnum = $colnum;
242
	}
243
    }
244
} // Class
245
 
246
?>