/libs_frame/PhpOffice/PhpSpreadsheet/Style/Color.php

https://github.com/a07061625/swooleyaf · PHP · 410 lines · 239 code · 36 blank · 135 comment · 25 complexity · 8543f233ebd98acf8e37120f842768d9 MD5 · raw file

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