PHPExcel_Style
[ class tree: PHPExcel_Style ] [ index: PHPExcel_Style ] [ all elements ]

Source for file Color.php

Documentation is available at Color.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Style_Color
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Style
  34.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. class PHPExcel_Style_Color implements PHPExcel_IComparable
  37. {
  38.     /* Colors */
  39.     const COLOR_BLACK                        'FF000000';
  40.     const COLOR_WHITE                        'FFFFFFFF';
  41.     const COLOR_RED                            'FFFF0000';
  42.     const COLOR_DARKRED                        'FF800000';
  43.     const COLOR_BLUE                        'FF0000FF';
  44.     const COLOR_DARKBLUE                    'FF000080';
  45.     const COLOR_GREEN                        'FF00FF00';
  46.     const COLOR_DARKGREEN                    'FF008000';
  47.     const COLOR_YELLOW                        'FFFFFF00';
  48.     const COLOR_DARKYELLOW                    'FF808000';
  49.  
  50.     /**
  51.      * Indexed colors array
  52.      *
  53.      * @var array 
  54.      */
  55.     private static $_indexedColors;
  56.  
  57.     /**
  58.      * ARGB - Alpha RGB
  59.      *
  60.      * @var string 
  61.      */
  62.     private $_argb;
  63.  
  64.     /**
  65.      * Supervisor?
  66.      *
  67.      * @var boolean 
  68.      */
  69.     private $_isSupervisor;
  70.  
  71.     /**
  72.      * Parent. Only used for supervisor
  73.      *
  74.      * @var mixed 
  75.      */
  76.     private $_parent;
  77.  
  78.     /**
  79.      * Parent property name
  80.      *
  81.      * @var string 
  82.      */
  83.     private $_parentPropertyName;
  84.  
  85.     /**
  86.      * Create a new PHPExcel_Style_Color
  87.      *
  88.      * @param string $pARGB 
  89.      */
  90.     public function __construct($pARGB PHPExcel_Style_Color::COLOR_BLACK$isSupervisor false)
  91.     {
  92.         // Supervisor?
  93.         $this->_isSupervisor $isSupervisor;
  94.  
  95.         // Initialise values
  96.         $this->_argb            $pARGB;
  97.     }
  98.  
  99.     /**
  100.      * Bind parent. Only used for supervisor
  101.      *
  102.      * @param mixed $parent 
  103.      * @param string $parentPropertyName 
  104.      * @return PHPExcel_Style_Color 
  105.      */
  106.     public function bindParent($parent$parentPropertyName)
  107.     {
  108.         $this->_parent $parent;
  109.         $this->_parentPropertyName $parentPropertyName;
  110.         return $this;
  111.     }
  112.  
  113.     /**
  114.      * Is this a supervisor or a real style component?
  115.      *
  116.      * @return boolean 
  117.      */
  118.     public function getIsSupervisor()
  119.     {
  120.         return $this->_isSupervisor;
  121.     }
  122.  
  123.     /**
  124.      * Get the shared style component for the currently active cell in currently active sheet.
  125.      * Only used for style supervisor
  126.      *
  127.      * @return PHPExcel_Style_Color 
  128.      */
  129.     public function getSharedComponent()
  130.     {
  131.         switch ($this->_parentPropertyName{
  132.         case '_endColor':
  133.             return $this->_parent->getSharedComponent()->getEndColor();
  134.             break;
  135.  
  136.         case '_color':
  137.             return $this->_parent->getSharedComponent()->getColor();
  138.             break;
  139.  
  140.         case '_startColor':
  141.             return $this->_parent->getSharedComponent()->getStartColor();
  142.             break;
  143.         }
  144.     }
  145.  
  146.     /**
  147.      * Get the currently active sheet. Only used for supervisor
  148.      *
  149.      * @return PHPExcel_Worksheet 
  150.      */
  151.     public function getActiveSheet()
  152.     {
  153.         return $this->_parent->getActiveSheet();
  154.     }
  155.  
  156.     /**
  157.      * Get the currently active cell coordinate in currently active sheet.
  158.      * Only used for supervisor
  159.      *
  160.      * @return string E.g. 'A1'
  161.      */
  162.     public function getSelectedCells()
  163.     {
  164.         return $this->getActiveSheet()->getSelectedCells();
  165.     }
  166.  
  167.     /**
  168.      * Get the currently active cell coordinate in currently active sheet.
  169.      * Only used for supervisor
  170.      *
  171.      * @return string E.g. 'A1'
  172.      */
  173.     public function getActiveCell()
  174.     {
  175.         return $this->getActiveSheet()->getActiveCell();
  176.     }
  177.  
  178.     /**
  179.      * Build style array from subcomponents
  180.      *
  181.      * @param array $array 
  182.      * @return array 
  183.      */
  184.     public function getStyleArray($array)
  185.     {
  186.         switch ($this->_parentPropertyName{
  187.         case '_endColor':
  188.             $key 'endcolor';
  189.             break;
  190.  
  191.         case '_color':
  192.             $key 'color';
  193.             break;
  194.  
  195.         case '_startColor':
  196.             $key 'startcolor';
  197.             break;
  198.  
  199.         }
  200.         return $this->_parent->getStyleArray(array($key => $array));
  201.     }
  202.  
  203.     /**
  204.      * Apply styles from array
  205.      *
  206.      * <code>
  207.      * $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray( array('rgb' => '808080') );
  208.      * </code>
  209.      *
  210.      * @param    array    $pStyles    Array containing style information
  211.      * @throws    Exception
  212.      * @return PHPExcel_Style_Color 
  213.      */
  214.     public function applyFromArray($pStyles null{
  215.         if (is_array($pStyles)) {
  216.             if ($this->_isSupervisor{
  217.                 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  218.             else {
  219.                 if (array_key_exists('rgb'$pStyles)) {
  220.                     $this->setRGB($pStyles['rgb']);
  221.                 }
  222.                 if (array_key_exists('argb'$pStyles)) {
  223.                     $this->setARGB($pStyles['argb']);
  224.                 }
  225.             }
  226.         else {
  227.             throw new Exception("Invalid style array passed.");
  228.         }
  229.         return $this;
  230.     }
  231.  
  232.     /**
  233.      * Get ARGB
  234.      *
  235.      * @return string 
  236.      */
  237.     public function getARGB({
  238.         if ($this->_isSupervisor{
  239.             return $this->getSharedComponent()->getARGB();
  240.         }
  241.         return $this->_argb;
  242.     }
  243.  
  244.     /**
  245.      * Set ARGB
  246.      *
  247.      * @param string $pValue 
  248.      * @return PHPExcel_Style_Color 
  249.      */
  250.     public function setARGB($pValue PHPExcel_Style_Color::COLOR_BLACK{
  251.         if ($pValue == ''{
  252.             $pValue PHPExcel_Style_Color::COLOR_BLACK;
  253.         }
  254.         if ($this->_isSupervisor{
  255.             $styleArray $this->getStyleArray(array('argb' => $pValue));
  256.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  257.         else {
  258.             $this->_argb $pValue;
  259.         }
  260.         return $this;
  261.     }
  262.  
  263.     /**
  264.      * Get RGB
  265.      *
  266.      * @return string 
  267.      */
  268.     public function getRGB({
  269.         if ($this->_isSupervisor{
  270.             return $this->getSharedComponent()->getRGB();
  271.         }
  272.         return substr($this->_argb2);
  273.     }
  274.  
  275.     /**
  276.      * Set RGB
  277.      *
  278.      * @param string $pValue 
  279.      * @return PHPExcel_Style_Color 
  280.      */
  281.     public function setRGB($pValue '000000'{
  282.         if ($pValue == ''{
  283.             $pValue '000000';
  284.         }
  285.         if ($this->_isSupervisor{
  286.             $styleArray $this->getStyleArray(array('argb' => 'FF' $pValue));
  287.             $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
  288.         else {
  289.             $this->_argb 'FF' $pValue;
  290.         }
  291.         return $this;
  292.     }
  293.  
  294.     /**
  295.      * Get indexed color
  296.      *
  297.      * @param    int        $pIndex 
  298.      * @return    PHPExcel_Style_Color 
  299.      */
  300.     public static function indexedColor($pIndex{
  301.         // Clean parameter
  302.         $pIndex intval($pIndex);
  303.  
  304.         // Indexed colors
  305.         if (is_null(self::$_indexedColors)) {
  306.             self::$_indexedColors array();
  307.             self::$_indexedColors['00000000';
  308.             self::$_indexedColors['00FFFFFF';
  309.             self::$_indexedColors['00FF0000';
  310.             self::$_indexedColors['0000FF00';
  311.             self::$_indexedColors['000000FF';
  312.             self::$_indexedColors['00FFFF00';
  313.             self::$_indexedColors['00FF00FF';
  314.             self::$_indexedColors['0000FFFF';
  315.             self::$_indexedColors['00000000';
  316.             self::$_indexedColors['00FFFFFF';
  317.             self::$_indexedColors['00FF0000';
  318.             self::$_indexedColors['0000FF00';
  319.             self::$_indexedColors['000000FF';
  320.             self::$_indexedColors['00FFFF00';
  321.             self::$_indexedColors['00FF00FF';
  322.             self::$_indexedColors['0000FFFF';
  323.             self::$_indexedColors['00800000';
  324.             self::$_indexedColors['00008000';
  325.             self::$_indexedColors['00000080';
  326.             self::$_indexedColors['00808000';
  327.             self::$_indexedColors['00800080';
  328.             self::$_indexedColors['00008080';
  329.             self::$_indexedColors['00C0C0C0';
  330.             self::$_indexedColors['00808080';
  331.             self::$_indexedColors['009999FF';
  332.             self::$_indexedColors['00993366';
  333.             self::$_indexedColors['00FFFFCC';
  334.             self::$_indexedColors['00CCFFFF';
  335.             self::$_indexedColors['00660066';
  336.             self::$_indexedColors['00FF8080';
  337.             self::$_indexedColors['000066CC';
  338.             self::$_indexedColors['00CCCCFF';
  339.             self::$_indexedColors['00000080';
  340.             self::$_indexedColors['00FF00FF';
  341.             self::$_indexedColors['00FFFF00';
  342.             self::$_indexedColors['0000FFFF';
  343.             self::$_indexedColors['00800080';
  344.             self::$_indexedColors['00800000';
  345.             self::$_indexedColors['00008080';
  346.             self::$_indexedColors['000000FF';
  347.             self::$_indexedColors['0000CCFF';
  348.             self::$_indexedColors['00CCFFFF';
  349.             self::$_indexedColors['00CCFFCC';
  350.             self::$_indexedColors['00FFFF99';
  351.             self::$_indexedColors['0099CCFF';
  352.             self::$_indexedColors['00FF99CC';
  353.             self::$_indexedColors['00CC99FF';
  354.             self::$_indexedColors['00FFCC99';
  355.             self::$_indexedColors['003366FF';
  356.             self::$_indexedColors['0033CCCC';
  357.             self::$_indexedColors['0099CC00';
  358.             self::$_indexedColors['00FFCC00';
  359.             self::$_indexedColors['00FF9900';
  360.             self::$_indexedColors['00FF6600';
  361.             self::$_indexedColors['00666699';
  362.             self::$_indexedColors['00969696';
  363.             self::$_indexedColors['00003366';
  364.             self::$_indexedColors['00339966';
  365.             self::$_indexedColors['00003300';
  366.             self::$_indexedColors['00333300';
  367.             self::$_indexedColors['00993300';
  368.             self::$_indexedColors['00993366';
  369.             self::$_indexedColors['00333399';
  370.             self::$_indexedColors['00333333';
  371.         }
  372.  
  373.         if (array_key_exists($pIndexself::$_indexedColors)) {
  374.             return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
  375.         }
  376.  
  377.         return new PHPExcel_Style_Color();
  378.     }
  379.  
  380.     /**
  381.      * Get hash code
  382.      *
  383.      * @return string    Hash code
  384.      */
  385.     public function getHashCode({
  386.         if ($this->_isSupervisor{
  387.             return $this->getSharedComponent()->getHashCode();
  388.         }
  389.         return md5(
  390.               $this->_argb
  391.             . __CLASS__
  392.         );
  393.     }
  394.  
  395.     /**
  396.      * Implement PHP __clone to create a deep clone, not just a shallow copy.
  397.      */
  398.     public function __clone({
  399.         $vars get_object_vars($this);
  400.         foreach ($vars as $key => $value{
  401.             if (is_object($value)) {
  402.                 $this->$key clone $value;
  403.             else {
  404.                 $this->$key $value;
  405.             }
  406.         }
  407.     }
  408. }

Documentation generated on Tue, 01 Jun 2010 17:02:34 +0200 by phpDocumentor 1.4.3