Revision [13622]

This is an old revision of ColorAction made by NilsLindenberg on 2006-03-27 08:10:03.

 

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.

  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).
  7.  *
  8.  * @package Actions
  9.  * @name    Color
  10.  *
  11.  * @author  ?, probably Hendrik Mans
  12.  * @author  {@link http://www.wikkawiki.org/NilsLindenberg NilsLindenberg} (modifications)
  13.  * @since   ?
  14.  *
  15.  * @input   string $text mandatory: the text which should be colored.  
  16.  * @input   string $c/ $hex  mandatory: (html)name or hex-value of the color for the text;
  17.  * @input   string $bg optional: (html)name or hex-value for the backgroundcolor;
  18.  * @output  colored text
  19.  *
  20.  * @todo    make it part of the formatter instead of using an action
  21.  */
  22.  
  23. // *** Constant section ***
  24. define('ERROR_NO_TEXT_GIVEN','There is no text to highlight!');
  25. define('ERROR_NO_COLOR_SPECIFIED', 'Sorry, but you did not specify a color for highlighting!');
  26.  
  27. $html_color_names = array ('aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal', 'yellow', 'white');
  28. $hex_color_regexp = '/^\#[0-9A-F]{3|6}$/';
  29.  
  30. // initialization
  31. $text = ERROR_NO_TEXT_GIVEN;
  32. $style = $colorcode = $backgroundcolor = $output = '';
  33.  
  34. // *** User input section ***
  35. if (is_array($vars)) {
  36.     foreach ($vars as $param => $value) {
  37.         switch ($param)
  38.             {
  39.                 case 'text':
  40.                     $text = $this->htmlspecialchars_ent($value);
  41.                     break;
  42.                 case 'c':
  43.                 case 'hex':
  44.                     if (in_array(strtolower($value), $html_color_names)) $colorcode = strtolower($value);
  45.                     else if (preg_match($hex_color_regexp, $value)) $colorcode = $value;
  46.                     break;
  47.                 case 'bg':
  48.                     if (in_array(strtolower($value), $html_color_names)) $backgroundcolor = strtolower($value);
  49.                     else if (preg_match($hex_color_regexp, $value)) $backgroundcolor = $value;
  50.             }
  51.     }
  52. }  
  53.  
  54. // *** prepare the output ***
  55. if ($colorcode !== '') $style .= 'color:'.$colorcode.';';
  56. if ($backgroundcolor !== '') $style .= 'background-color:'.$backgroundcolor.';';
  57. if ($style !== '') $output .= '<span style="'.$style.'">'.$text.'</span>';
  58. else $output = $text.' '.ERROR_NO_COLOR_SPECIFIED;
  59.  
  60. // *** Output section ***
  61. print ($output);
  62. ?>
There are 3 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki