Fill.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.8.0, 2014-03-02
  26. */
  27. /**
  28. * PHPExcel_Style_Fill
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Fill extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  35. {
  36. /* Fill types */
  37. const FILL_NONE = 'none';
  38. const FILL_SOLID = 'solid';
  39. const FILL_GRADIENT_LINEAR = 'linear';
  40. const FILL_GRADIENT_PATH = 'path';
  41. const FILL_PATTERN_DARKDOWN = 'darkDown';
  42. const FILL_PATTERN_DARKGRAY = 'darkGray';
  43. const FILL_PATTERN_DARKGRID = 'darkGrid';
  44. const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
  45. const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
  46. const FILL_PATTERN_DARKUP = 'darkUp';
  47. const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
  48. const FILL_PATTERN_GRAY0625 = 'gray0625';
  49. const FILL_PATTERN_GRAY125 = 'gray125';
  50. const FILL_PATTERN_LIGHTDOWN = 'lightDown';
  51. const FILL_PATTERN_LIGHTGRAY = 'lightGray';
  52. const FILL_PATTERN_LIGHTGRID = 'lightGrid';
  53. const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
  54. const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
  55. const FILL_PATTERN_LIGHTUP = 'lightUp';
  56. const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
  57. const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
  58. /**
  59. * Fill type
  60. *
  61. * @var string
  62. */
  63. protected $_fillType = PHPExcel_Style_Fill::FILL_NONE;
  64. /**
  65. * Rotation
  66. *
  67. * @var double
  68. */
  69. protected $_rotation = 0;
  70. /**
  71. * Start color
  72. *
  73. * @var PHPExcel_Style_Color
  74. */
  75. protected $_startColor;
  76. /**
  77. * End color
  78. *
  79. * @var PHPExcel_Style_Color
  80. */
  81. protected $_endColor;
  82. /**
  83. * Create a new PHPExcel_Style_Fill
  84. *
  85. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  86. * Leave this value at default unless you understand exactly what
  87. * its ramifications are
  88. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  89. * Leave this value at default unless you understand exactly what
  90. * its ramifications are
  91. */
  92. public function __construct($isSupervisor = FALSE, $isConditional = FALSE)
  93. {
  94. // Supervisor?
  95. parent::__construct($isSupervisor);
  96. // Initialise values
  97. if ($isConditional) {
  98. $this->_fillType = NULL;
  99. }
  100. $this->_startColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE, $isSupervisor, $isConditional);
  101. $this->_endColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor, $isConditional);
  102. // bind parent if we are a supervisor
  103. if ($isSupervisor) {
  104. $this->_startColor->bindParent($this, '_startColor');
  105. $this->_endColor->bindParent($this, '_endColor');
  106. }
  107. }
  108. /**
  109. * Get the shared style component for the currently active cell in currently active sheet.
  110. * Only used for style supervisor
  111. *
  112. * @return PHPExcel_Style_Fill
  113. */
  114. public function getSharedComponent()
  115. {
  116. return $this->_parent->getSharedComponent()->getFill();
  117. }
  118. /**
  119. * Build style array from subcomponents
  120. *
  121. * @param array $array
  122. * @return array
  123. */
  124. public function getStyleArray($array)
  125. {
  126. return array('fill' => $array);
  127. }
  128. /**
  129. * Apply styles from array
  130. *
  131. * <code>
  132. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  133. * array(
  134. * 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  135. * 'rotation' => 0,
  136. * 'startcolor' => array(
  137. * 'rgb' => '000000'
  138. * ),
  139. * 'endcolor' => array(
  140. * 'argb' => 'FFFFFFFF'
  141. * )
  142. * )
  143. * );
  144. * </code>
  145. *
  146. * @param array $pStyles Array containing style information
  147. * @throws PHPExcel_Exception
  148. * @return PHPExcel_Style_Fill
  149. */
  150. public function applyFromArray($pStyles = null) {
  151. if (is_array($pStyles)) {
  152. if ($this->_isSupervisor) {
  153. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  154. } else {
  155. if (array_key_exists('type', $pStyles)) {
  156. $this->setFillType($pStyles['type']);
  157. }
  158. if (array_key_exists('rotation', $pStyles)) {
  159. $this->setRotation($pStyles['rotation']);
  160. }
  161. if (array_key_exists('startcolor', $pStyles)) {
  162. $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  163. }
  164. if (array_key_exists('endcolor', $pStyles)) {
  165. $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  166. }
  167. if (array_key_exists('color', $pStyles)) {
  168. $this->getStartColor()->applyFromArray($pStyles['color']);
  169. }
  170. }
  171. } else {
  172. throw new PHPExcel_Exception("Invalid style array passed.");
  173. }
  174. return $this;
  175. }
  176. /**
  177. * Get Fill Type
  178. *
  179. * @return string
  180. */
  181. public function getFillType() {
  182. if ($this->_isSupervisor) {
  183. return $this->getSharedComponent()->getFillType();
  184. }
  185. return $this->_fillType;
  186. }
  187. /**
  188. * Set Fill Type
  189. *
  190. * @param string $pValue PHPExcel_Style_Fill fill type
  191. * @return PHPExcel_Style_Fill
  192. */
  193. public function setFillType($pValue = PHPExcel_Style_Fill::FILL_NONE) {
  194. if ($this->_isSupervisor) {
  195. $styleArray = $this->getStyleArray(array('type' => $pValue));
  196. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  197. } else {
  198. $this->_fillType = $pValue;
  199. }
  200. return $this;
  201. }
  202. /**
  203. * Get Rotation
  204. *
  205. * @return double
  206. */
  207. public function getRotation() {
  208. if ($this->_isSupervisor) {
  209. return $this->getSharedComponent()->getRotation();
  210. }
  211. return $this->_rotation;
  212. }
  213. /**
  214. * Set Rotation
  215. *
  216. * @param double $pValue
  217. * @return PHPExcel_Style_Fill
  218. */
  219. public function setRotation($pValue = 0) {
  220. if ($this->_isSupervisor) {
  221. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  222. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  223. } else {
  224. $this->_rotation = $pValue;
  225. }
  226. return $this;
  227. }
  228. /**
  229. * Get Start Color
  230. *
  231. * @return PHPExcel_Style_Color
  232. */
  233. public function getStartColor() {
  234. return $this->_startColor;
  235. }
  236. /**
  237. * Set Start Color
  238. *
  239. * @param PHPExcel_Style_Color $pValue
  240. * @throws PHPExcel_Exception
  241. * @return PHPExcel_Style_Fill
  242. */
  243. public function setStartColor(PHPExcel_Style_Color $pValue = null) {
  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. return $this->_endColor;
  261. }
  262. /**
  263. * Set End Color
  264. *
  265. * @param PHPExcel_Style_Color $pValue
  266. * @throws PHPExcel_Exception
  267. * @return PHPExcel_Style_Fill
  268. */
  269. public function setEndColor(PHPExcel_Style_Color $pValue = null) {
  270. // make sure parameter is a real color and not a supervisor
  271. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  272. if ($this->_isSupervisor) {
  273. $styleArray = $this->getEndColor()->getStyleArray(array('argb' => $color->getARGB()));
  274. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  275. } else {
  276. $this->_endColor = $color;
  277. }
  278. return $this;
  279. }
  280. /**
  281. * Get hash code
  282. *
  283. * @return string Hash code
  284. */
  285. public function getHashCode() {
  286. if ($this->_isSupervisor) {
  287. return $this->getSharedComponent()->getHashCode();
  288. }
  289. return md5(
  290. $this->getFillType()
  291. . $this->getRotation()
  292. . $this->getStartColor()->getHashCode()
  293. . $this->getEndColor()->getHashCode()
  294. . __CLASS__
  295. );
  296. }
  297. }