PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/wi6857-memory/Classes/PHPExcel/Style/Fill.php

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