Fill.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Style_Fill extends PHPExcel_Style_Supervisor implements PHPExcel_IComparable
  8. {
  9. /* Fill types */
  10. const FILL_NONE = 'none';
  11. const FILL_SOLID = 'solid';
  12. const FILL_GRADIENT_LINEAR = 'linear';
  13. const FILL_GRADIENT_PATH = 'path';
  14. const FILL_PATTERN_DARKDOWN = 'darkDown';
  15. const FILL_PATTERN_DARKGRAY = 'darkGray';
  16. const FILL_PATTERN_DARKGRID = 'darkGrid';
  17. const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
  18. const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
  19. const FILL_PATTERN_DARKUP = 'darkUp';
  20. const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
  21. const FILL_PATTERN_GRAY0625 = 'gray0625';
  22. const FILL_PATTERN_GRAY125 = 'gray125';
  23. const FILL_PATTERN_LIGHTDOWN = 'lightDown';
  24. const FILL_PATTERN_LIGHTGRAY = 'lightGray';
  25. const FILL_PATTERN_LIGHTGRID = 'lightGrid';
  26. const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
  27. const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
  28. const FILL_PATTERN_LIGHTUP = 'lightUp';
  29. const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
  30. const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
  31. /**
  32. * Fill type
  33. *
  34. * @var string
  35. */
  36. protected $fillType = PHPExcel_Style_Fill::FILL_NONE;
  37. /**
  38. * Rotation
  39. *
  40. * @var double
  41. */
  42. protected $rotation = 0;
  43. /**
  44. * Start color
  45. *
  46. * @var PHPExcel_Style_Color
  47. */
  48. protected $startColor;
  49. /**
  50. * End color
  51. *
  52. * @var PHPExcel_Style_Color
  53. */
  54. protected $endColor;
  55. /**
  56. * Create a new PHPExcel_Style_Fill
  57. *
  58. * @param boolean $isSupervisor Flag indicating if this is a supervisor or not
  59. * Leave this value at default unless you understand exactly what
  60. * its ramifications are
  61. * @param boolean $isConditional Flag indicating if this is a conditional style or not
  62. * Leave this value at default unless you understand exactly what
  63. * its ramifications are
  64. */
  65. public function __construct($isSupervisor = false, $isConditional = false)
  66. {
  67. // Supervisor?
  68. parent::__construct($isSupervisor);
  69. // Initialise values
  70. if ($isConditional) {
  71. $this->fillType = null;
  72. }
  73. $this->startColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_WHITE, $isSupervisor, $isConditional);
  74. $this->endColor = new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_BLACK, $isSupervisor, $isConditional);
  75. // bind parent if we are a supervisor
  76. if ($isSupervisor) {
  77. $this->startColor->bindParent($this, 'startColor');
  78. $this->endColor->bindParent($this, 'endColor');
  79. }
  80. }
  81. /**
  82. * Get the shared style component for the currently active cell in currently active sheet.
  83. * Only used for style supervisor
  84. *
  85. * @return PHPExcel_Style_Fill
  86. */
  87. public function getSharedComponent()
  88. {
  89. return $this->parent->getSharedComponent()->getFill();
  90. }
  91. /**
  92. * Build style array from subcomponents
  93. *
  94. * @param array $array
  95. * @return array
  96. */
  97. public function getStyleArray($array)
  98. {
  99. return array('fill' => $array);
  100. }
  101. /**
  102. * Apply styles from array
  103. *
  104. * <code>
  105. * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  106. * array(
  107. * 'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
  108. * 'rotation' => 0,
  109. * 'startcolor' => array(
  110. * 'rgb' => '000000'
  111. * ),
  112. * 'endcolor' => array(
  113. * 'argb' => 'FFFFFFFF'
  114. * )
  115. * )
  116. * );
  117. * </code>
  118. *
  119. * @param array $pStyles Array containing style information
  120. * @throws PHPExcel_Exception
  121. * @return PHPExcel_Style_Fill
  122. */
  123. public function applyFromArray($pStyles = null)
  124. {
  125. if (is_array($pStyles)) {
  126. if ($this->isSupervisor) {
  127. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  128. } else {
  129. if (array_key_exists('type', $pStyles)) {
  130. $this->setFillType($pStyles['type']);
  131. }
  132. if (array_key_exists('rotation', $pStyles)) {
  133. $this->setRotation($pStyles['rotation']);
  134. }
  135. if (array_key_exists('startcolor', $pStyles)) {
  136. $this->getStartColor()->applyFromArray($pStyles['startcolor']);
  137. }
  138. if (array_key_exists('endcolor', $pStyles)) {
  139. $this->getEndColor()->applyFromArray($pStyles['endcolor']);
  140. }
  141. if (array_key_exists('color', $pStyles)) {
  142. $this->getStartColor()->applyFromArray($pStyles['color']);
  143. }
  144. }
  145. } else {
  146. throw new PHPExcel_Exception("Invalid style array passed.");
  147. }
  148. return $this;
  149. }
  150. /**
  151. * Get Fill Type
  152. *
  153. * @return string
  154. */
  155. public function getFillType()
  156. {
  157. if ($this->isSupervisor) {
  158. return $this->getSharedComponent()->getFillType();
  159. }
  160. return $this->fillType;
  161. }
  162. /**
  163. * Set Fill Type
  164. *
  165. * @param string $pValue PHPExcel_Style_Fill fill type
  166. * @return PHPExcel_Style_Fill
  167. */
  168. public function setFillType($pValue = PHPExcel_Style_Fill::FILL_NONE)
  169. {
  170. if ($this->isSupervisor) {
  171. $styleArray = $this->getStyleArray(array('type' => $pValue));
  172. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  173. } else {
  174. $this->fillType = $pValue;
  175. }
  176. return $this;
  177. }
  178. /**
  179. * Get Rotation
  180. *
  181. * @return double
  182. */
  183. public function getRotation()
  184. {
  185. if ($this->isSupervisor) {
  186. return $this->getSharedComponent()->getRotation();
  187. }
  188. return $this->rotation;
  189. }
  190. /**
  191. * Set Rotation
  192. *
  193. * @param double $pValue
  194. * @return PHPExcel_Style_Fill
  195. */
  196. public function setRotation($pValue = 0)
  197. {
  198. if ($this->isSupervisor) {
  199. $styleArray = $this->getStyleArray(array('rotation' => $pValue));
  200. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  201. } else {
  202. $this->rotation = $pValue;
  203. }
  204. return $this;
  205. }
  206. /**
  207. * Get Start Color
  208. *
  209. * @return PHPExcel_Style_Color
  210. */
  211. public function getStartColor()
  212. {
  213. return $this->startColor;
  214. }
  215. /**
  216. * Set Start Color
  217. *
  218. * @param PHPExcel_Style_Color $pValue
  219. * @throws PHPExcel_Exception
  220. * @return PHPExcel_Style_Fill
  221. */
  222. public function setStartColor(PHPExcel_Style_Color $pValue = null)
  223. {
  224. // make sure parameter is a real color and not a supervisor
  225. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  226. if ($this->isSupervisor) {
  227. $styleArray = $this->getStartColor()->getStyleArray(array('argb' => $color->getARGB()));
  228. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  229. } else {
  230. $this->startColor = $color;
  231. }
  232. return $this;
  233. }
  234. /**
  235. * Get End Color
  236. *
  237. * @return PHPExcel_Style_Color
  238. */
  239. public function getEndColor()
  240. {
  241. return $this->endColor;
  242. }
  243. /**
  244. * Set End Color
  245. *
  246. * @param PHPExcel_Style_Color $pValue
  247. * @throws PHPExcel_Exception
  248. * @return PHPExcel_Style_Fill
  249. */
  250. public function setEndColor(PHPExcel_Style_Color $pValue = null)
  251. {
  252. // make sure parameter is a real color and not a supervisor
  253. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  254. if ($this->isSupervisor) {
  255. $styleArray = $this->getEndColor()->getStyleArray(array('argb' => $color->getARGB()));
  256. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  257. } else {
  258. $this->endColor = $color;
  259. }
  260. return $this;
  261. }
  262. /**
  263. * Get hash code
  264. *
  265. * @return string Hash code
  266. */
  267. public function getHashCode()
  268. {
  269. if ($this->isSupervisor) {
  270. return $this->getSharedComponent()->getHashCode();
  271. }
  272. return md5(
  273. $this->getFillType() .
  274. $this->getRotation() .
  275. $this->getStartColor()->getHashCode() .
  276. $this->getEndColor()->getHashCode() .
  277. __CLASS__
  278. );
  279. }
  280. }