Fill.php 8.3 KB

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