Fill.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Style;
  3. class Fill extends Supervisor
  4. {
  5. // Fill types
  6. const FILL_NONE = 'none';
  7. const FILL_SOLID = 'solid';
  8. const FILL_GRADIENT_LINEAR = 'linear';
  9. const FILL_GRADIENT_PATH = 'path';
  10. const FILL_PATTERN_DARKDOWN = 'darkDown';
  11. const FILL_PATTERN_DARKGRAY = 'darkGray';
  12. const FILL_PATTERN_DARKGRID = 'darkGrid';
  13. const FILL_PATTERN_DARKHORIZONTAL = 'darkHorizontal';
  14. const FILL_PATTERN_DARKTRELLIS = 'darkTrellis';
  15. const FILL_PATTERN_DARKUP = 'darkUp';
  16. const FILL_PATTERN_DARKVERTICAL = 'darkVertical';
  17. const FILL_PATTERN_GRAY0625 = 'gray0625';
  18. const FILL_PATTERN_GRAY125 = 'gray125';
  19. const FILL_PATTERN_LIGHTDOWN = 'lightDown';
  20. const FILL_PATTERN_LIGHTGRAY = 'lightGray';
  21. const FILL_PATTERN_LIGHTGRID = 'lightGrid';
  22. const FILL_PATTERN_LIGHTHORIZONTAL = 'lightHorizontal';
  23. const FILL_PATTERN_LIGHTTRELLIS = 'lightTrellis';
  24. const FILL_PATTERN_LIGHTUP = 'lightUp';
  25. const FILL_PATTERN_LIGHTVERTICAL = 'lightVertical';
  26. const FILL_PATTERN_MEDIUMGRAY = 'mediumGray';
  27. /**
  28. * @var null|int
  29. */
  30. public $startcolorIndex;
  31. /**
  32. * @var null|int
  33. */
  34. public $endcolorIndex;
  35. /**
  36. * Fill type.
  37. *
  38. * @var null|string
  39. */
  40. protected $fillType = self::FILL_NONE;
  41. /**
  42. * Rotation.
  43. *
  44. * @var float
  45. */
  46. protected $rotation = 0;
  47. /**
  48. * Start color.
  49. *
  50. * @var Color
  51. */
  52. protected $startColor;
  53. /**
  54. * End color.
  55. *
  56. * @var Color
  57. */
  58. protected $endColor;
  59. /**
  60. * Create a new Fill.
  61. *
  62. * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  63. * Leave this value at default unless you understand exactly what
  64. * its ramifications are
  65. * @param bool $isConditional Flag indicating if this is a conditional style or not
  66. * Leave this value at default unless you understand exactly what
  67. * its ramifications are
  68. */
  69. public function __construct($isSupervisor = false, $isConditional = false)
  70. {
  71. // Supervisor?
  72. parent::__construct($isSupervisor);
  73. // Initialise values
  74. if ($isConditional) {
  75. $this->fillType = null;
  76. }
  77. $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
  78. $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
  79. // bind parent if we are a supervisor
  80. if ($isSupervisor) {
  81. $this->startColor->bindParent($this, 'startColor');
  82. $this->endColor->bindParent($this, 'endColor');
  83. }
  84. }
  85. /**
  86. * Get the shared style component for the currently active cell in currently active sheet.
  87. * Only used for style supervisor.
  88. *
  89. * @return Fill
  90. */
  91. public function getSharedComponent()
  92. {
  93. return $this->parent->getSharedComponent()->getFill();
  94. }
  95. /**
  96. * Build style array from subcomponents.
  97. *
  98. * @param array $array
  99. *
  100. * @return array
  101. */
  102. public function getStyleArray($array)
  103. {
  104. return ['fill' => $array];
  105. }
  106. /**
  107. * Apply styles from array.
  108. *
  109. * <code>
  110. * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  111. * [
  112. * 'fillType' => Fill::FILL_GRADIENT_LINEAR,
  113. * 'rotation' => 0,
  114. * 'startColor' => [
  115. * 'rgb' => '000000'
  116. * ],
  117. * 'endColor' => [
  118. * 'argb' => 'FFFFFFFF'
  119. * ]
  120. * ]
  121. * );
  122. * </code>
  123. *
  124. * @param array $styleArray Array containing style information
  125. *
  126. * @return $this
  127. */
  128. public function applyFromArray(array $styleArray)
  129. {
  130. if ($this->isSupervisor) {
  131. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
  132. } else {
  133. if (isset($styleArray['fillType'])) {
  134. $this->setFillType($styleArray['fillType']);
  135. }
  136. if (isset($styleArray['rotation'])) {
  137. $this->setRotation($styleArray['rotation']);
  138. }
  139. if (isset($styleArray['startColor'])) {
  140. $this->getStartColor()->applyFromArray($styleArray['startColor']);
  141. }
  142. if (isset($styleArray['endColor'])) {
  143. $this->getEndColor()->applyFromArray($styleArray['endColor']);
  144. }
  145. if (isset($styleArray['color'])) {
  146. $this->getStartColor()->applyFromArray($styleArray['color']);
  147. $this->getEndColor()->applyFromArray($styleArray['color']);
  148. }
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Get Fill Type.
  154. *
  155. * @return null|string
  156. */
  157. public function getFillType()
  158. {
  159. if ($this->isSupervisor) {
  160. return $this->getSharedComponent()->getFillType();
  161. }
  162. return $this->fillType;
  163. }
  164. /**
  165. * Set Fill Type.
  166. *
  167. * @param string $fillType Fill type, see self::FILL_*
  168. *
  169. * @return $this
  170. */
  171. public function setFillType($fillType)
  172. {
  173. if ($this->isSupervisor) {
  174. $styleArray = $this->getStyleArray(['fillType' => $fillType]);
  175. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  176. } else {
  177. $this->fillType = $fillType;
  178. }
  179. return $this;
  180. }
  181. /**
  182. * Get Rotation.
  183. *
  184. * @return float
  185. */
  186. public function getRotation()
  187. {
  188. if ($this->isSupervisor) {
  189. return $this->getSharedComponent()->getRotation();
  190. }
  191. return $this->rotation;
  192. }
  193. /**
  194. * Set Rotation.
  195. *
  196. * @param float $angleInDegrees
  197. *
  198. * @return $this
  199. */
  200. public function setRotation($angleInDegrees)
  201. {
  202. if ($this->isSupervisor) {
  203. $styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]);
  204. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  205. } else {
  206. $this->rotation = $angleInDegrees;
  207. }
  208. return $this;
  209. }
  210. /**
  211. * Get Start Color.
  212. *
  213. * @return Color
  214. */
  215. public function getStartColor()
  216. {
  217. return $this->startColor;
  218. }
  219. /**
  220. * Set Start Color.
  221. *
  222. * @return $this
  223. */
  224. public function setStartColor(Color $color)
  225. {
  226. // make sure parameter is a real color and not a supervisor
  227. $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
  228. if ($this->isSupervisor) {
  229. $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
  230. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  231. } else {
  232. $this->startColor = $color;
  233. }
  234. return $this;
  235. }
  236. /**
  237. * Get End Color.
  238. *
  239. * @return Color
  240. */
  241. public function getEndColor()
  242. {
  243. return $this->endColor;
  244. }
  245. /**
  246. * Set End Color.
  247. *
  248. * @return $this
  249. */
  250. public function setEndColor(Color $color)
  251. {
  252. // make sure parameter is a real color and not a supervisor
  253. $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
  254. if ($this->isSupervisor) {
  255. $styleArray = $this->getEndColor()->getStyleArray(['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. // Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
  273. // different hashes if we don't explicitly prevent this
  274. return md5(
  275. $this->getFillType() .
  276. $this->getRotation() .
  277. ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') .
  278. ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') .
  279. __CLASS__
  280. );
  281. }
  282. protected function exportArray1(): array
  283. {
  284. $exportedArray = [];
  285. $this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
  286. $this->exportArray2($exportedArray, 'fillType', $this->getFillType());
  287. $this->exportArray2($exportedArray, 'rotation', $this->getRotation());
  288. $this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
  289. return $exportedArray;
  290. }
  291. }