Development page for the Color / Colour action
The color action, as of version 1.1.6.0, has some disadvantages:
- it does no verification if a color is actually given or if it is valid, therefore produces invalid html
- it causes two php-notices
- it has problems with some kinds of text
- it does not use the standard for action-params
the code below solves this issues and adds support for a background-color, too:
Update 27/03/2006: new version which (hopefully) addresses the issues raised by jw in the comments. Please test.
Update 28/03/2006: Regexp for hex-values works now. Error-messages wraped in class.
<?php
/**
* Colors a given text.
*
* You can specifiy either one of htmls defined names or hex-values
* (with the former one takinge precedency). Same for the background-color.
*
* @package Actions
* @name Color
*
* @author ?, probably Hendrik Mans
* @author {@link http://www.wikkawiki.org/NilsLindenberg NilsLindenberg} (modifications)
*
* @version 1.3
* @since ?
*
* @input string $text mandatory: the text which should be colored.
* @input string $c mandatory: (html)name or hex-value of the color for the text;
* @input string $hex optional: (html)name or hex-value of the color for the text,
* keept for backwards-compatibility;
* @input string $bg optional: (html)name or hex-value for the backgroundcolor;
* @output colored text
*
* @todo make it part of the formatter instead of using an action
*/
// *** Constant section ***
define('ERROR_NO_TEXT_GIVEN',
'There is no text to highlight!');
define('ERROR_NO_COLOR_SPECIFIED',
'Sorry, but you did not specify a color for highlighting!');
$html_color_names =
array ('aqua',
'black',
'blue',
'fuchsia',
'gray',
'green',
'lime',
'maroon',
'navy',
'olive',
'purple',
'red',
'silver',
'teal',
'yellow',
'white');
$hex_color_regexp = '/^\#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/';
// initialization
$text = '<em class="error">'.ERROR_NO_TEXT_GIVEN.'</em>';
$style = $colorcode = $backgroundcolor = $output = '';
// *** User input section ***
foreach ($vars as $param => $value) {
switch ($param)
{
case 'text':
$text = $this->htmlspecialchars_ent($value);
break;
case 'c':
case 'hex':
else if (preg_match($hex_color_regexp,
$value)) $colorcode =
$value;
break;
case 'bg':
else if (preg_match($hex_color_regexp,
$value)) $backgroundcolor =
$value;
}
}
}
// *** prepare the output ***
if ($colorcode !== '') $style .= 'color:'.$colorcode.';';
if ($backgroundcolor !== '') $style .= 'background-color:'.$backgroundcolor.';';
if ($style !== '') $output .= '<span style="'.$style.'">'.$text.'</span>';
else $output = $text.' <em class="error">'.ERROR_NO_COLOR_SPECIFIED.'</em>';
// *** Output section ***
?>