PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel_1.7.8-with_documentation-msoffice_format/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Style/Fill.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 409 lines | 178 code | 32 blank | 199 comment | 26 complexity | c3d6a698de03cf59cbc4e45f7496cd83 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_Style_Fill
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Style
  32. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Style_Fill 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. private $_fillType = PHPExcel_Style_Fill::FILL_NONE;
  64. /**
  65. * Rotation
  66. *
  67. * @var double
  68. */
  69. private $_rotation = 0;
  70. /**
  71. * Start color
  72. *
  73. * @var PHPExcel_Style_Color
  74. */
  75. private $_startColor;
  76. /**
  77. * End color
  78. *
  79. * @var PHPExcel_Style_Color
  80. */
  81. private $_endColor;
  82. /**
  83. * Parent Borders
  84. *
  85. * @var _parentPropertyName string
  86. */
  87. private $_parentPropertyName;
  88. /**
  89. * Supervisor?
  90. *
  91. * @var boolean
  92. */
  93. private $_isSupervisor;
  94. /**
  95. * Parent. Only used for supervisor
  96. *
  97. * @var PHPExcel_Style
  98. */
  99. private $_parent;
  100. /**
  101. * Create a new PHPExcel_Style_Fill
  102. *
  103. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  104. * Leave this value at default unless you understand exactly what
  105. * its ramifications are
  106. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  107. * Leave this value at default unless you understand exactly what
  108. * its ramifications are
  109. */
  110. public function __construct($isSupervisor = false, $isConditional = false)
  111. {
  112. // Supervisor?
  113. $this->_isSupervisor = $isSupervisor;
  114. // Initialise values
  115. if ($isConditional) {
  116. $this->_fillType = NULL;
  117. }
  118. $this->_startColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE, $isSupervisor, $isConditional);
  119. $this->_endColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor, $isConditional);
  120. // bind parent if we are a supervisor
  121. if ($isSupervisor) {
  122. $this->_startColor->bindParent($this, '_startColor');
  123. $this->_endColor->bindParent($this, '_endColor');
  124. }
  125. }
  126. /**
  127. * Bind parent. Only used for supervisor
  128. *
  129. * @param PHPExcel_Style $parent
  130. * @return PHPExcel_Style_Fill
  131. */
  132. public function bindParent($parent)
  133. {
  134. $this->_parent = $parent;
  135. return $this;
  136. }
  137. /**
  138. * Is this a supervisor or a real style component?
  139. *
  140. * @return boolean
  141. */
  142. public function getIsSupervisor()
  143. {
  144. return $this->_isSupervisor;
  145. }
  146. /**
  147. * Get the shared style component for the currently active cell in currently active sheet.
  148. * Only used for style supervisor
  149. *
  150. * @return PHPExcel_Style_Fill
  151. */
  152. public function getSharedComponent()
  153. {
  154. return $this->_parent->getSharedComponent()->getFill();
  155. }
  156. /**
  157. * Get the currently active sheet. Only used for supervisor
  158. *
  159. * @return PHPExcel_Worksheet
  160. */
  161. public function getActiveSheet()
  162. {
  163. return $this->_parent->getActiveSheet();
  164. }
  165. /**
  166. * Get the currently active cell coordinate in currently active sheet.
  167. * Only used for supervisor
  168. *
  169. * @return string E.g. 'A1'
  170. */
  171. public function getSelectedCells()
  172. {
  173. return $this->getActiveSheet()->getSelectedCells();
  174. }
  175. /**
  176. * Get the currently active cell coordinate in currently active sheet.
  177. * Only used for supervisor
  178. *
  179. * @return string E.g. 'A1'
  180. */
  181. public function getActiveCell()
  182. {
  183. return $this->getActiveSheet()->getActiveCell();
  184. }
  185. /**
  186. * Build style array from subcomponents
  187. *
  188. * @param array $array
  189. * @return array
  190. */
  191. public function getStyleArray($array)
  192. {
  193. return array('fill' => $array);
  194. }
  195. /**
  196. * Apply styles from array
  197. *
  198. * <code>
  199. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  200. * array(
  201. * 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  202. * 'rotation' => 0,
  203. * 'startcolor' => array(
  204. * 'rgb' => '000000'
  205. * ),
  206. * 'endcolor' => array(
  207. * 'argb' => 'FFFFFFFF'
  208. * )
  209. * )
  210. * );
  211. * </code>
  212. *
  213. * @param array $pStyles Array containing style information
  214. * @throws Exception
  215. * @return PHPExcel_Style_Fill
  216. */
  217. public function applyFromArray($pStyles = null) {
  218. if (is_array($pStyles)) {
  219. if ($this->_isSupervisor) {
  220. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  221. } else {
  222. if (array_key_exists('type', $pStyles)) {
  223. $this->setFillType($pStyles['type']);
  224. }
  225. if (array_key_exists('rotation', $pStyles)) {
  226. $this->setRotation($pStyles['rotation']);
  227. }
  228. if (array_key_exists('startcolor', $pStyles)) {
  229. $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  230. }
  231. if (array_key_exists('endcolor', $pStyles)) {
  232. $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  233. }
  234. if (array_key_exists('color', $pStyles)) {
  235. $this->getStartColor()->applyFromArray($pStyles['color']);
  236. }
  237. }
  238. } else {
  239. throw new Exception("Invalid style array passed.");
  240. }
  241. return $this;
  242. }
  243. /**
  244. * Get Fill Type
  245. *
  246. * @return string
  247. */
  248. public function getFillType() {
  249. if ($this->_isSupervisor) {
  250. return $this->getSharedComponent()->getFillType();
  251. }
  252. return $this->_fillType;
  253. }
  254. /**
  255. * Set Fill Type
  256. *
  257. * @param string $pValue PHPExcel_Style_Fill fill type
  258. * @return PHPExcel_Style_Fill
  259. */
  260. public function setFillType($pValue = PHPExcel_Style_Fill::FILL_NONE) {
  261. if ($this->_isSupervisor) {
  262. $styleArray = $this->getStyleArray(array('type' => $pValue));
  263. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  264. } else {
  265. $this->_fillType = $pValue;
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Get Rotation
  271. *
  272. * @return double
  273. */
  274. public function getRotation() {
  275. if ($this->_isSupervisor) {
  276. return $this->getSharedComponent()->getRotation();
  277. }
  278. return $this->_rotation;
  279. }
  280. /**
  281. * Set Rotation
  282. *
  283. * @param double $pValue
  284. * @return PHPExcel_Style_Fill
  285. */
  286. public function setRotation($pValue = 0) {
  287. if ($this->_isSupervisor) {
  288. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  289. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  290. } else {
  291. $this->_rotation = $pValue;
  292. }
  293. return $this;
  294. }
  295. /**
  296. * Get Start Color
  297. *
  298. * @return PHPExcel_Style_Color
  299. */
  300. public function getStartColor() {
  301. return $this->_startColor;
  302. }
  303. /**
  304. * Set Start Color
  305. *
  306. * @param PHPExcel_Style_Color $pValue
  307. * @throws Exception
  308. * @return PHPExcel_Style_Fill
  309. */
  310. public function setStartColor(PHPExcel_Style_Color $pValue = null) {
  311. // make sure parameter is a real color and not a supervisor
  312. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  313. if ($this->_isSupervisor) {
  314. $styleArray = $this->getStartColor()->getStyleArray(array('argb' => $color->getARGB()));
  315. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  316. } else {
  317. $this->_startColor = $color;
  318. }
  319. return $this;
  320. }
  321. /**
  322. * Get End Color
  323. *
  324. * @return PHPExcel_Style_Color
  325. */
  326. public function getEndColor() {
  327. return $this->_endColor;
  328. }
  329. /**
  330. * Set End Color
  331. *
  332. * @param PHPExcel_Style_Color $pValue
  333. * @throws Exception
  334. * @return PHPExcel_Style_Fill
  335. */
  336. public function setEndColor(PHPExcel_Style_Color $pValue = null) {
  337. // make sure parameter is a real color and not a supervisor
  338. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  339. if ($this->_isSupervisor) {
  340. $styleArray = $this->getEndColor()->getStyleArray(array('argb' => $color->getARGB()));
  341. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  342. } else {
  343. $this->_endColor = $color;
  344. }
  345. return $this;
  346. }
  347. /**
  348. * Get hash code
  349. *
  350. * @return string Hash code
  351. */
  352. public function getHashCode() {
  353. if ($this->_isSupervisor) {
  354. return $this->getSharedComponent()->getHashCode();
  355. }
  356. return md5(
  357. $this->getFillType()
  358. . $this->getRotation()
  359. . $this->getStartColor()->getHashCode()
  360. . $this->getEndColor()->getHashCode()
  361. . __CLASS__
  362. );
  363. }
  364. /**
  365. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  366. */
  367. public function __clone() {
  368. $vars = get_object_vars($this);
  369. foreach ($vars as $key => $value) {
  370. if ((is_object($value)) && ($key != '_parent')) {
  371. $this->$key = clone $value;
  372. } else {
  373. $this->$key = $value;
  374. }
  375. }
  376. }
  377. }