Development page for the Color / Colour action




The color action, as of version 1.1.6.0, 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.

  1. <?php
  2. /**
  3.  * Colors a given text.
  4.  *
  5.  * You can specifiy either one of htmls defined names or hex-values
  6.  * (with the former one takinge precedency). Same for the background-color.
  7.  *
  8.  * @package Actions
  9.  * @name    Color
  10.  *
  11.  * @author  ?, probably Hendrik Mans
  12.  * @author  {@link http://www.wikkawiki.org/NilsLindenberg NilsLindenberg} (modifications)
  13.  *
  14.  * @version    1.3
  15.  * @since   ?
  16.  *
  17.  * @input   string $text mandatory: the text which should be colored.  
  18.  * @input   string $c mandatory: (html)name or hex-value of the color for the text;
  19.  * @input   string $hex optional: (html)name or hex-value of the color for the text,
  20.  *      keept for backwards-compatibility;
  21.  * @input   string $bg optional: (html)name or hex-value for the backgroundcolor;
  22.  * @output  colored text
  23.  *
  24.  * @todo    make it part of the formatter instead of using an action
  25.  */
  26.  
  27. // *** Constant section ***
  28. define('ERROR_NO_TEXT_GIVEN','There is no text to highlight!');
  29. define('ERROR_NO_COLOR_SPECIFIED', 'Sorry, but you did not specify a color for highlighting!');
  30.  
  31. $html_color_names = array ('aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', 'yellow', 'white');
  32. $hex_color_regexp = '/^\#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})$/';
  33.  
  34. // initialization
  35. $text = '<em class="error">'.ERROR_NO_TEXT_GIVEN.'</em>';
  36. $style = $colorcode = $backgroundcolor = $output = '';
  37.  
  38. // *** User input section ***
  39. if (is_array($vars)) {
  40.     foreach ($vars as $param => $value) {
  41.         switch ($param)
  42.             {
  43.                 case 'text':
  44.                     $text = $this->htmlspecialchars_ent($value);
  45.                     break;
  46.                 case 'c':
  47.                 case 'hex':
  48.                     if (in_array(strtolower($value), $html_color_names)) $colorcode = strtolower($value);
  49.                     else if (preg_match($hex_color_regexp, $value)) $colorcode = $value;
  50.                     break;
  51.                 case 'bg':
  52.                     if (in_array(strtolower($value), $html_color_names)) $backgroundcolor = strtolower($value);
  53.                     else if (preg_match($hex_color_regexp, $value)) $backgroundcolor = $value;
  54.             }
  55.     }
  56. }  
  57.  
  58. // *** prepare the output ***
  59. if ($colorcode !== '') $style .= 'color:'.$colorcode.';';
  60. if ($backgroundcolor !== '') $style .= 'background-color:'.$backgroundcolor.';';
  61. if ($style !== '') $output .= '<span style="'.$style.'">'.$text.'</span>';
  62. else $output = $text.' <em class="error">'.ERROR_NO_COLOR_SPECIFIED.'</em>';
  63.  
  64. // *** Output section ***
  65. print ($output);
  66. ?>
Comments
Comment by JavaWoman
2005-11-29 15:10:06
While you're modifying it...
- 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)
Comment by WazoO
2006-03-27 11:44:16
Quick install, small test while writing up the additional instructions .... functional ... definitely different than the hack I threw in mine last week ... thanks.
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.
Comment by DarTar
2006-03-28 08:42:03
Nils, why don't you keep track of these changes in a branch of the SVN repository? We should start doing this automatically for all development code.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki