Skip to content

Commit 13fc647

Browse files
committed
html writes / setup table cell color
1 parent d1ce0c3 commit 13fc647

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/PhpWord/Writer/HTML/Element/Table.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public function write()
5050
$rowCellCount = count($rowCells);
5151
for ($j = 0; $j < $rowCellCount; $j++) {
5252
$cellStyle = $rowCells[$j]->getStyle();
53+
$cellBgColor = $cellStyle->getBgColor();
54+
$cellFgColor = null;
55+
if ($cellBgColor) {
56+
$r = hexdec(substr($cellBgColor, 0, 2));
57+
$g = hexdec(substr($cellBgColor, 2, 2));
58+
$b = hexdec(substr($cellBgColor, 4, 2));
59+
$cellFgColor = (($r * 0.299 + $g * 0.587 + $b * 0.114) > 186) ? null : 'ffffff';
60+
}
5361
$cellColSpan = $cellStyle->getGridSpan();
5462
$cellRowSpan = 1;
5563
$cellVMerge = $cellStyle->getVMerge();
@@ -73,7 +81,9 @@ public function write()
7381
$cellTag = $tblHeader ? 'th' : 'td';
7482
$cellColSpanAttr = (is_numeric($cellColSpan) && ($cellColSpan > 1) ? " colspan=\"{$cellColSpan}\"" : '');
7583
$cellRowSpanAttr = ($cellRowSpan > 1 ? " rowspan=\"{$cellRowSpan}\"" : '');
76-
$content .= "<{$cellTag}{$cellColSpanAttr}{$cellRowSpanAttr}>" . PHP_EOL;
84+
$cellBgColorAttr = (is_null($cellBgColor) ? '' : " bgcolor=\"#{$cellBgColor}\"");
85+
$cellFgColorAttr = (is_null($cellFgColor) ? '' : " color=\"#{$cellFgColor}\"");
86+
$content .= "<{$cellTag}{$cellColSpanAttr}{$cellRowSpanAttr}{$cellBgColorAttr}{$cellFgColorAttr}>" . PHP_EOL;
7787
$writer = new Container($this->parentWriter, $rowCells[$j]);
7888
$content .= $writer->write();
7989
if ($cellRowSpan > 1) {

0 commit comments

Comments
 (0)