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

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

#
PHP | 256 lines | 105 code | 21 blank | 130 comment | 11 complexity | 27ae2644863c47a9fff8961472827389 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/lgpl.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. * Create a new PHPExcel_Style_Fill
  88. */
  89. public function __construct()
  90. {
  91. // Initialise values
  92. $this->_fillType = PHPExcel_Style_Fill::FILL_NONE;
  93. $this->_rotation = 0;
  94. $this->_startColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE);
  95. $this->_endColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK);
  96. }
  97. /**
  98. * Apply styles from array
  99. *
  100. * <code>
  101. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  102. * array(
  103. * 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  104. * 'rotation' => 0,
  105. * 'startcolor' => array(
  106. * 'rgb' => '000000'
  107. * ),
  108. * 'endcolor' => array(
  109. * 'argb' => 'FFFFFFFF'
  110. * )
  111. * )
  112. * );
  113. * </code>
  114. *
  115. * @param array $pStyles Array containing style information
  116. * @throws Exception
  117. */
  118. public function applyFromArray($pStyles = null) {
  119. if (is_array($pStyles)) {
  120. if (array_key_exists('type', $pStyles)) {
  121. $this->setFillType($pStyles['type']);
  122. }
  123. if (array_key_exists('rotation', $pStyles)) {
  124. $this->setRotation($pStyles['rotation']);
  125. }
  126. if (array_key_exists('startcolor', $pStyles)) {
  127. $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  128. }
  129. if (array_key_exists('endcolor', $pStyles)) {
  130. $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  131. }
  132. if (array_key_exists('color', $pStyles)) {
  133. $this->getStartColor()->applyFromArray($pStyles['color']);
  134. }
  135. } else {
  136. throw new Exception("Invalid style array passed.");
  137. }
  138. }
  139. /**
  140. * Get Fill Type
  141. *
  142. * @return string
  143. */
  144. public function getFillType() {
  145. if ($this->_fillType == '') {
  146. $this->_fillType = self::FILL_NONE;
  147. }
  148. return $this->_fillType;
  149. }
  150. /**
  151. * Set Fill Type
  152. *
  153. * @param string $pValue PHPExcel_Style_Fill fill type
  154. */
  155. public function setFillType($pValue = PHPExcel_Style_Fill::FILL_NONE) {
  156. $this->_fillType = $pValue;
  157. }
  158. /**
  159. * Get Rotation
  160. *
  161. * @return double
  162. */
  163. public function getRotation() {
  164. return $this->_rotation;
  165. }
  166. /**
  167. * Set Rotation
  168. *
  169. * @param double $pValue
  170. */
  171. public function setRotation($pValue = 0) {
  172. $this->_rotation = $pValue;
  173. }
  174. /**
  175. * Get Start Color
  176. *
  177. * @return PHPExcel_Style_Color
  178. */
  179. public function getStartColor() {
  180. return $this->_startColor;
  181. }
  182. /**
  183. * Set Start Color
  184. *
  185. * @param PHPExcel_Style_Color $pValue
  186. * @throws Exception
  187. */
  188. public function setStartColor(PHPExcel_Style_Color $pValue = null) {
  189. $this->_startColor = $pValue;
  190. }
  191. /**
  192. * Get End Color
  193. *
  194. * @return PHPExcel_Style_Color
  195. */
  196. public function getEndColor() {
  197. return $this->_endColor;
  198. }
  199. /**
  200. * Set End Color
  201. *
  202. * @param PHPExcel_Style_Color $pValue
  203. * @throws Exception
  204. */
  205. public function setEndColor(PHPExcel_Style_Color $pValue = null) {
  206. $this->_endColor = $pValue;
  207. }
  208. /**
  209. * Get hash code
  210. *
  211. * @return string Hash code
  212. */
  213. public function getHashCode() {
  214. return md5(
  215. $this->_fillType
  216. . $this->_rotation
  217. . $this->_startColor->getHashCode()
  218. . $this->_endColor->getHashCode()
  219. . __CLASS__
  220. );
  221. }
  222. /**
  223. * Implement PHP __clone to create a deep clone, not just a shallow copy.
  224. */
  225. public function __clone() {
  226. $vars = get_object_vars($this);
  227. foreach ($vars as $key => $value) {
  228. if (is_object($value)) {
  229. $this->$key = clone $value;
  230. } else {
  231. $this->$key = $value;
  232. }
  233. }
  234. }
  235. }