Skip to content

Commit 2d2fe52

Browse files
authored
Merge pull request #1387 from SailorMax/html_writer_auto_invert_text_color
Html writer auto invert text color
2 parents 81af913 + 8931ab1 commit 2d2fe52

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

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

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

tests/PhpWord/Writer/HTML/ElementTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ public function testWriteColSpan()
8686
$section = $phpWord->addSection();
8787
$table = $section->addTable();
8888
$row1 = $table->addRow();
89-
$cell11 = $row1->addCell(1000, array('gridSpan' => 2));
89+
$cell11 = $row1->addCell(1000, array('gridSpan' => 2, 'bgColor' => '6086B8'));
9090
$cell11->addText('cell spanning 2 bellow');
9191
$row2 = $table->addRow();
92-
$cell21 = $row2->addCell(500);
92+
$cell21 = $row2->addCell(500, array('bgColor' => 'ffffff'));
9393
$cell21->addText('first cell');
9494
$cell22 = $row2->addCell(500);
9595
$cell22->addText('second cell');
@@ -100,6 +100,11 @@ public function testWriteColSpan()
100100
$this->assertEquals(1, $xpath->query('/html/body/table/tr[1]/td')->length);
101101
$this->assertEquals('2', $xpath->query('/html/body/table/tr/td[1]')->item(0)->attributes->getNamedItem('colspan')->textContent);
102102
$this->assertEquals(2, $xpath->query('/html/body/table/tr[2]/td')->length);
103+
104+
$this->assertEquals('#6086B8', $xpath->query('/html/body/table/tr[1]/td')->item(0)->attributes->getNamedItem('bgcolor')->textContent);
105+
$this->assertEquals('#ffffff', $xpath->query('/html/body/table/tr[1]/td')->item(0)->attributes->getNamedItem('color')->textContent);
106+
$this->assertEquals('#ffffff', $xpath->query('/html/body/table/tr[2]/td')->item(0)->attributes->getNamedItem('bgcolor')->textContent);
107+
$this->assertNull($xpath->query('/html/body/table/tr[2]/td')->item(0)->attributes->getNamedItem('color'));
103108
}
104109

105110
/**

0 commit comments

Comments
 (0)