Fill.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * PHPExcel_Style_Fill
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Style
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Style_Fill extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  28. {
  29. /* Fill types */
  30. const FILL_NONE = 'none';
  31. const FILL_SOLID = 'solid';
  32. const FILL_GRADIENT_LINEAR = 'linear';
  33. const FILL_GRADIENT_PATH = 'path';
  34. const FILL_PATTERN_DARKDOWN = 'darkDown';
  35. const FILL_PATTERN_DARKGRAY = 'darkGray';
  36. const FILL_PATTERN_DARKGRID = 'darkGrid';
  37. const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
  38. const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
  39. const FILL_PATTERN_DARKUP = 'darkUp';
  40. const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
  41. const FILL_PATTERN_GRAY0625 = 'gray0625';
  42. const FILL_PATTERN_GRAY125 = 'gray125';
  43. const FILL_PATTERN_LIGHTDOWN = 'lightDown';
  44. const FILL_PATTERN_LIGHTGRAY = 'lightGray';
  45. const FILL_PATTERN_LIGHTGRID = 'lightGrid';
  46. const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
  47. const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
  48. const FILL_PATTERN_LIGHTUP = 'lightUp';
  49. const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
  50. const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
  51. /**
  52. * Fill type
  53. *
  54. * @var string
  55. */
  56. protected $fillType = PHPExcel_Style_Fill::FILL_NONE;
  57. /**
  58. * Rotation
  59. *
  60. * @var double
  61. */
  62. protected $rotation = 0;
  63. /**
  64. * Start color
  65. *
  66. * @var PHPExcel_Style_Color
  67. */
  68. protected $startColor;
  69. /**
  70. * End color
  71. *
  72. * @var PHPExcel_Style_Color
  73. */
  74. protected $endColor;
  75. /**
  76. * Create a new PHPExcel_Style_Fill
  77. *
  78. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  79. * Leave this value at default unless you understand exactly what
  80. * its ramifications are
  81. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  82. * Leave this value at default unless you understand exactly what
  83. * its ramifications are
  84. */
  85. public function __construct($isSupervisor = false, $isConditional = false)
  86. {
  87. // Supervisor?
  88. parent::__construct($isSupervisor);
  89. // Initialise values
  90. if ($isConditional) {
  91. $this->fillType = null;
  92. }
  93. $this->startColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE, $isSupervisor, $isConditional);
  94. $this->endColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor, $isConditional);
  95. // bind parent if we are a supervisor
  96. if ($isSupervisor) {
  97. $this->startColor->bindParent($this, 'startColor');
  98. $this->endColor->bindParent($this, 'endColor');
  99. }
  100. }
  101. /**
  102. * Get the shared style component for the currently active cell in currently active sheet.
  103. * Only used for style supervisor
  104. *
  105. * @return PHPExcel_Style_Fill
  106. */
  107. public function getSharedComponent()
  108. {
  109. return $this->parent->getSharedComponent()->getFill();
  110. }
  111. /**
  112. * Build style array from subcomponents
  113. *
  114. * @param array $array
  115. * @return array
  116. */
  117. public function getStyleArray($array)
  118. {
  119. return array('fill' => $array);
  120. }
  121. /**
  122. * Apply styles from array
  123. *
  124. * <code>
  125. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  126. * array(
  127. * 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  128. * 'rotation' => 0,
  129. * 'startcolor' => array(
  130. * 'rgb' => '000000'
  131. * ),
  132. * 'endcolor' => array(
  133. * 'argb' => 'FFFFFFFF'
  134. * )
  135. * )
  136. * );
  137. * </code>
  138. *
  139. * @param array $pStyles Array containing style information
  140. * @throws PHPExcel_Exception
  141. * @return PHPExcel_Style_Fill
  142. */
  143. public function applyFromArray($pStyles = null)
  144. {
  145. if (is_array($pStyles)) {
  146. if ($this->isSupervisor) {
  147. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  148. } else {
  149. if (array_key_exists('type', $pStyles)) {
  150. $this->setFillType($pStyles['type']);
  151. }
  152. if (array_key_exists('rotation', $pStyles)) {
  153. $this->setRotation($pStyles['rotation']);
  154. }
  155. if (array_key_exists('startcolor', $pStyles)) {
  156. $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  157. }
  158. if (array_key_exists('endcolor', $pStyles)) {
  159. $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  160. }
  161. if (array_key_exists('color', $pStyles)) {
  162. $this->getStartColor()->applyFromArray($pStyles['color']);
  163. }
  164. }
  165. } else {
  166. throw new PHPExcel_Exception("Invalid style array passed.");
  167. }
  168. return $this;
  169. }
  170. /**
  171. * Get Fill Type
  172. *
  173. * @return string
  174. */
  175. public function getFillType()
  176. {
  177. if ($this->isSupervisor) {
  178. return $this->getSharedComponent()->getFillType();
  179. }
  180. return $this->fillType;
  181. }
  182. /**
  183. * Set Fill Type
  184. *
  185. * @param string $pValue PHPExcel_Style_Fill fill type
  186. * @return PHPExcel_Style_Fill
  187. */
  188. public function setFillType($pValue = PHPExcel_Style_Fill::FILL_NONE)
  189. {
  190. if ($this->isSupervisor) {
  191. $styleArray = $this->getStyleArray(array('type' => $pValue));
  192. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  193. } else {
  194. $this->fillType = $pValue;
  195. }
  196. return $this;
  197. }
  198. /**
  199. * Get Rotation
  200. *
  201. * @return double
  202. */
  203. public function getRotation()
  204. {
  205. if ($this->isSupervisor) {
  206. return $this->getSharedComponent()->getRotation();
  207. }
  208. return $this->rotation;
  209. }
  210. /**
  211. * Set Rotation
  212. *
  213. * @param double $pValue
  214. * @return PHPExcel_Style_Fill
  215. */
  216. public function setRotation($pValue = 0)
  217. {
  218. if ($this->isSupervisor) {
  219. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  220. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  221. } else {
  222. $this->rotation = $pValue;
  223. }
  224. return $this;
  225. }
  226. /**
  227. * Get Start Color
  228. *
  229. * @return PHPExcel_Style_Color
  230. */
  231. public function getStartColor()
  232. {
  233. return $this->startColor;
  234. }
  235. /**
  236. * Set Start Color
  237. *
  238. * @param PHPExcel_Style_Color $pValue
  239. * @throws PHPExcel_Exception
  240. * @return PHPExcel_Style_Fill
  241. */
  242. public function setStartColor(PHPExcel_Style_Color $pValue = null)
  243. {
  244. // make sure parameter is a real color and not a supervisor
  245. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  246. if ($this->isSupervisor) {
  247. $styleArray = $this->getStartColor()->getStyleArray(array('argb' => $color->getARGB()));
  248. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  249. } else {
  250. $this->startColor = $color;
  251. }
  252. return $this;
  253. }
  254. /**
  255. * Get End Color
  256. *
  257. * @return PHPExcel_Style_Color
  258. */
  259. public function getEndColor()
  260. {
  261. return $this->endColor;
  262. }
  263. /**
  264. * Set End Color
  265. *
  266. * @param PHPExcel_Style_Color $pValue
  267. * @throws PHPExcel_Exception
  268. * @return PHPExcel_Style_Fill
  269. */
  270. public function setEndColor(PHPExcel_Style_Color $pValue = null)
  271. {
  272. // make sure parameter is a real color and not a supervisor
  273. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  274. if ($this->isSupervisor) {
  275. $styleArray = $this->getEndColor()->getStyleArray(array('argb' => $color->getARGB()));
  276. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  277. } else {
  278. $this->endColor = $color;
  279. }
  280. return $this;
  281. }
  282. /**
  283. * Get hash code
  284. *
  285. * @return string Hash code
  286. */
  287. public function getHashCode()
  288. {
  289. if ($this->isSupervisor) {
  290. return $this->getSharedComponent()->getHashCode();
  291. }
  292. return md5(
  293. $this->getFillType() .
  294. $this->getRotation() .
  295. $this->getStartColor()->getHashCode() .
  296. $this->getEndColor()->getHashCode() .
  297. __CLASS__
  298. );
  299. }
  300. }