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