Color.php 13 KB

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