jpgraph_gradient.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:7k
源码类别:

电子政务应用

开发平台:

Java

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