Color.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Style;
  3. use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
  4. class Color extends Supervisor
  5. {
  6. // Colors
  7. const COLOR_BLACK = 'FF000000';
  8. const COLOR_WHITE = 'FFFFFFFF';
  9. const COLOR_RED = 'FFFF0000';
  10. const COLOR_DARKRED = 'FF800000';
  11. const COLOR_BLUE = 'FF0000FF';
  12. const COLOR_DARKBLUE = 'FF000080';
  13. const COLOR_GREEN = 'FF00FF00';
  14. const COLOR_DARKGREEN = 'FF008000';
  15. const COLOR_YELLOW = 'FFFFFF00';
  16. const COLOR_DARKYELLOW = 'FF808000';
  17. /**
  18. * Indexed colors array.
  19. *
  20. * @var array
  21. */
  22. protected static $indexedColors;
  23. /**
  24. * ARGB - Alpha RGB.
  25. *
  26. * @var string
  27. */
  28. protected $argb;
  29. /**
  30. * Create a new Color.
  31. *
  32. * @param string $pARGB ARGB value for the colour
  33. * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  34. * Leave this value at default unless you understand exactly what
  35. * its ramifications are
  36. * @param bool $isConditional Flag indicating if this is a conditional style or not
  37. * Leave this value at default unless you understand exactly what
  38. * its ramifications are
  39. */
  40. public function __construct($pARGB = self::COLOR_BLACK, $isSupervisor = false, $isConditional = false)
  41. {
  42. // Supervisor?
  43. parent::__construct($isSupervisor);
  44. // Initialise values
  45. if (!$isConditional) {
  46. $this->argb = $pARGB;
  47. }
  48. }
  49. /**
  50. * Get the shared style component for the currently active cell in currently active sheet.
  51. * Only used for style supervisor.
  52. *
  53. * @return Color
  54. */
  55. public function getSharedComponent()
  56. {
  57. switch ($this->parentPropertyName) {
  58. case 'endColor':
  59. return $this->parent->getSharedComponent()->getEndColor();
  60. case 'color':
  61. return $this->parent->getSharedComponent()->getColor();
  62. case 'startColor':
  63. return $this->parent->getSharedComponent()->getStartColor();
  64. }
  65. }
  66. /**
  67. * Build style array from subcomponents.
  68. *
  69. * @param array $array
  70. *
  71. * @return array
  72. */
  73. public function getStyleArray($array)
  74. {
  75. return $this->parent->getStyleArray([$this->parentPropertyName => $array]);
  76. }
  77. /**
  78. * Apply styles from array.
  79. *
  80. * <code>
  81. * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray(['rgb' => '808080']);
  82. * </code>
  83. *
  84. * @param array $pStyles Array containing style information
  85. *
  86. * @throws PhpSpreadsheetException
  87. *
  88. * @return Color
  89. */
  90. public function applyFromArray(array $pStyles)
  91. {
  92. if ($this->isSupervisor) {
  93. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  94. } else {
  95. if (isset($pStyles['rgb'])) {
  96. $this->setRGB($pStyles['rgb']);
  97. }
  98. if (isset($pStyles['argb'])) {
  99. $this->setARGB($pStyles['argb']);
  100. }
  101. }
  102. return $this;
  103. }
  104. /**
  105. * Get ARGB.
  106. *
  107. * @return string
  108. */
  109. public function getARGB()
  110. {
  111. if ($this->isSupervisor) {
  112. return $this->getSharedComponent()->getARGB();
  113. }
  114. return $this->argb;
  115. }
  116. /**
  117. * Set ARGB.
  118. *
  119. * @param string $pValue see self::COLOR_*
  120. *
  121. * @return Color
  122. */
  123. public function setARGB($pValue)
  124. {
  125. if ($pValue == '') {
  126. $pValue = self::COLOR_BLACK;
  127. }
  128. if ($this->isSupervisor) {
  129. $styleArray = $this->getStyleArray(['argb' => $pValue]);
  130. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  131. } else {
  132. $this->argb = $pValue;
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Get RGB.
  138. *
  139. * @return string
  140. */
  141. public function getRGB()
  142. {
  143. if ($this->isSupervisor) {
  144. return $this->getSharedComponent()->getRGB();
  145. }
  146. return substr($this->argb, 2);
  147. }
  148. /**
  149. * Set RGB.
  150. *
  151. * @param string $pValue RGB value
  152. *
  153. * @return Color
  154. */
  155. public function setRGB($pValue)
  156. {
  157. if ($pValue == '') {
  158. $pValue = '000000';
  159. }
  160. if ($this->isSupervisor) {
  161. $styleArray = $this->getStyleArray(['argb' => 'FF' . $pValue]);
  162. $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  163. } else {
  164. $this->argb = 'FF' . $pValue;
  165. }
  166. return $this;
  167. }
  168. /**
  169. * Get a specified colour component of an RGB value.
  170. *
  171. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  172. * @param int $offset Position within the RGB value to extract
  173. * @param bool $hex Flag indicating whether the component should be returned as a hex or a
  174. * decimal value
  175. *
  176. * @return string The extracted colour component
  177. */
  178. private static function getColourComponent($RGB, $offset, $hex = true)
  179. {
  180. $colour = substr($RGB, $offset, 2);
  181. if (!$hex) {
  182. $colour = hexdec($colour);
  183. }
  184. return $colour;
  185. }
  186. /**
  187. * Get the red colour component of an RGB value.
  188. *
  189. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  190. * @param bool $hex Flag indicating whether the component should be returned as a hex or a
  191. * decimal value
  192. *
  193. * @return string The red colour component
  194. */
  195. public static function getRed($RGB, $hex = true)
  196. {
  197. return self::getColourComponent($RGB, strlen($RGB) - 6, $hex);
  198. }
  199. /**
  200. * Get the green colour component of an RGB value.
  201. *
  202. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  203. * @param bool $hex Flag indicating whether the component should be returned as a hex or a
  204. * decimal value
  205. *
  206. * @return string The green colour component
  207. */
  208. public static function getGreen($RGB, $hex = true)
  209. {
  210. return self::getColourComponent($RGB, strlen($RGB) - 4, $hex);
  211. }
  212. /**
  213. * Get the blue colour component of an RGB value.
  214. *
  215. * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
  216. * @param bool $hex Flag indicating whether the component should be returned as a hex or a
  217. * decimal value
  218. *
  219. * @return string The blue colour component
  220. */
  221. public static function getBlue($RGB, $hex = true)
  222. {
  223. return self::getColourComponent($RGB, strlen($RGB) - 2, $hex);
  224. }
  225. /**
  226. * Adjust the brightness of a color.
  227. *
  228. * @param string $hex The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
  229. * @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
  230. *
  231. * @return string The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
  232. */
  233. public static function changeBrightness($hex, $adjustPercentage)
  234. {
  235. $rgba = (strlen($hex) == 8);
  236. $red = self::getRed($hex, false);
  237. $green = self::getGreen($hex, false);
  238. $blue = self::getBlue($hex, false);
  239. if ($adjustPercentage > 0) {
  240. $red += (255 - $red) * $adjustPercentage;
  241. $green += (255 - $green) * $adjustPercentage;
  242. $blue += (255 - $blue) * $adjustPercentage;
  243. } else {
  244. $red += $red * $adjustPercentage;
  245. $green += $green * $adjustPercentage;
  246. $blue += $blue * $adjustPercentage;
  247. }
  248. if ($red < 0) {
  249. $red = 0;
  250. } elseif ($red > 255) {
  251. $red = 255;
  252. }
  253. if ($green < 0) {
  254. $green = 0;
  255. } elseif ($green > 255) {
  256. $green = 255;
  257. }
  258. if ($blue < 0) {
  259. $blue = 0;
  260. } elseif ($blue > 255) {
  261. $blue = 255;
  262. }
  263. $rgb = strtoupper(
  264. str_pad(dechex($red), 2, '0', 0) .
  265. str_pad(dechex($green), 2, '0', 0) .
  266. str_pad(dechex($blue), 2, '0', 0)
  267. );
  268. return (($rgba) ? 'FF' : '') . $rgb;
  269. }
  270. /**
  271. * Get indexed color.
  272. *
  273. * @param int $pIndex Index entry point into the colour array
  274. * @param bool $background Flag to indicate whether default background or foreground colour
  275. * should be returned if the indexed colour doesn't exist
  276. *
  277. * @return Color
  278. */
  279. public static function indexedColor($pIndex, $background = false)
  280. {
  281. // Clean parameter
  282. $pIndex = (int) $pIndex;
  283. // Indexed colors
  284. if (self::$indexedColors === null) {
  285. self::$indexedColors = [
  286. 1 => 'FF000000', // System Colour #1 - Black
  287. 2 => 'FFFFFFFF', // System Colour #2 - White
  288. 3 => 'FFFF0000', // System Colour #3 - Red
  289. 4 => 'FF00FF00', // System Colour #4 - Green
  290. 5 => 'FF0000FF', // System Colour #5 - Blue
  291. 6 => 'FFFFFF00', // System Colour #6 - Yellow
  292. 7 => 'FFFF00FF', // System Colour #7- Magenta
  293. 8 => 'FF00FFFF', // System Colour #8- Cyan
  294. 9 => 'FF800000', // Standard Colour #9
  295. 10 => 'FF008000', // Standard Colour #10
  296. 11 => 'FF000080', // Standard Colour #11
  297. 12 => 'FF808000', // Standard Colour #12
  298. 13 => 'FF800080', // Standard Colour #13
  299. 14 => 'FF008080', // Standard Colour #14
  300. 15 => 'FFC0C0C0', // Standard Colour #15
  301. 16 => 'FF808080', // Standard Colour #16
  302. 17 => 'FF9999FF', // Chart Fill Colour #17
  303. 18 => 'FF993366', // Chart Fill Colour #18
  304. 19 => 'FFFFFFCC', // Chart Fill Colour #19
  305. 20 => 'FFCCFFFF', // Chart Fill Colour #20
  306. 21 => 'FF660066', // Chart Fill Colour #21
  307. 22 => 'FFFF8080', // Chart Fill Colour #22
  308. 23 => 'FF0066CC', // Chart Fill Colour #23
  309. 24 => 'FFCCCCFF', // Chart Fill Colour #24
  310. 25 => 'FF000080', // Chart Line Colour #25
  311. 26 => 'FFFF00FF', // Chart Line Colour #26
  312. 27 => 'FFFFFF00', // Chart Line Colour #27
  313. 28 => 'FF00FFFF', // Chart Line Colour #28
  314. 29 => 'FF800080', // Chart Line Colour #29
  315. 30 => 'FF800000', // Chart Line Colour #30
  316. 31 => 'FF008080', // Chart Line Colour #31
  317. 32 => 'FF0000FF', // Chart Line Colour #32
  318. 33 => 'FF00CCFF', // Standard Colour #33
  319. 34 => 'FFCCFFFF', // Standard Colour #34
  320. 35 => 'FFCCFFCC', // Standard Colour #35
  321. 36 => 'FFFFFF99', // Standard Colour #36
  322. 37 => 'FF99CCFF', // Standard Colour #37
  323. 38 => 'FFFF99CC', // Standard Colour #38
  324. 39 => 'FFCC99FF', // Standard Colour #39
  325. 40 => 'FFFFCC99', // Standard Colour #40
  326. 41 => 'FF3366FF', // Standard Colour #41
  327. 42 => 'FF33CCCC', // Standard Colour #42
  328. 43 => 'FF99CC00', // Standard Colour #43
  329. 44 => 'FFFFCC00', // Standard Colour #44
  330. 45 => 'FFFF9900', // Standard Colour #45
  331. 46 => 'FFFF6600', // Standard Colour #46
  332. 47 => 'FF666699', // Standard Colour #47
  333. 48 => 'FF969696', // Standard Colour #48
  334. 49 => 'FF003366', // Standard Colour #49
  335. 50 => 'FF339966', // Standard Colour #50
  336. 51 => 'FF003300', // Standard Colour #51
  337. 52 => 'FF333300', // Standard Colour #52
  338. 53 => 'FF993300', // Standard Colour #53
  339. 54 => 'FF993366', // Standard Colour #54
  340. 55 => 'FF333399', // Standard Colour #55
  341. 56 => 'FF333333', // Standard Colour #56
  342. ];
  343. }
  344. if (isset(self::$indexedColors[$pIndex])) {
  345. return new self(self::$indexedColors[$pIndex]);
  346. }
  347. if ($background) {
  348. return new self(self::COLOR_WHITE);
  349. }
  350. return new self(self::COLOR_BLACK);
  351. }
  352. /**
  353. * Get hash code.
  354. *
  355. * @return string Hash code
  356. */
  357. public function getHashCode()
  358. {
  359. if ($this->isSupervisor) {
  360. return $this->getSharedComponent()->getHashCode();
  361. }
  362. return md5(
  363. $this->argb .
  364. __CLASS__
  365. );
  366. }
  367. }