2010年10月10日日曜日

PHPExcelでセルの背景色指定

背景色の付け方自体は以下の参考リンクから。

PHPExcel-セルのスタイル
PHPExcel(リファレンス1)
PHPExcel セル背景色 & 行と列からアドレスに変換

上記、すごく助かったんですが、実際にどんな値設定すれば何色になるのかリファレンスは見つからず。
PHPExcel_Style_Colorにいくつか有用な定義がされていますが、今一つ何色になるのか分かりにくい。

    const COLOR_BLACK                        = 'FF000000';
   
const COLOR_WHITE                        = 'FFFFFFFF';
   
const COLOR_RED                            = 'FFFF0000';
   
const COLOR_DARKRED                        = 'FF800000';
   
const COLOR_BLUE                        = 'FF0000FF';
   
const COLOR_DARKBLUE                    = 'FF000080';
   
const COLOR_GREEN                        = 'FF00FF00';
   
const COLOR_DARKGREEN                    = 'FF008000';
   
const COLOR_YELLOW                        = 'FFFFFF00';
   
const COLOR_DARKYELLOW                    = 'FF808000';

   
public static function indexedColor($pIndex) {
       
// Clean parameter
        $pIndex
= intval($pIndex);

       
// Indexed colors
       
if (is_null(self::$_indexedColors)) {
           
self::$_indexedColors = array();
           
self::$_indexedColors[] = '00000000';
           
self::$_indexedColors[] = '00FFFFFF';
           
self::$_indexedColors[] = '00FF0000';
           
self::$_indexedColors[] = '0000FF00';
           
self::$_indexedColors[] = '000000FF';
           
self::$_indexedColors[] = '00FFFF00';
           
self::$_indexedColors[] = '00FF00FF';
           
self::$_indexedColors[] = '0000FFFF';
           
self::$_indexedColors[] = '00000000';
           
self::$_indexedColors[] = '00FFFFFF';
           
self::$_indexedColors[] = '00FF0000';
           
self::$_indexedColors[] = '0000FF00';
           
self::$_indexedColors[] = '000000FF';
           
self::$_indexedColors[] = '00FFFF00';
           
self::$_indexedColors[] = '00FF00FF';
           
self::$_indexedColors[] = '0000FFFF';
           
self::$_indexedColors[] = '00800000';
           
self::$_indexedColors[] = '00008000';
           
self::$_indexedColors[] = '00000080';
           
self::$_indexedColors[] = '00808000';
           
self::$_indexedColors[] = '00800080';
           
self::$_indexedColors[] = '00008080';
           
self::$_indexedColors[] = '00C0C0C0';
           
self::$_indexedColors[] = '00808080';
           
self::$_indexedColors[] = '009999FF';
           
self::$_indexedColors[] = '00993366';
           
self::$_indexedColors[] = '00FFFFCC';
           
self::$_indexedColors[] = '00CCFFFF';
           
self::$_indexedColors[] = '00660066';
           
self::$_indexedColors[] = '00FF8080';
           
self::$_indexedColors[] = '000066CC';
           
self::$_indexedColors[] = '00CCCCFF';
           
self::$_indexedColors[] = '00000080';
           
self::$_indexedColors[] = '00FF00FF';
           
self::$_indexedColors[] = '00FFFF00';
           
self::$_indexedColors[] = '0000FFFF';
           
self::$_indexedColors[] = '00800080';
           
self::$_indexedColors[] = '00800000';
           
self::$_indexedColors[] = '00008080';
           
self::$_indexedColors[] = '000000FF';
           
self::$_indexedColors[] = '0000CCFF';
           
self::$_indexedColors[] = '00CCFFFF';
           
self::$_indexedColors[] = '00CCFFCC';
           
self::$_indexedColors[] = '00FFFF99';
           
self::$_indexedColors[] = '0099CCFF';
           
self::$_indexedColors[] = '00FF99CC';
           
self::$_indexedColors[] = '00CC99FF';
           
self::$_indexedColors[] = '00FFCC99';
           
self::$_indexedColors[] = '003366FF';
           
self::$_indexedColors[] = '0033CCCC';
           
self::$_indexedColors[] = '0099CC00';
           
self::$_indexedColors[] = '00FFCC00';
           
self::$_indexedColors[] = '00FF9900';
           
self::$_indexedColors[] = '00FF6600';
           
self::$_indexedColors[] = '00666699';
           
self::$_indexedColors[] = '00969696';
           
self::$_indexedColors[] = '00003366';
           
self::$_indexedColors[] = '00339966';
           
self::$_indexedColors[] = '00003300';
           
self::$_indexedColors[] = '00333300';
           
self::$_indexedColors[] = '00993300';
           
self::$_indexedColors[] = '00993366';
           
self::$_indexedColors[] = '00333399';
           
self::$_indexedColors[] = '00333333';
       
}

       
if (array_key_exists($pIndex, self::$_indexedColors)) {
           
return new PHPExcel_Style_Color(self::$_indexedColors[$pIndex]);
       
}

       
return new PHPExcel_Style_Color();
   
}

ということで、Excel標準のカラーパレットに定義されている色のARGBを調べました。
カラーパレット左上から右順で、

FF000000, FF993300, FF333300, FF003300, FF003366, FF000080, FF333399, FF333333,
FF800000
, FFFF6600, FF808000, FF008000, FF008080, FF0000FF, FF666699, FF808080,
FFFF0000
, FFFF9900, FF99CC00, FF339966, FF33CCCC, FF3366FF, FF800080, FF969696,
FFFF00FF
, FFFFCC00, FFFFFF00, FF00FF00, FF00FFFF, FF00CCFF, FF993366, FFC0C0C0,
FFFF99CC
, FFFFCC99, FFFFFF99, FFCCFFCC, FFCCFFFF, FF99CCFF, FFCC99FF, FFFFFFFF,

控えておくと地味に役に立つこともあるかも。

3 件のコメント:

  1. このカラーパレットはExcel2003ですか?
    アルファ値は分からないけど、調べてみたら違っていたので、

    返信削除
  2. Excel2003ですね。
    2007とか2010だと違うかも・・・。

    ちなみにPHPExcelで.xlsx扱うとZIPのアーカイブとか色々必要になります。
    また、多分、記事書いた時はPHPExcel1.7.3だったと思います。

    返信削除
  3. PHPExcelを使うと色空間が変わってしまって、
    どうにも思い通りの色が出せませんでした。

    カラーチャートを読み込んで、getARGBしてみました。
    http://misaki.sitemix.jp/phpexcel_color.zip

    返信削除