Skip to content

Commit ebf5cf7

Browse files
committed
Convert named constant colors to RGB in Shared/Converter.
Otherwise, colors will not be as expected for RTF and ODT.
1 parent 2d60f32 commit ebf5cf7

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/PhpWord/Shared/Converter.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,50 @@ public static function angleToDegree($angle = 1)
272272
return round($angle / self::DEGREE_TO_ANGLE);
273273
}
274274

275+
/**
276+
* Convert colorname as string to RGB
277+
*
278+
* @param string $value color name
279+
* @return string color as hex RGB string, or original value if unknown
280+
*/
281+
public static function stringToRgb($value)
282+
{
283+
switch ($value) {
284+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_YELLOW:
285+
return 'FFFF00';
286+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGREEN:
287+
return '90EE90';
288+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_CYAN:
289+
return '00FFFF';
290+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_MAGENTA:
291+
return 'FF00FF';
292+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLUE:
293+
return '0000FF';
294+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_RED:
295+
return 'FF0000';
296+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKBLUE:
297+
return '00008B';
298+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKCYAN:
299+
return '008B8B';
300+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGREEN:
301+
return '006400';
302+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKMAGENTA:
303+
return '8B008B';
304+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKRED:
305+
return '8B0000';
306+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKYELLOW:
307+
return '8B8B00';
308+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKGRAY:
309+
return 'A9A9A9';
310+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_LIGHTGRAY:
311+
return 'D3D3D3';
312+
case \PhpOffice\PhpWord\Style\Font::FGCOLOR_BLACK:
313+
return '000000';
314+
}
315+
316+
return $value;
317+
}
318+
275319
/**
276320
* Convert HTML hexadecimal to RGB
277321
*
@@ -283,6 +327,7 @@ public static function htmlToRgb($value)
283327
if ($value[0] == '#') {
284328
$value = substr($value, 1);
285329
}
330+
$value = self::stringToRgb($value);
286331

287332
if (strlen($value) == 6) {
288333
list($red, $green, $blue) = array($value[0] . $value[1], $value[2] . $value[3], $value[4] . $value[5]);

tests/PhpWord/Shared/ConverterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public function testHtmlToRGB()
114114
$values[] = array('FF99DD', array(255, 153, 221)); // 6 characters
115115
$values[] = array('F9D', array(255, 153, 221)); // 3 characters
116116
$values[] = array('0F9D', false); // 4 characters
117+
$values[] = array(\PhpOffice\PhpWord\Style\Font::FGCOLOR_DARKMAGENTA, array(139, 0, 139));
118+
$values[] = array('unknow', array(0, 0, 0)); // 6 characters, invalid
117119
// Conduct test
118120
foreach ($values as $value) {
119121
$result = Converter::htmlToRgb($value[0]);

0 commit comments

Comments
 (0)