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. ?>
There are 3 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki