PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/Common/3rdParty/jpgraph/src/jpgraph_gradient.php

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