PageRenderTime 35ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/v1.7.2/Classes/PHPExcel/Style/Fill.php

#
PHP | 416 lines | 182 code | 36 blank | 198 comment | 24 complexity | 5a9e40bd2e1a6269d5956acf88e231a8 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 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. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. }
  34. /** PHPExcel_Style_Color */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel/Style/Color.php';
  36. /** PHPExcel_IComparable */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/IComparable.php';
  38. /**
  39. * PHPExcel_Style_Fill
  40. *
  41. * @category PHPExcel
  42. * @package PHPExcel_Style
  43. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  44. */
  45. class PHPExcel_Style_Fill implements PHPExcel_IComparable
  46. {
  47. /* Fill types */
  48. const FILL_NONE = 'none';
  49. const FILL_SOLID = 'solid';
  50. const FILL_GRADIENT_LINEAR = 'linear';
  51. const FILL_GRADIENT_PATH = 'path';
  52. const FILL_PATTERN_DARKDOWN = 'darkDown';
  53. const FILL_PATTERN_DARKGRAY = 'darkGray';
  54. const FILL_PATTERN_DARKGRID = 'darkGrid';
  55. const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
  56. const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
  57. const FILL_PATTERN_DARKUP = 'darkUp';
  58. const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
  59. const FILL_PATTERN_GRAY0625 = 'gray0625';
  60. const FILL_PATTERN_GRAY125 = 'gray125';
  61. const FILL_PATTERN_LIGHTDOWN = 'lightDown';
  62. const FILL_PATTERN_LIGHTGRAY = 'lightGray';
  63. const FILL_PATTERN_LIGHTGRID = 'lightGrid';
  64. const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
  65. const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
  66. const FILL_PATTERN_LIGHTUP = 'lightUp';
  67. const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
  68. const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
  69. /**
  70. * Fill type
  71. *
  72. * @var string
  73. */
  74. private $_fillType;
  75. /**
  76. * Rotation
  77. *
  78. * @var double
  79. */
  80. private $_rotation;
  81. /**
  82. * Start color
  83. *
  84. * @var PHPExcel_Style_Color
  85. */
  86. private $_startColor;
  87. /**
  88. * End color
  89. *
  90. * @var PHPExcel_Style_Color
  91. */
  92. private $_endColor;
  93. /**
  94. * Parent Borders
  95. *
  96. * @var _parentPropertyName string
  97. */
  98. private $_parentPropertyName;
  99. /**
  100. * Supervisor?
  101. *
  102. * @var boolean
  103. */
  104. private $_isSupervisor;
  105. /**
  106. * Parent. Only used for supervisor
  107. *
  108. * @var PHPExcel_Style
  109. */
  110. private $_parent;
  111. /**
  112. * Create a new PHPExcel_Style_Fill
  113. */
  114. public function __construct($isSupervisor = false)
  115. {
  116. // Supervisor?
  117. $this->_isSupervisor = $isSupervisor;
  118. // Initialise values
  119. $this->_fillType = PHPExcel_Style_Fill::FILL_NONE;
  120. $this->_rotation = 0;
  121. $this->_startColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE, $isSupervisor);
  122. $this->_endColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor);
  123. // bind parent if we are a supervisor
  124. if ($isSupervisor) {
  125. $this->_startColor->bindParent($this, '_startColor');
  126. $this->_endColor->bindParent($this, '_endColor');
  127. }
  128. }
  129. /**
  130. * Bind parent. Only used for supervisor
  131. *
  132. * @param PHPExcel_Style $parent
  133. * @return PHPExcel_Style_Fill
  134. */
  135. public function bindParent($parent)
  136. {
  137. $this->_parent = $parent;
  138. return $this;
  139. }
  140. /**
  141. * Is this a supervisor or a real style component?
  142. *
  143. * @return boolean
  144. */
  145. public function getIsSupervisor()
  146. {
  147. return $this->_isSupervisor;
  148. }
  149. /**
  150. * Get the shared style component for the currently active cell in currently active sheet.
  151. * Only used for style supervisor
  152. *
  153. * @return PHPExcel_Style_Fill
  154. */
  155. public function getSharedComponent()
  156. {
  157. return $this->_parent->getSharedComponent()->getFill();
  158. }
  159. /**
  160. * Get the currently active sheet. Only used for supervisor
  161. *
  162. * @return PHPExcel_Worksheet
  163. */
  164. public function getActiveSheet()
  165. {
  166. return $this->_parent->getActiveSheet();
  167. }
  168. /**
  169. * Get the currently active cell coordinate in currently active sheet.
  170. * Only used for supervisor
  171. *
  172. * @return string E.g. 'A1'
  173. */
  174. public function getSelectedCells()
  175. {
  176. return $this->getActiveSheet()->getSelectedCells();
  177. }
  178. /**
  179. * Get the currently active cell coordinate in currently active sheet.
  180. * Only used for supervisor
  181. *
  182. * @return string E.g. 'A1'
  183. */
  184. public function getActiveCell()
  185. {
  186. return $this->getActiveSheet()->getActiveCell();
  187. }
  188. /**
  189. * Build style array from subcomponents
  190. *
  191. * @param array $array
  192. * @return array
  193. */
  194. public function getStyleArray($array)
  195. {
  196. return array('fill' => $array);
  197. }
  198. /**
  199. * Apply styles from array
  200. *
  201. * <code>
  202. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  203. * array(
  204. * 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  205. * 'rotation' => 0,
  206. * 'startcolor' => array(
  207. * 'rgb' => '000000'
  208. * ),
  209. * 'endcolor' => array(
  210. * 'argb' => 'FFFFFFFF'
  211. * )
  212. * )
  213. * );
  214. * </code>
  215. *
  216. * @param array $pStyles Array containing style information
  217. * @throws Exception
  218. * @return PHPExcel_Style_Fill
  219. */
  220. public function applyFromArray($pStyles = null) {
  221. if (is_array($pStyles)) {
  222. if ($this->_isSupervisor) {
  223. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  224. } else {
  225. if (array_key_exists('type', $pStyles)) {
  226. $this->setFillType($pStyles['type']);
  227. }
  228. if (array_key_exists('rotation', $pStyles)) {
  229. $this->setRotation($pStyles['rotation']);
  230. }
  231. if (array_key_exists('startcolor', $pStyles)) {
  232. $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  233. }
  234. if (array_key_exists('endcolor', $pStyles)) {
  235. $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  236. }
  237. if (array_key_exists('color', $pStyles)) {
  238. $this->getStartColor()->applyFromArray($pStyles['color']);
  239. }
  240. }
  241. } else {
  242. throw new Exception("Invalid style array passed.");
  243. }
  244. return $this;
  245. }
  246. /**
  247. * Get Fill Type
  248. *
  249. * @return string
  250. */
  251. public function getFillType() {
  252. if ($this->_isSupervisor) {
  253. return $this->getSharedComponent()->getFillType();
  254. }
  255. return $this->_fillType;
  256. }
  257. /**
  258. * Set Fill Type
  259. *
  260. * @param string $pValue PHPExcel_Style_Fill fill type
  261. * @return PHPExcel_Style_Fill
  262. */
  263. public function setFillType($pValue = PHPExcel_Style_Fill::FILL_NONE) {
  264. if ($this->_isSupervisor) {
  265. $styleArray = $this->getStyleArray(array('type' => $pValue));
  266. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  267. } else {
  268. $this->_fillType = $pValue;
  269. }
  270. return $this;
  271. }
  272. /**
  273. * Get Rotation
  274. *
  275. * @return double
  276. */
  277. public function getRotation() {
  278. if ($this->_isSupervisor) {
  279. return $this->getSharedComponent()->getRotation();
  280. }
  281. return $this->_rotation;
  282. }
  283. /**
  284. * Set Rotation
  285. *
  286. * @param double $pValue
  287. * @return PHPExcel_Style_Fill
  288. */
  289. public function setRotation($pValue = 0) {
  290. if ($this->_isSupervisor) {
  291. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  292. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  293. } else {
  294. $this->_rotation = $pValue;
  295. }
  296. return $this;
  297. }
  298. /**
  299. * Get Start Color
  300. *
  301. * @return PHPExcel_Style_Color
  302. */
  303. public function getStartColor() {
  304. return $this->_startColor;
  305. }
  306. /**
  307. * Set Start Color
  308. *
  309. * @param PHPExcel_Style_Color $pValue
  310. * @throws Exception
  311. * @return PHPExcel_Style_Fill
  312. */
  313. public function setStartColor(PHPExcel_Style_Color $pValue = null) {
  314. // make sure parameter is a real color and not a supervisor
  315. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  316. if ($this->_isSupervisor) {
  317. $styleArray = $this->getStartColor()->getStyleArray(array('argb' => $color->getARGB()));
  318. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  319. } else {
  320. $this->_startColor = $color;
  321. }
  322. return $this;
  323. }
  324. /**
  325. * Get End Color
  326. *
  327. * @return PHPExcel_Style_Color
  328. */
  329. public function getEndColor() {
  330. return $this->_endColor;
  331. }
  332. /**
  333. * Set End Color
  334. *
  335. * @param PHPExcel_Style_Color $pValue
  336. * @throws Exception
  337. * @return PHPExcel_Style_Fill
  338. */
  339. public function setEndColor(PHPExcel_Style_Color $pValue = null) {
  340. // make sure parameter is a real color and not a supervisor
  341. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  342. if ($this->_isSupervisor) {
  343. $styleArray = $this->getEndColor()->getStyleArray(array('argb' => $color->getARGB()));
  344. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  345. } else {
  346. $this->_endColor = $color;
  347. }
  348. return $this;
  349. }
  350. /**
  351. * Get hash code
  352. *
  353. * @return string Hash code
  354. */
  355. public function getHashCode() {
  356. if ($this->_isSupervisor) {
  357. return $this->getSharedComponent()->getHashCode();
  358. }
  359. return md5(
  360. $this->getFillType()
  361. . $this->getRotation()
  362. . $this->getStartColor()->getHashCode()
  363. . $this->getEndColor()->getHashCode()
  364. . __CLASS__
  365. );
  366. }
  367. /**
  368. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  369. */
  370. public function __clone() {
  371. $vars = get_object_vars($this);
  372. foreach ($vars as $key => $value) {
  373. if (is_object($value)) {
  374. $this->$key = clone $value;
  375. } else {
  376. $this->$key = $value;
  377. }
  378. }
  379. }
  380. }