Wiki source for ColorAction

Show raw source

=====Development page for the Color / Colour action=====

**See also**
[[Docs:ColorActionInfo | Documentation page]]

The color action, as of version, has some disadvantages:

1) it does no verification if a color is actually given or if it is valid, therefore produces invalid html
1) it causes two php-notices
1) it has problems with some kinds of text
1) 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.''

* 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 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 ***
if (is_array($vars)) {
foreach ($vars as $param => $value) {
switch ($param)
case 'text':
$text = $this->htmlspecialchars_ent($value);
case 'c':
case 'hex':
if (in_array(strtolower($value), $html_color_names)) $colorcode = strtolower($value);
else if (preg_match($hex_color_regexp, $value)) $colorcode = $value;
case 'bg':
if (in_array(strtolower($value), $html_color_names)) $backgroundcolor = strtolower($value);
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 ***
print ($output);
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki