4b13f8b3d629623a33cb06720c1c16486489a65e.svn-base 13 KB

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