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 ***
- $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':
- break;
- case 'bg':
- }
- }
- }
- // *** 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);
- ?>
- Why use two different parameters (each with variations to boot) for specifying a color? What happens if *both* are specified?
- Three-digit hex colors are valid in CSS as well; the regexp doesn't cover this
- Only a single regexp would be needed to cover all possibilities
- Comment block states that both color and background color are mandatory - but the code doesn't seem to behave like that: if neither are specified there is no output (no error message either!); if only one is specified there will be output.
- Comment block doesn't mention $text but I guess that certainly should be required (with an error message if not provided or an empty string)
Later: .. One issue brought up was how to include quote marks within a colored text item. We had addressed that by offering up the instructions on using the & quot tag within the text string, which made folks happy. This file does its thing as designed, which means that our previous 'fix" no longer works.