Fill.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.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. /** @var bool */
  60. private $colorChanged = false;
  61. /**
  62. * Create a new Fill.
  63. *
  64. * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  65. * Leave this value at default unless you understand exactly what
  66. * its ramifications are
  67. * @param bool $isConditional Flag indicating if this is a conditional style or not
  68. * Leave this value at default unless you understand exactly what
  69. * its ramifications are
  70. */
  71. public function __construct($isSupervisor = false, $isConditional = false)
  72. {
  73. // Supervisor?
  74. parent::__construct($isSupervisor);
  75. // Initialise values
  76. if ($isConditional) {
  77. $this->fillType = null;
  78. }
  79. $this->startColor = new Color(Color::COLOR_WHITE, $isSupervisor, $isConditional);
  80. $this->endColor = new Color(Color::COLOR_BLACK, $isSupervisor, $isConditional);
  81. // bind parent if we are a supervisor
  82. if ($isSupervisor) {
  83. $this->startColor->bindParent($this, 'startColor');
  84. $this->endColor->bindParent($this, 'endColor');
  85. }
  86. }
  87. /**
  88. * Get the shared style component for the currently active cell in currently active sheet.
  89. * Only used for style supervisor.
  90. *
  91. * @return Fill
  92. */
  93. public function getSharedComponent()
  94. {
  95. /** @var Style */
  96. $parent = $this->parent;
  97. return $parent->getSharedComponent()->getFill();
  98. }
  99. /**
  100. * Build style array from subcomponents.
  101. *
  102. * @param array $array
  103. *
  104. * @return array
  105. */
  106. public function getStyleArray($array)
  107. {
  108. return ['fill' => $array];
  109. }
  110. /**
  111. * Apply styles from array.
  112. *
  113. * <code>
  114. * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  115. * [
  116. * 'fillType' => Fill::FILL_GRADIENT_LINEAR,
  117. * 'rotation' => 0.0,
  118. * 'startColor' => [
  119. * 'rgb' => '000000'
  120. * ],
  121. * 'endColor' => [
  122. * 'argb' => 'FFFFFFFF'
  123. * ]
  124. * ]
  125. * );
  126. * </code>
  127. *
  128. * @param array $styleArray Array containing style information
  129. *
  130. * @return $this
  131. */
  132. public function applyFromArray(array $styleArray)
  133. {
  134. if ($this->isSupervisor) {
  135. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
  136. } else {
  137. if (isset($styleArray['fillType'])) {
  138. $this->setFillType($styleArray['fillType']);
  139. }
  140. if (isset($styleArray['rotation'])) {
  141. $this->setRotation($styleArray['rotation']);
  142. }
  143. if (isset($styleArray['startColor'])) {
  144. $this->getStartColor()->applyFromArray($styleArray['startColor']);
  145. }
  146. if (isset($styleArray['endColor'])) {
  147. $this->getEndColor()->applyFromArray($styleArray['endColor']);
  148. }
  149. if (isset($styleArray['color'])) {
  150. $this->getStartColor()->applyFromArray($styleArray['color']);
  151. $this->getEndColor()->applyFromArray($styleArray['color']);
  152. }
  153. }
  154. return $this;
  155. }
  156. /**
  157. * Get Fill Type.
  158. *
  159. * @return null|string
  160. */
  161. public function getFillType()
  162. {
  163. if ($this->isSupervisor) {
  164. return $this->getSharedComponent()->getFillType();
  165. }
  166. return $this->fillType;
  167. }
  168. /**
  169. * Set Fill Type.
  170. *
  171. * @param string $fillType Fill type, see self::FILL_*
  172. *
  173. * @return $this
  174. */
  175. public function setFillType($fillType)
  176. {
  177. if ($this->isSupervisor) {
  178. $styleArray = $this->getStyleArray(['fillType' => $fillType]);
  179. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  180. } else {
  181. $this->fillType = $fillType;
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Get Rotation.
  187. *
  188. * @return float
  189. */
  190. public function getRotation()
  191. {
  192. if ($this->isSupervisor) {
  193. return $this->getSharedComponent()->getRotation();
  194. }
  195. return $this->rotation;
  196. }
  197. /**
  198. * Set Rotation.
  199. *
  200. * @param float $angleInDegrees
  201. *
  202. * @return $this
  203. */
  204. public function setRotation($angleInDegrees)
  205. {
  206. if ($this->isSupervisor) {
  207. $styleArray = $this->getStyleArray(['rotation' => $angleInDegrees]);
  208. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  209. } else {
  210. $this->rotation = $angleInDegrees;
  211. }
  212. return $this;
  213. }
  214. /**
  215. * Get Start Color.
  216. *
  217. * @return Color
  218. */
  219. public function getStartColor()
  220. {
  221. return $this->startColor;
  222. }
  223. /**
  224. * Set Start Color.
  225. *
  226. * @return $this
  227. */
  228. public function setStartColor(Color $color)
  229. {
  230. $this->colorChanged = true;
  231. // make sure parameter is a real color and not a supervisor
  232. $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
  233. if ($this->isSupervisor) {
  234. $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
  235. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  236. } else {
  237. $this->startColor = $color;
  238. }
  239. return $this;
  240. }
  241. /**
  242. * Get End Color.
  243. *
  244. * @return Color
  245. */
  246. public function getEndColor()
  247. {
  248. return $this->endColor;
  249. }
  250. /**
  251. * Set End Color.
  252. *
  253. * @return $this
  254. */
  255. public function setEndColor(Color $color)
  256. {
  257. $this->colorChanged = true;
  258. // make sure parameter is a real color and not a supervisor
  259. $color = $color->getIsSupervisor() ? $color->getSharedComponent() : $color;
  260. if ($this->isSupervisor) {
  261. $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
  262. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  263. } else {
  264. $this->endColor = $color;
  265. }
  266. return $this;
  267. }
  268. public function getColorsChanged(): bool
  269. {
  270. if ($this->isSupervisor) {
  271. $changed = $this->getSharedComponent()->colorChanged;
  272. } else {
  273. $changed = $this->colorChanged;
  274. }
  275. return $changed || $this->startColor->getHasChanged() || $this->endColor->getHasChanged();
  276. }
  277. /**
  278. * Get hash code.
  279. *
  280. * @return string Hash code
  281. */
  282. public function getHashCode()
  283. {
  284. if ($this->isSupervisor) {
  285. return $this->getSharedComponent()->getHashCode();
  286. }
  287. // Note that we don't care about colours for fill type NONE, but could have duplicate NONEs with
  288. // different hashes if we don't explicitly prevent this
  289. return md5(
  290. $this->getFillType() .
  291. $this->getRotation() .
  292. ($this->getFillType() !== self::FILL_NONE ? $this->getStartColor()->getHashCode() : '') .
  293. ($this->getFillType() !== self::FILL_NONE ? $this->getEndColor()->getHashCode() : '') .
  294. ((string) $this->getColorsChanged()) .
  295. __CLASS__
  296. );
  297. }
  298. protected function exportArray1(): array
  299. {
  300. $exportedArray = [];
  301. $this->exportArray2($exportedArray, 'fillType', $this->getFillType());
  302. $this->exportArray2($exportedArray, 'rotation', $this->getRotation());
  303. if ($this->getColorsChanged()) {
  304. $this->exportArray2($exportedArray, 'endColor', $this->getEndColor());
  305. $this->exportArray2($exportedArray, 'startColor', $this->getStartColor());
  306. }
  307. return $exportedArray;
  308. }
  309. }