Fill.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. * <code>
  110. * $spreadsheet->getActiveSheet()->getStyle('B2')->getFill()->applyFromArray(
  111. * array(
  112. * 'fillType' => Fill::FILL_GRADIENT_LINEAR,
  113. * 'rotation' => 0,
  114. * 'startColor' => array(
  115. * 'rgb' => '000000'
  116. * ),
  117. * 'endColor' => array(
  118. * 'argb' => 'FFFFFFFF'
  119. * )
  120. * )
  121. * );
  122. * </code>.
  123. *
  124. * @param array $pStyles Array containing style information
  125. *
  126. * @throws PhpSpreadsheetException
  127. *
  128. * @return Fill
  129. */
  130. public function applyFromArray(array $pStyles)
  131. {
  132. if ($this->isSupervisor) {
  133. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  134. } else {
  135. if (isset($pStyles['fillType'])) {
  136. $this->setFillType($pStyles['fillType']);
  137. }
  138. if (isset($pStyles['rotation'])) {
  139. $this->setRotation($pStyles['rotation']);
  140. }
  141. if (isset($pStyles['startColor'])) {
  142. $this->getStartColor()->applyFromArray($pStyles['startColor']);
  143. }
  144. if (isset($pStyles['endColor'])) {
  145. $this->getEndColor()->applyFromArray($pStyles['endColor']);
  146. }
  147. if (isset($pStyles['color'])) {
  148. $this->getStartColor()->applyFromArray($pStyles['color']);
  149. $this->getEndColor()->applyFromArray($pStyles['color']);
  150. }
  151. }
  152. return $this;
  153. }
  154. /**
  155. * Get Fill Type.
  156. *
  157. * @return string
  158. */
  159. public function getFillType()
  160. {
  161. if ($this->isSupervisor) {
  162. return $this->getSharedComponent()->getFillType();
  163. }
  164. return $this->fillType;
  165. }
  166. /**
  167. * Set Fill Type.
  168. *
  169. * @param string $pValue Fill type, see self::FILL_*
  170. *
  171. * @return Fill
  172. */
  173. public function setFillType($pValue)
  174. {
  175. if ($this->isSupervisor) {
  176. $styleArray = $this->getStyleArray(['fillType' => $pValue]);
  177. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  178. } else {
  179. $this->fillType = $pValue;
  180. }
  181. return $this;
  182. }
  183. /**
  184. * Get Rotation.
  185. *
  186. * @return float
  187. */
  188. public function getRotation()
  189. {
  190. if ($this->isSupervisor) {
  191. return $this->getSharedComponent()->getRotation();
  192. }
  193. return $this->rotation;
  194. }
  195. /**
  196. * Set Rotation.
  197. *
  198. * @param float $pValue
  199. *
  200. * @return Fill
  201. */
  202. public function setRotation($pValue)
  203. {
  204. if ($this->isSupervisor) {
  205. $styleArray = $this->getStyleArray(['rotation' => $pValue]);
  206. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  207. } else {
  208. $this->rotation = $pValue;
  209. }
  210. return $this;
  211. }
  212. /**
  213. * Get Start Color.
  214. *
  215. * @return Color
  216. */
  217. public function getStartColor()
  218. {
  219. return $this->startColor;
  220. }
  221. /**
  222. * Set Start Color.
  223. *
  224. * @param Color $pValue
  225. *
  226. * @throws PhpSpreadsheetException
  227. *
  228. * @return Fill
  229. */
  230. public function setStartColor(Color $pValue)
  231. {
  232. // make sure parameter is a real color and not a supervisor
  233. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  234. if ($this->isSupervisor) {
  235. $styleArray = $this->getStartColor()->getStyleArray(['argb' => $color->getARGB()]);
  236. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  237. } else {
  238. $this->startColor = $color;
  239. }
  240. return $this;
  241. }
  242. /**
  243. * Get End Color.
  244. *
  245. * @return Color
  246. */
  247. public function getEndColor()
  248. {
  249. return $this->endColor;
  250. }
  251. /**
  252. * Set End Color.
  253. *
  254. * @param Color $pValue
  255. *
  256. * @throws PhpSpreadsheetException
  257. *
  258. * @return Fill
  259. */
  260. public function setEndColor(Color $pValue)
  261. {
  262. // make sure parameter is a real color and not a supervisor
  263. $color = $pValue->getIsSupervisor() ? $pValue->getSharedComponent() : $pValue;
  264. if ($this->isSupervisor) {
  265. $styleArray = $this->getEndColor()->getStyleArray(['argb' => $color->getARGB()]);
  266. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  267. } else {
  268. $this->endColor = $color;
  269. }
  270. return $this;
  271. }
  272. /**
  273. * Get hash code.
  274. *
  275. * @return string Hash code
  276. */
  277. public function getHashCode()
  278. {
  279. if ($this->isSupervisor) {
  280. return $this->getSharedComponent()->getHashCode();
  281. }
  282. return md5(
  283. $this->getFillType() .
  284. $this->getRotation() .
  285. $this->getStartColor()->getHashCode() .
  286. $this->getEndColor()->getHashCode() .
  287. __CLASS__
  288. );
  289. }
  290. }