[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Style/ -> Color.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1