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

/htdocs/includes/phpexcel/PHPExcel/Style/Fill.php

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