For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 lines
16 KiB

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