PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
PHP | 331 lines | 130 code | 33 blank | 168 comment | 15 complexity | 1df6054c75548af423fc22fccfe08de4 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 - 2008 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 - 2008 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 - 2008 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 Style
  88. *
  89. * @var PHPExcel_Style
  90. */
  91. private $_parent;
  92. /**
  93. * Parent Borders
  94. *
  95. * @var _parentPropertyName string
  96. */
  97. private $_parentPropertyName;
  98. /**
  99. * Create a new PHPExcel_Style_Fill
  100. */
  101. public function __construct()
  102. {
  103. // Initialise values
  104. $this->_fillType = PHPExcel_Style_Fill::FILL_NONE;
  105. $this->_rotation = 0;
  106. $this->_startColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE);
  107. $this->_endColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  108. }
  109. /**
  110. * Property Prepare bind
  111. *
  112. * Configures this object for late binding as a property of a parent object
  113. *
  114. * @param $parent
  115. * @param $parentPropertyName
  116. */
  117. public function propertyPrepareBind($parent, $parentPropertyName)
  118. {
  119. // Initialize parent PHPExcel_Style for late binding. This relationship purposely ends immediately when this object
  120. // is bound to the PHPExcel_Style object pointed to so as to prevent circular references.
  121. $this->_parent = $parent;
  122. $this->_parentPropertyName = $parentPropertyName;
  123. }
  124. /**
  125. * Property Get Bound
  126. *
  127. * Returns the PHPExcel_Style_Fill that is actual bound to PHPExcel_Style
  128. *
  129. * @return PHPExcel_Style_Fill
  130. */
  131. private function propertyGetBound() {
  132. if(!isset($this->_parent))
  133. return $this; // I am bound
  134. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  135. return $this->_parent->getFill(); // Another one is bound
  136. return $this; // No one is bound yet
  137. }
  138. /**
  139. * Property Begin Bind
  140. *
  141. * If no PHPExcel_Style_Fill has been bound to PHPExcel_Style then bind this one. Return the actual bound one.
  142. *
  143. * @return PHPExcel_Style_Fill
  144. */
  145. private function propertyBeginBind() {
  146. if(!isset($this->_parent))
  147. return $this; // I am already bound
  148. if($this->_parent->propertyIsBound($this->_parentPropertyName))
  149. return $this->_parent->getFill(); // Another one is already bound
  150. $this->_parent->propertyCompleteBind($this, $this->_parentPropertyName); // Bind myself
  151. $this->_parent = null;
  152. return $this;
  153. }
  154. /**
  155. * Apply styles from array
  156. *
  157. * <code>
  158. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  159. * array(
  160. * 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  161. * 'rotation' => 0,
  162. * 'startcolor' => array(
  163. * 'rgb' => '000000'
  164. * ),
  165. * 'endcolor' => array(
  166. * 'argb' => 'FFFFFFFF'
  167. * )
  168. * )
  169. * );
  170. * </code>
  171. *
  172. * @param array $pStyles Array containing style information
  173. * @throws Exception
  174. */
  175. public function applyFromArray($pStyles = null) {
  176. if (is_array($pStyles)) {
  177. if (array_key_exists('type', $pStyles)) {
  178. $this->setFillType($pStyles['type']);
  179. }
  180. if (array_key_exists('rotation', $pStyles)) {
  181. $this->setRotation($pStyles['rotation']);
  182. }
  183. if (array_key_exists('startcolor', $pStyles)) {
  184. $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  185. }
  186. if (array_key_exists('endcolor', $pStyles)) {
  187. $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  188. }
  189. if (array_key_exists('color', $pStyles)) {
  190. $this->getStartColor()->applyFromArray($pStyles['color']);
  191. }
  192. } else {
  193. throw new Exception("Invalid style array passed.");
  194. }
  195. }
  196. /**
  197. * Get Fill Type
  198. *
  199. * @return string
  200. */
  201. public function getFillType() {
  202. $property = $this->propertyGetBound();
  203. if ($property->_fillType == '') {
  204. $property->_fillType = self::FILL_NONE;
  205. }
  206. return $property->_fillType;
  207. }
  208. /**
  209. * Set Fill Type
  210. *
  211. * @param string $pValue PHPExcel_Style_Fill fill type
  212. */
  213. public function setFillType($pValue = PHPExcel_Style_Fill::FILL_NONE) {
  214. $this->propertyBeginBind()->_fillType = $pValue;
  215. }
  216. /**
  217. * Get Rotation
  218. *
  219. * @return double
  220. */
  221. public function getRotation() {
  222. return $this->propertyGetBound()->_rotation;
  223. }
  224. /**
  225. * Set Rotation
  226. *
  227. * @param double $pValue
  228. */
  229. public function setRotation($pValue = 0) {
  230. $this->propertyBeginBind()->_rotation = $pValue;
  231. }
  232. /**
  233. * Get Start Color
  234. *
  235. * @return PHPExcel_Style_Color
  236. */
  237. public function getStartColor() {
  238. // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  239. // So bind as an assurance.
  240. return $this->propertyBeginBind()->_startColor;
  241. }
  242. /**
  243. * Set Start Color
  244. *
  245. * @param PHPExcel_Style_Color $pValue
  246. * @throws Exception
  247. */
  248. public function setStartColor(PHPExcel_Style_Color $pValue = null) {
  249. $this->propertyBeginBind()->_startColor = $pValue;
  250. }
  251. /**
  252. * Get End Color
  253. *
  254. * @return PHPExcel_Style_Color
  255. */
  256. public function getEndColor() {
  257. // It's a get but it may lead to a modified color which we won't detect but in which case we must bind.
  258. // So bind as an assurance.
  259. return $this->propertyBeginBind()->_endColor;
  260. }
  261. /**
  262. * Set End Color
  263. *
  264. * @param PHPExcel_Style_Color $pValue
  265. * @throws Exception
  266. */
  267. public function setEndColor(PHPExcel_Style_Color $pValue = null) {
  268. $this->propertyBeginBind()->_endColor = $pValue;
  269. }
  270. /**
  271. * Get hash code
  272. *
  273. * @return string Hash code
  274. */
  275. public function getHashCode() {
  276. $property = $this->propertyGetBound();
  277. return md5(
  278. $property->getFillType()
  279. . $property->getRotation()
  280. . $property->getStartColor()->getHashCode()
  281. . $property->getEndColor()->getHashCode()
  282. . __CLASS__
  283. );
  284. }
  285. /**
  286. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  287. */
  288. public function __clone() {
  289. $vars = get_object_vars($this);
  290. foreach ($vars as $key => $value) {
  291. if (is_object($value)) {
  292. $this->$key = clone $value;
  293. } else {
  294. $this->$key = $value;
  295. }
  296. }
  297. }
  298. }