Color.php 15 KB

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