PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/jpgraph/jpgraph_gradient.php

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