Color.Class.php 15 KB

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